Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 795acf3

Browse files
committed
[Validator] Checks if the $normalizer option value is a valid callable
1 parent ecc57ea commit 795acf3

File tree

14 files changed

+260
-0
lines changed

14 files changed

+260
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,9 @@ public function __construct($options = null)
6868
}
6969

7070
parent::__construct($options);
71+
72+
if (null !== $this->normalizer && !\is_callable($options['normalizer'])) {
73+
throw new \InvalidArgumentException('The "normalizer" parameter value is not a valid callable.');
74+
}
7175
}
7276
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,9 @@ public function __construct($options = null)
8484
if (!\in_array($this->version, self::$versions)) {
8585
throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s"', implode('", "', self::$versions)));
8686
}
87+
88+
if (null !== $this->normalizer && !\is_callable($options['normalizer'])) {
89+
throw new \InvalidArgumentException('The "normalizer" parameter value is not a valid callable.');
90+
}
8791
}
8892
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,9 @@ public function __construct($options = null)
5555
if (null === $this->min && null === $this->max) {
5656
throw new MissingOptionsException(sprintf('Either option "min" or "max" must be given for constraint %s', __CLASS__), array('min', 'max'));
5757
}
58+
59+
if (null !== $this->normalizer && !\is_callable($options['normalizer'])) {
60+
throw new \InvalidArgumentException('The "normalizer" parameter value is not a valid callable.');
61+
}
5862
}
5963
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ class NotBlank extends Constraint
2929

3030
public $message = 'This value should not be blank.';
3131
public $normalizer;
32+
33+
public function __construct($options = null)
34+
{
35+
parent::__construct($options);
36+
37+
if (null !== $this->normalizer && !\is_callable($options['normalizer'])) {
38+
throw new \InvalidArgumentException('The "normalizer" parameter value is not a valid callable.');
39+
}
40+
}
3241
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ class Regex extends Constraint
3333
public $match = true;
3434
public $normalizer;
3535

36+
public function __construct($options = null)
37+
{
38+
parent::__construct($options);
39+
40+
if (null !== $this->normalizer && !\is_callable($options['normalizer'])) {
41+
throw new \InvalidArgumentException('The "normalizer" parameter value is not a valid callable.');
42+
}
43+
}
44+
3645
/**
3746
* {@inheritdoc}
3847
*/

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,9 @@ public function __construct($options = null)
119119
}
120120

121121
parent::__construct($options);
122+
123+
if (null !== $this->normalizer && !\is_callable($options['normalizer'])) {
124+
throw new \InvalidArgumentException('The "normalizer" parameter value is not a valid callable.');
125+
}
122126
}
123127
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,13 @@ class Uuid extends Constraint
7676
);
7777

