Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b716b0d

Browse filesBrowse files
committed
[Validator] Add the Color constraint and validator
1 parent 1c28bf7 commit b716b0d
Copy full SHA for b716b0d

File tree

6 files changed

+196
-0
lines changed
Filter options

6 files changed

+196
-0
lines changed

‎src/Symfony/Component/Validator/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added the `Hostname` constraint and validator
88
* added option `alpha3` to `Country` constraint
9+
* added the `Color` constraint and validator
910

1011
5.0.0
1112
-----
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @see https://www.w3.org/TR/html52/sec-forms.html#color-state-typecolor
21+
*/
22+
final class Color extends Constraint
23+
{
24+
public const INVALID_FORMAT_ERROR = 'e8c5955b-9ee3-451e-9c12-4d18240805db';
25+
26+
public $message = 'This value is not a valid color.';
27+
}
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
18+
19+
final class ColorValidator extends ConstraintValidator
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function validate($value, Constraint $constraint)
25+
{
26+
if (!$constraint instanceof Color) {
27+
throw new UnexpectedTypeException($constraint, Color::class);
28+
}
29+
30+
if (null === $value || '' === $value) {
31+
return;
32+
}
33+
34+
if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
35+
throw new UnexpectedValueException($value, 'string');
36+
}
37+
38+
$value = (string) $value;
39+
40+
if (!preg_match('/^#[0-9a-f]{6}$/i', $value)) {
41+
$this->context->buildViolation($constraint->message)
42+
->setParameter('{{ value }}', $this->formatValue($value))
43+
->setCode(Color::INVALID_FORMAT_ERROR)
44+
->addViolation();
45+
}
46+
}
47+
}

‎src/Symfony/Component/Validator/Resources/translations/validators.en.xlf

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Resources/translations/validators.en.xlf
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@
370370
<source>This value is not a valid hostname.</source>
371371
<target>This value is not a valid hostname.</target>
372372
</trans-unit>
373+
<trans-unit id="96">
374+
<source>This value is not a valid color.</source>
375+
<target>This value is not a valid color.</target>
376+
</trans-unit>
373377
</body>
374378
</file>
375379
</xliff>

‎src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@
370370
<source>This value is not a valid hostname.</source>
371371
<target>Cette valeur n'est pas un nom d'hôte valide.</target>
372372
</trans-unit>
373+
<trans-unit id="96">
374+
<source>This value is not a valid color.</source>
375+
<target>Cette valeur n'est pas une couleur valide.</target>
376+
</trans-unit>
373377
</body>
374378
</file>
375379
</xliff>
+113Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Validator\Constraints\Color;
15+
use Symfony\Component\Validator\Constraints\ColorValidator;
16+
use Symfony\Component\Validator\Constraints\Email;
17+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
19+
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
20+
21+
final class ColorValidatorTest extends ConstraintValidatorTestCase
22+
{
23+
protected function createValidator()
24+
{
25+
return new ColorValidator();
26+
}
27+
28+
public function testUnexpectedType()
29+
{
30+
$this->expectException(UnexpectedTypeException::class);
31+
$this->expectExceptionMessage('Expected argument of type "Symfony\Component\Validator\Constraints\Color", "Symfony\Component\Validator\Constraints\Email" given');
32+
33+
$this->validator->validate(null, new Email());
34+
}
35+
36+
public function testUnexpectedValue()
37+
{
38+
$this->expectException(UnexpectedValueException::class);
39+
$this->expectExceptionMessage('Expected argument of type "string", "stdClass" given');
40+
41+
$this->validator->validate(new \stdClass(), new Color());
42+
}
43+
44+
public function testNullIsValid()
45+
{
46+
$this->validator->validate(null, new Color());
47+
48+
$this->assertNoViolation();
49+
}
50+
51+
public function testEmptyStringIsValid()
52+
{
53+
$this->validator->validate('', new Color());
54+
55+
$this->assertNoViolation();
56+
}
57+
58+
/**
59+
* @dataProvider getValidColors
60+
*/
61+
public function testValidColors($color)
62+
{
63+
$this->validator->validate($color, new Color());
64+
65+
$this->assertNoViolation();
66+
}
67+
68+
public function getValidColors()
69+
{
70+
return [
71+
['#000000'],
72+
['#abcabc'],
73+
['#BbBbBb'],
74+
[new class() {
75+
public function __toString()
76+
{
77+
return '#1Ee54d';
78+
}
79+
}],
80+
];
81+
}
82+
83+
/**
84+
* @dataProvider getColorsWithInvalidFormat
85+
*/
86+
public function testColorsWithInvalidFormat($color)
87+
{
88+
$this->validator->validate($color, new Color([
89+
'message' => 'foo',
90+
]));
91+
92+
$this->buildViolation('foo')
93+
->setParameter('{{ value }}', '"'.(string) $color.'"')
94+
->setCode(Color::INVALID_FORMAT_ERROR)
95+
->assertRaised();
96+
}
97+
98+
public function getColorsWithInvalidFormat()
99+
{
100+
return [
101+
['000000'],
102+
['#abcabg'],
103+
[' #ffffff'],
104+
['#12345'],
105+
[new class() {
106+
public function __toString()
107+
{
108+
return '#010101 ';
109+
}
110+
}],
111+
];
112+
}
113+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.