8000 Merge branch '2.7' into 2.8 · symfony/symfony@564c8e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 564c8e1

Browse files
Merge branch '2.7' into 2.8
* 2.7: [travis] start hhvm first [Validator] always evaluate binary format when changed
2 parents fc6ed5b + 8c999b5 commit 564c8e1

File tree

3 files changed

+93
-21
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ addons:
99

1010
matrix:
1111
include:
12+
- php: hhvm
1213
- php: 5.3
1314
- php: 5.4
1415
- php: 5.5
@@ -18,7 +19,6 @@ matrix:
1819
- php: 5.6
1920
env: deps=high
2021
- php: nightly
21-
- php: hhvm
2222
allow_failures:
2323
- php: nightly
2424
fast_finish: true

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

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class File extends Constraint
4040
self::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR',
4141
);
4242

43-
public $maxSize;
4443
public $binaryFormat;
4544
public $mimeTypes = array();
4645
public $notFoundMessage = 'The file could not be found.';
@@ -58,29 +57,56 @@ class File extends Constraint
5857
public $uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail.';
5958
public $uploadErrorMessage = 'The file could not be uploaded.';
6059

60+
protected $maxSize;
61+
6162
public function __construct($options = null)
6263
{
6364
parent::__construct($options);
6465

65-
if ($this->maxSize) {
66-
if (ctype_digit((string) $this->maxSize)) {
67-
$this->maxSize = (int) $this->maxSize;
68-
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
69-
} elseif (preg_match('/^\d++k$/i', $this->maxSize)) {
70-
$this->maxSize = $this->maxSize * 1000;
71-
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
72-
} elseif (preg_match('/^\d++M$/i', $this->maxSize)) {
73-
$this->maxSize = $this->maxSize * 1000000;
74-
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
75-
} elseif (preg_match('/^\d++Ki$/i', $this->maxSize)) {
76-
$this->maxSize = $this->maxSize << 10;
77-
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
78-
} elseif (preg_match('/^\d++Mi$/i', $this->maxSize)) {
79-
$this->maxSize = $this->maxSize << 20;
80-
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
81-
} else {
82-
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
83-
}
66+
if (null !== $this->maxSize) {
67+
$this->normalizeBinaryFormat($this->maxSize);
68+
}
69+
}
70+
71+
public function __set($option, $value)
72+
{
73+
if ('maxSize' === $option) {
74+
$this->normalizeBinaryFormat($value);
75+
76+
return;
77+
}
78+
79+
parent::__set($option, $value);
80+
}
81+
82+
public function __get($option)
83+
{
84+
if ('maxSize' === $option) {
85+
return $this->maxSize;
86+
}
87+
88+
return parent::__get($option);
89+
}
90+
91+
private function normalizeBinaryFormat($maxSize)
92+
{
93+
if (ctype_digit((string) $maxSize)) {
94+
$this->maxSize = (int) $maxSize;
95+
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
96+
} elseif (preg_match('/^\d++k$/i', $maxSize)) {
97+
$this->maxSize = $maxSize * 1000;
98+
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
99+
} elseif (preg_match('/^\d++M$/i', $maxSize)) {
100+
$this->maxSize = $maxSize * 1000000;
101+
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
102+
} elseif (preg_match('/^\d++Ki$/i', $maxSize)) {
103+
$this->maxSize = $maxSize << 10;
104+
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
105+
} elseif (preg_match('/^\d++Mi$/i', $maxSize)) {
106+
$this->maxSize = $maxSize << 20;
107+
$this->binaryFormat = null === $this->binaryFormat ? true : $this->binaryFormat;
108+
} else {
109+
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
84110
}
85111
}
86112
}

src/Symfony/Component/Validator/Tests/Constraints/FileTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

1414
use Symfony\Component\Validator\Constraints\File;
15+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1516

1617
class FileTest extends \PHPUnit_Framework_TestCase
1718
{
@@ -29,6 +30,51 @@ public function testMaxSize($maxSize, $bytes, $binaryFormat)
2930
$this->assertSame($binaryFormat, $file->binaryFormat);
3031
}
3132

33+
/**
34+
* @dataProvider provideValidSizes
35+
*
36+
* @param int|string $maxSize
37+
* @param int $bytes
38+
* @param string $binaryFormat
39+
*/
40+
public function testMaxSizeCanBeSetAfterInitialization($maxSize, $bytes, $binaryFormat)
41+
{
42+
$file = new File();
43+
$file->maxSize = $maxSize;
44+
45+
$this->assertSame($bytes, $file->maxSize);
46+
$this->assertSame($binaryFormat, $file->binaryFormat);
47+
}
48+
49+
/**
50+
* @dataProvider provideInvalidSizes
51+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
52+
*
53+
* @param int|string $maxSize
54+
*/
55+
public function testInvalidValueForMaxSizeThrowsExceptionAfterInitialization($maxSize)
56+
{
57+
$file = new File(array('maxSize' => 1000));
58+
$file->maxSize = $maxSize;
59+
}
60+
61+
/**
62+
* @dataProvider provideInvalidSizes
63+
*
64+
* @param int|string $maxSize
65+
*/
66+
public function testMaxSizeCannotBeSetToInvalidValueAfterInitialization($maxSize)
67+
{
68+
$file = new File(array('maxSize' => 1000));
69+
70+
try {
71+
$file->maxSize = $maxSize;
72+
} catch (ConstraintDefinitionException $e) {
73+
}
74+
75+
$this->assertSame(1000, $file->maxSize);
76+
}
77+
3278
/**
3379
* @param mixed $maxSize
3480
* @dataProvider provideInValidSizes

0 commit comments

Comments
 (0)
0