Skip to content

Commit d936533

Browse files
committed
minor #43369 [Validator] [CssColor] Fixed default behavior (welcoMattic)
This PR was merged into the 5.4 branch. Discussion ---------- [Validator] [CssColor] Fixed default behavior | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Following the `@javiereguiluz` PR in docs (symfony/symfony-docs#15906), I figured out I've missed the default behavior of the constraint, which is "all formats are valid if you pass no arguments". It's fixed. I've also replaced an overengineered `array_reduce` with an `implode`. Tests have been updated too. Commits ------- cac2ccfcb5 Fixed default behavior
2 parents 604d6d9 + b9cc4fe commit d936533

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

Constraints/CssColor.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,22 @@ class CssColor extends Constraint
6565
/**
6666
* @param array|string $formats The types of CSS colors allowed (e.g. hexadecimal only, RGB and HSL only, etc.).
6767
*/
68-
public function __construct($formats, string $message = null, array $groups = null, $payload = null, array $options = null)
68+
public function __construct($formats = [], string $message = null, array $groups = null, $payload = null, array $options = null)
6969
{
70-
$validationModesAsString = array_reduce(self::$validationModes, function ($carry, $value) {
71-
return $carry ? $carry.', '.$value : $value;
72-
}, '');
70+
$validationModesAsString = implode(', ', self::$validationModes);
7371

74-
if (\is_array($formats) && \is_string(key($formats))) {
72+
if (!$formats) {
73+
$options['value'] = self::$validationModes;
74+
} elseif (\is_array($formats) && \is_string(key($formats))) {
7575
$options = array_merge($formats, $options);
7676
} elseif (\is_array($formats)) {
77-
if ([] === array_intersect(static::$validationModes, $formats)) {
77+
if ([] === array_intersect(self::$validationModes, $formats)) {
7878
throw new InvalidArgumentException(sprintf('The "formats" parameter value is not valid. It must contain one or more of the following values: "%s".', $validationModesAsString));
7979
}
8080

8181
$options['value'] = $formats;
8282
} elseif (\is_string($formats)) {
83-
if (!\in_array($formats, static::$validationModes)) {
83+
if (!\in_array($formats, self::$validationModes)) {
8484
throw new InvalidArgumentException(sprintf('The "formats" parameter value is not valid. It must contain one or more of the following values: "%s".', $validationModesAsString));
8585
}
8686

Constraints/CssColorValidator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class CssColorValidator extends ConstraintValidator
3232
// List comes from https://drafts.csswg.org/css-color/#css-system-colors
3333
private const PATTERN_SYSTEM_COLORS = '/^(Canvas|CanvasText|LinkText|VisitedText|ActiveText|ButtonFace|ButtonText|ButtonBorder|Field|FieldText|Highlight|HighlightText|SelectedItem|SelectedItemText|Mark|MarkText|GrayText)$/i';
3434
private const PATTERN_KEYWORDS = '/^(transparent|currentColor)$/i';
35-
private const PATTERN_RGB = '/^rgb\((0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s?(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s?(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d)\)$/i';
36-
private const PATTERN_RGBA = '/^rgba\((0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s?(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s?(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s?(0|0?\.\d|1(\.0)?)\)$/i';
37-
private const PATTERN_HSL = '/^hsl\((0|360|35\d|3[0-4]\d|[12]\d\d|0?\d?\d),\s?(0|100|\d{1,2})%,\s?(0|100|\d{1,2})%\)$/i';
38-
private const PATTERN_HSLA = '/^hsla\((0|360|35\d|3[0-4]\d|[12]\d\d|0?\d?\d),\s?(0|100|\d{1,2})%,\s?(0|100|\d{1,2})%,\s?(0?\.\d|1(\.0)?)\)$/i';
35+
private const PATTERN_RGB = '/^rgb\(\s*(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s*(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s*(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d)\s*\)$/i';
36+
private const PATTERN_RGBA = '/^rgba\(\s*(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s*(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s*(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),\s*(0|0?\.\d|1(\.0)?)\s*\)$/i';
37+
private const PATTERN_HSL = '/^hsl\(\s*(0|360|35\d|3[0-4]\d|[12]\d\d|0?\d?\d),\s*(0|100|\d{1,2})%,\s*(0|100|\d{1,2})%\s*\)$/i';
38+
private const PATTERN_HSLA = '/^hsla\(\s*(0|360|35\d|3[0-4]\d|[12]\d\d|0?\d?\d),\s*(0|100|\d{1,2})%,\s*(0|100|\d{1,2})%,\s*(0?\.\d|1(\.0)?)\s*\)$/i';
3939

4040
private const COLOR_PATTERNS = [
4141
CssColor::HEX_LONG => self::PATTERN_HEX_LONG,

Tests/Constraints/CssColorValidatorTest.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ public function testExpectsStringCompatibleType()
4343
$this->validator->validate(new \stdClass(), new CssColor(CssColor::HEX_LONG));
4444
}
4545

46+
/**
47+
* @dataProvider getValidAnyColor
48+
*/
49+
public function testValidAnyColor($cssColor)
50+
{
51+
$this->validator->validate($cssColor, new CssColor());
52+
$this->assertNoViolation();
53+
}
54+
55+
public function getValidAnyColor(): array
56+
{
57+
return [
58+
['#ABCDEF'],
59+
['#ABCDEF00'],
60+
['#F4B'],
61+
['#F4B1'],
62+
['black'],
63+
['aliceblue'],
64+
['Canvas'],
65+
['transparent'],
66+
['rgb(255, 255, 255)'],
67+
['rgba(255, 255, 255, 0.3)'],
68+
['hsl(0, 0%, 20%)'],
69+
['hsla(0, 0%, 20%, 0.4)'],
70+
];
71+
}
72+
4673
/**
4774
* @dataProvider getValidHexLongColors
4875
*/
@@ -177,6 +204,7 @@ public function testValidRGB($cssColor)
177204
public function getValidRGB(): array
178205
{
179206
return [
207+
['rgb(0, 255, 243)'],
180208
['rgb(255, 255, 255)'], ['rgb(0, 0, 0)'], ['rgb(0, 0, 255)'], ['rgb(255, 0, 0)'], ['rgb(122, 122, 122)'], ['rgb(66, 66, 66)'],
181209
['rgb(255,255,255)'], ['rgb(0,0,0)'], ['rgb(0,0,255)'], ['rgb(255,0,0)'], ['rgb(122,122,122)'], ['rgb(66,66,66)'],
182210
];
@@ -194,6 +222,7 @@ public function testValidRGBA($cssColor)
194222
public function getValidRGBA(): array
195223
{
196224
return [
225+
['rgba( 255, 255, 255, 0.3 )'], ['rgba(255, 255, 255, 0.3)'], ['rgba(255, 255, 255, .3)'],
197226
['rgba(255, 255, 255, 0.3)'], ['rgba(0, 0, 0, 0.3)'], ['rgba(0, 0, 255, 0.3)'], ['rgba(255, 0, 0, 0.3)'], ['rgba(122, 122, 122, 0.3)'], ['rgba(66, 66, 66, 0.3)'],
198227
['rgba(255,255,255,0.3)'], ['rgba(0,0,0,0.3)'], ['rgba(0,0,255,0.3)'], ['rgba(255,0,0,0.3)'], ['rgba(122,122,122,0.3)'], ['rgba(66,66,66,0.3)'],
199228
];
@@ -211,6 +240,7 @@ public function testValidHSL($cssColor)
211240
public function getValidHSL(): array
212241
{
213242
return [
243+
['hsl(0, 0%, 20%)'], ['hsl( 0, 0%, 20% )'],
214244
['hsl(0, 0%, 20%)'], ['hsl(0, 100%, 50%)'], ['hsl(147, 50%, 47%)'], ['hsl(46, 100%, 0%)'],
215245
['hsl(0,0%,20%)'], ['hsl(0,100%,50%)'], ['hsl(147,50%,47%)'], ['hsl(46,100%,0%)'],
216246
];
@@ -228,6 +258,7 @@ public function testValidHSLA($cssColor)
228258
public function getValidHSLA(): array
229259
{
230260
return [
261+
['hsla( 0, 0%, 20%, 0.4 )'], ['hsla(0, 0%, 20%, 0.4)'], ['hsla(0, 0%, 20%, .4)'],
231262
['hsla(0, 0%, 20%, 0.4)'], ['hsla(0, 100%, 50%, 0.4)'], ['hsla(147, 50%, 47%, 0.4)'], ['hsla(46, 100%, 0%, 0.4)'],
232263
['hsla(0,0%,20%,0.4)'], ['hsla(0,100%,50%,0.4)'], ['hsla(147,50%,47%,0.4)'], ['hsla(46,100%,0%,0.4)'],
233264
];
@@ -313,7 +344,7 @@ public function testInvalidRGB($cssColor)
313344

314345
public function getInvalidRGB(): array
315346
{
316-
return [['rgb(999,999,999)'], ['rgb(-99,-99,-99)'], ['rgb(a,b,c)']];
347+
return [['rgb(999,999,999)'], ['rgb(-99,-99,-99)'], ['rgb(a,b,c)'], ['rgb(99 99, 9 99, 99 9)']];
317348
}
318349

319350
/**
@@ -336,7 +367,7 @@ public function testInvalidRGBA($cssColor)
336367

337368
public function getInvalidRGBA(): array
338369
{
339-
return [['rgba(999,999,999,999)'], ['rgba(-99,-99,-99,-99)'], ['rgba(a,b,c,d)']];
370+
return [['rgba(999,999,999,999)'], ['rgba(-99,-99,-99,-99)'], ['rgba(a,b,c,d)'], ['rgba(99 99, 9 99, 99 9, . 9)']];
340371
}
341372

342373
/**
@@ -359,7 +390,7 @@ public function testInvalidHSL($cssColor)
359390

360391
public function getInvalidHSL(): array
361392
{
362-
return [['hsl(1000, 1000%, 20000%)'], ['hsl(-100, -10%, -2%)'], ['hsl(a, b, c)'], ['hsl(a, b%, c%)']];
393+
return [['hsl(1000, 1000%, 20000%)'], ['hsl(-100, -10%, -2%)'], ['hsl(a, b, c)'], ['hsl(a, b%, c%)'], ['hsl( 99 99% , 9 99% , 99 9%)']];
363394
}
364395

365396
/**
@@ -382,7 +413,7 @@ public function testInvalidHSLA($cssColor)
382413

383414
public function getInvalidHSLA(): array
384415
{
385-
return [['hsla(1000, 1000%, 20000%, 999)'], ['hsla(-100, -10%, -2%, 999)'], ['hsla(a, b, c, d)'], ['hsla(a, b%, c%, d)']];
416+
return [['hsla(1000, 1000%, 20000%, 999)'], ['hsla(-100, -10%, -2%, 999)'], ['hsla(a, b, c, d)'], ['hsla(a, b%, c%, d)'], ['hsla( 9 99% , 99 9% , 9 %']];
386417
}
387418

388419
public function testUnknownFormatsOnValidateTriggerException()

0 commit comments

Comments
 (0)