Skip to content

[Validator] Add RgbColor constraint and validator #35736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/Symfony/Component/Validator/Constraints/RgbColor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\InvalidArgumentException;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
*/
class RgbColor extends Constraint
{
public const INVALID_FORMAT_ERROR = '5720b8cb-fb2a-430e-94e9-2d33b4cd1fa9';

protected static $errorNames = [
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
];

public $message = 'This is not a valid RGB color.';
public $allowAlpha = true;
public $alphaPrecision = 2;
public $lowerCaseOnly = false;

public function __construct($options = null)
{
parent::__construct($options);

if (!\is_int($this->alphaPrecision) || 1 > $this->alphaPrecision) {
throw new InvalidArgumentException(sprintf('The "alphaPrecision" option must be a positive integer ("%s" given)', $this->alphaPrecision));
}
}
}
69 changes: 69 additions & 0 deletions src/Symfony/Component/Validator/Constraints/RgbColorValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
*/
class RgbColorValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof RgbColor) {
throw new UnexpectedTypeException($constraint, RgbColor::class);
}

if (null === $value || '' === $value) {
return;
}

if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;

$pattern = sprintf('/^rgb%s\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})%s\)$/%s', $constraint->allowAlpha ? 'a?' : '', $constraint->allowAlpha ? '(,\s*[0-1]{0,1}(?:\.\d{1,'.$constraint->alphaPrecision.'})?)?' : '', $constraint->lowerCaseOnly ? '' : 'i');

if (!preg_match($pattern, $value, $matches) || !$this->areRangesValid($matches)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(RgbColor::INVALID_FORMAT_ERROR)
->addViolation();
}
}

private function areRangesValid(array $matches): bool
{
for ($i = 1; $i < 4; ++$i) {
if (0 > $matches[$i] || 255 < $matches[$i]) {
return false;
}
}

if ($alpha = trim($matches[4] ?? null, ',')) {
if (0 > $alpha || 1 < $alpha) {
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@
<source>This value is not a valid hostname.</source>
<target>This value is not a valid hostname.</target>
</trans-unit>
<trans-unit id="96">
<source>This is not a valid RGB color.</source>
<target>This is not a valid RGB color.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@
<source>This value should be between {{ min }} and {{ max }}.</source>
<target>Ta wartość powinna być pomiędzy {{ min }} a {{ max }}.</target>
</trans-unit>
<trans-unit id="96">
<source>This is not a valid RGB color.</source>
<target>To nie jest poprawny kolor RGB.</target>
</trans-unit>
</body>
</file>
</xliff>
Loading