7878
public $normalizer;
79+
80+
public function __construct($options = null)
81+
{
82+
parent::__construct($options);
83+
84+
if (null !== $this->normalizer && !\is_callable($options['normalizer'])) {
85+
throw new \InvalidArgumentException('The "normalizer" parameter value is not a valid callable.');
86+
}
87+
}
7988
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,20 @@ public function testUnknownModesTriggerException()
4242
{
4343
new Email(array('mode' => 'Unknown Mode'));
4444
}
45+
46+
public function testNormalizerCanBeSet()
47+
{
48+
$email = new Email(array('normalizer' => 'trim'));
49+
50+
$this->assertEquals('trim', $email->normalizer);
51+
}
52+
53+
/**
54+
* @expectedException \InvalidArgumentException
55+
* @expectedExceptionMessage The "normalizer" parameter value is not a valid callable.
56+
*/
57+
public function testInvalidNormalizerThrowsException()
58+
{
59+
new Email(array('normalizer' => 'Unknown Callable'));
60+
}
4561
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\Ip;
16+
17+
/**
18+
* @author Renan Taranto <[email protected]>
19+
*/
20+
class IpTest extends TestCase
21+
{
22+
public function testNormalizerCanBeSet()
23+
{
24+
$ip = new Ip(array('normalizer' => 'trim'));
25+
26+
$this->assertEquals('trim', $ip->normalizer);
27+
}
28+
29+
/**
30+
* @expectedException \InvalidArgumentException
31+
* @expectedExceptionMessage The "normalizer" parameter value is not a valid callable.
32+
*/
33+
public function testInvalidNormalizerThrowsException()
34+
{
35+
new Ip(array('normalizer' => 'Unknown Callable'));
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\Length;
16+
17+
/**
18+
* @author Renan Taranto <[email protected]>
19+
*/
20+
class LengthTest extends TestCase
21+
{
22+
public function testNormalizerCanBeSet()
23+
{
24+
$length = new Length(array('min' => 0, 'max' => 10, 'normalizer' => 'trim'));
25+
26+
$this->assertEquals('trim', $length->normalizer);
27+
}
28+
29+
/**
30+
* @expectedException \InvalidArgumentException
31+
* @expectedExceptionMessage The "normalizer" parameter value is not a valid callable.
32+
*/
33+
public function testInvalidNormalizerThrowsException()
34+
{
35+
new Length(array('min' => 0, 'max' => 10, 'normalizer' => 'Unknown Callable'));
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\NotBlank;
16+
17+
/**
18+
* @author Renan Taranto <[email protected]>
19+
*/
20+
class NotBlankTest extends TestCase
21+
{
22+
public function testNormalizerCanBeSet()
23+
{
24+
$notBlank = new NotBlank(array('normalizer' => 'trim'));
25+
26+
$this->assertEquals('trim', $notBlank->normalizer);
27+
}
28+
29+
/**
30+
* @expectedException \InvalidArgumentException
31+
* @expectedExceptionMessage The "normalizer" parameter value is not a valid callable.
32+
*/
33+
public function testInvalidNormalizerThrowsException()
34+
{
35+
new NotBlank(array('normalizer' => 'Unknown Callable'));
36+
}
37+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,20 @@ public function testGetCustomHtmlPattern()
8585
$this->assertSame('((?![0-9]$|[a-z]+).)*', $constraint->pattern);
8686
$this->assertSame('foobar', $constraint->getHtmlPattern());
8787
}
88+
89+
public function testNormalizerCanBeSet()
90+
{
91+
$regex = new Regex(array('pattern' => '/^[0-9]+$/', 'normalizer' => 'trim'));
92+
93+
$this->assertEquals('trim', $regex->normalizer);
94+
}
95+
96+
/**
97+
* @expectedException \InvalidArgumentException
98+
* @expectedExceptionMessage The "normalizer" parameter value is not a valid callable.
99+
*/
100+
public function testInvalidNormalizerThrowsException()
101+
{
102+
new Regex(array('pattern' => '/^[0-9]+$/', 'normalizer' => 'Unknown Callable'));
103+
}
88104
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\Url;
16+
17+
/**
18+
* @author Renan Taranto <[email protected]>
19+
*/
20+
class UrlTest extends TestCase
21+
{
22+
public function testNormalizerCanBeSet()
23+
{
24+
$url = new Url(array('normalizer' => 'trim'));
25+
26+
$this->assertEquals('trim', $url->normalizer);
27+
}
28+
29+
/**
30+
* @expectedException \InvalidArgumentException
31+
* @expectedExceptionMessage The "normalizer" parameter value is not a valid callable.
32+
*/
33+
public function testInvalidNormalizerThrowsException()
34+
{
35+
new Url(array('normalizer' => 'Unknown Callable'));
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\Uuid;
16+
17+
/**
18+
* @author Renan Taranto <[email protected]>
19+
*/
20+
class UuidTest extends TestCase
21+
{
22+
public function testNormalizerCanBeSet()
23+
{
24+
$uuid = new Uuid(array('normalizer' => 'trim'));
25+
26+
$this->assertEquals('trim', $uuid->normalizer);
27+
}
28+
29+
/**
30+
* @expectedException \InvalidArgumentException
31+
* @expectedExceptionMessage The "normalizer" parameter value is not a valid callable.
32+
*/
33+
public function testInvalidNormalizerThrowsException()
34+
{
35+
new Uuid(array('normalizer' => 'Unknown Callable'));
36+
}
37+
}

0 commit comments

Comments
 (0)