8000 Updated according to PR review · Seldaek/symfony@6ad83e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ad83e7

Browse files
benjamindulaustof
authored andcommitted
Updated according to PR review
1 parent 2de243c commit 6ad83e7

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

src/Symfony/Component/Validator/Constraints/Image.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class Image extends File
2525
public $minHeight = null;
2626

2727
public $mimeTypesMessage = 'This file is not a valid image';
28-
public $notDetectedMessage = 'The size of image could not be detected';
29-
public $maxWidthMessage = 'The image width is too big ({{ width }}px). Allowed maximum width is {{ maxWidth }}px';
30-
public $minWidthMessage = 'The image width is too small ({{ width }}px). Minimum width expected is {{ minWidth }}px';
31-
public $maxHeightMessage = 'The image height is too big ({{ height }}px). Allowed maximum width is {{ maxHeight }}px';
32-
public $minHeightMessage = 'The image width is too small ({{ height }}px). Minimum height expected is {{ minHeight }}px';
28+
public $sizeNotDetectedMessage = 'The size of the image could not be detected';
29+
public $maxWidthMessage = 'The image width is too big ({{ width }}px). Allowed maximum width is {{ max_width }}px';
30+
public $minWidthMessage = 'The image width is too small ({{ width }}px). Minimum width expected is {{ min_width }}px';
31+
public $maxHeightMessage = 'The image height is too big ({{ height }}px). Allowed maximum width is {{ max_eight }}px';
32+
public $minHeightMessage = 'The image width is too small ({{ height }}px). Minimum height expected is {{ min_height }}px';
3333
}

src/Symfony/Component/Validator/Constraints/ImageValidator.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ public function isValid($value, Constraint $constraint)
3333
return true;
3434
}
3535

36+
if (null === $constraint->minWidth && null === $constraint->maxWidth
37+
&& null === $constraint->minHeight && null === $constraint->maxHeight) {
38+
return true;
39+
}
40+
3641
$size = @getimagesize($value);
3742
if (empty($size) or ($size[0] === 0) or ($size[1] === 0)) {
38-
$this->setMessage($constraint->notDetectedMessage);
43+
$this->setMessage($constraint->sizeNotDetectedMessage);
3944

4045
return false;
4146
}
@@ -44,59 +49,59 @@ public function isValid($value, Constraint $constraint)
4449
$height = $size[1];
4550

4651
if ($constraint->minWidth) {
47-
if (!ctype_digit((string)$constraint->minWidth)) {
52+
if (!ctype_digit((string) $constraint->minWidth)) {
4853
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth));
4954
}
5055

5156
if ($width < $constraint->minWidth) {
5257
$this->setMessage($constraint->minWidthMessage, array(
5358
'{{ width }}' => $width,
54-
'{{ minWidth }}' => $constraint->minWidth
59+
'{{ min_width }}' => $constraint->minWidth
5560
));
5661

5762
return false;
5863
}
5964
}
6065

6166
if ($constraint->maxWidth) {
62-
if (!ctype_digit((string)$constraint->maxWidth)) {
67+
if (!ctype_digit((string) $constraint->maxWidth)) {
6368
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth));
6469
}
6570

6671
if ($width > $constraint->maxWidth) {
6772
$this->setMessage($constraint->maxWidthMessage, array(
6873
'{{ width }}' => $width,
69-
'{{ maxWidth }}' => $constraint->maxWidth
74+
'{{ max_width }}' => $constraint->maxWidth
7075
));
7176

7277
return false;
7378
}
7479
}
7580

7681
if ($constraint->minHeight) {
77-
if (!ctype_digit((string)$constraint->minHeight)) {
82+
if (!ctype_digit((string) $constraint->minHeight)) {
7883
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
7984
}
8085

8186
if ($height < $constraint->minHeight) {
8287
$this->setMessage($constraint->minHeightMessage, array(
8388
'{{ height }}' => $height,
84-
'{{ minHeight }}' => $constraint->minHeight
89+
'{{ min_height }}' => $constraint->minHeight
8590
));
8691

8792
return false;
8893
}
8994
}
9095

9196
if ($constraint->maxHeight) {
92-
if (!ctype_digit((string)$constraint->maxHeight)) {
97+
if (!ctype_digit((string) $constraint->maxHeight)) {
9398
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
9499
}
95100

96101
if ($height > $constraint->maxHeight) {
97102
$this->setMessage($constraint->maxHeightMessage, array(
98103
'{{ height }}' => $height,
99-
'{{ maxHeight }}' => $constraint->maxHeight
104+
'{{ max_height }}' => $constraint->maxHeight
100105
));
101106

102107
return false;

tests/Symfony/Tests/Component/Validator/Constraints/ImageValidatorTest.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ protected function setUp()
2626
$this->image = __DIR__.'/Fixtures/test.gif';
2727
}
2828

29-
protected function tearDown()
30-
{
31-
}
32-
3329
public function testNullIsValid()
3430
{
3531
$this->assertTrue($this->validator->isValid(null, new Image()));
@@ -65,11 +61,11 @@ public function testWidthTooSmall()
6561
));
6662

6763
$this->assertFalse($this->validator->isValid($this->image, $constraint));
68-
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
69-
$this->assertEquals($this->validator->getMessageParameters(), array(
64+
$this->assertEquals('myMessage', $this->validator->getMessageTemplate());
65+
$this->assertEquals(array(
7066
'{{ width }}' => '2',
71-
'{{ minWidth }}' => '3',
72-
));
67+
'{{ min_width }}' => '3',
68+
), $this->validator->getMessageParameters());
7369
}
7470

7571
public function testWidthTooBig()
@@ -80,11 +76,11 @@ public function testWidthTooBig()
8076
));
8177

8278
$this->assertFalse($this->validator->isValid($this->image, $constraint));
83-
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
84-
$this->assertEquals($this->validator->getMessageParameters(), array(
79+
$this->assertEquals('myMessage', $this->validator->getMessageTemplate());
80+
$this->assertEquals(array(
8581
'{{ width }}' => '2',
86-
'{{ maxWidth }}' => '1',
87-
));
82+
'{{ max_width }}' => '1',
83+
), $this->validator->getMessageParameters());
8884
}
8985

9086
public function testHeightTooSmall()
@@ -95,11 +91,11 @@ public function testHeightTooSmall()
9591
));
9692

9793
$this->assertFalse($this->validator->isValid($this->image, $constraint));
98-
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
99-
$this->assertEquals($this->validator->getMessageParameters(), array(
94+
$this->assertEquals('myMessage', $this->validator->getMessageTemplate());
95+
$this->assertEquals(array(
10096
'{{ height }}' => '2',
101-
'{{ minHeight }}' => '3',
102-
));
97+
'{{ min_height }}' => '3',
98+
), $this->validator->getMessageParameters());
10399
}
104100

105101
public function testHeightTooBig()
@@ -110,11 +106,11 @@ public function testHeightTooBig()
110106
));
111107

112108
$this->assertFalse($this->validator->isValid($this->image, $constraint));
113-
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
114-
$this->assertEquals($this->validator->getMessageParameters(), array(
109+
$this->assertEquals('myMessage', $this->validator->getMessageTemplate());
110+
$this->assertEquals(array(
115111
'{{ height }}' => '2',
116-
'{{ maxHeight }}' => '1',
117-
));
112+
'{{ max_height }}' => '1',
113+
), $this->validator->getMessageParameters());
118114
}
119115

120116
public function testInvalidMinWidth()

0 commit comments

Comments
 (0)
0