Upgrade 1-11.38

This commit is contained in:
xesmyd
2026-03-30 14:10:30 +02:00
parent f2a7e6d1fc
commit ac648ef29d
24665 changed files with 69682 additions and 2205004 deletions
+14 -5
View File
@@ -11,6 +11,7 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
@@ -24,6 +25,7 @@ abstract class AbstractComparison extends Constraint
{
public $message;
public $value;
public $propertyPath;
/**
* {@inheritdoc}
@@ -34,11 +36,18 @@ abstract class AbstractComparison extends Constraint
$options = array();
}
if (is_array($options) && !isset($options['value'])) {
throw new ConstraintDefinitionException(sprintf(
'The %s constraint requires the "value" option to be set.',
get_class($this)
));
if (\is_array($options)) {
if (!isset($options['value']) && !isset($options['propertyPath'])) {
throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires either the "value" or "propertyPath" option to be set.', \get_class($this)));
}
if (isset($options['value']) && isset($options['propertyPath'])) {
throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires only one of the "value" or "propertyPath" options to be set, not both.', \get_class($this)));
}
if (isset($options['propertyPath']) && !class_exists(PropertyAccess::class)) {
throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires the Symfony PropertyAccess component to use the "propertyPath" option.', \get_class($this)));
}
}
parent::__construct($options);
@@ -11,8 +11,12 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
@@ -23,6 +27,13 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
*/
abstract class AbstractComparisonValidator extends ConstraintValidator
{
private $propertyAccessor;
public function __construct(PropertyAccessor $propertyAccessor = null)
{
$this->propertyAccessor = $propertyAccessor;
}
/**
* {@inheritdoc}
*/
@@ -36,13 +47,25 @@ abstract class AbstractComparisonValidator extends ConstraintValidator
return;
}
$comparedValue = $constraint->value;
if ($path = $constraint->propertyPath) {
if (null === $object = $this->context->getObject()) {
return;
}
try {
$comparedValue = $this->getPropertyAccessor()->getValue($object, $path);
} catch (NoSuchPropertyException $e) {
throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: %s', $path, \get_class($constraint), $e->getMessage()), 0, $e);
}
} else {
$comparedValue = $constraint->value;
}
// Convert strings to DateTimes if comparing another DateTime
// This allows to compare with any date/time value supported by
// the DateTime constructor:
// http://php.net/manual/en/datetime.formats.php
if (is_string($comparedValue)) {
if (\is_string($comparedValue)) {
if ($value instanceof \DateTimeImmutable) {
// If $value is immutable, convert the compared value to a
// DateTimeImmutable too
@@ -63,6 +86,15 @@ abstract class AbstractComparisonValidator extends ConstraintValidator
}
}
private function getPropertyAccessor()
{
if (null === $this->propertyAccessor) {
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}
return $this->propertyAccessor;
}
/**
* Compares the two given values to find if their relationship is valid.
*
+1 -1
View File
@@ -33,7 +33,7 @@ class AllValidator extends ConstraintValidator
return;
}
if (!is_array($value) && !$value instanceof \Traversable) {
if (!\is_array($value) && !$value instanceof \Traversable) {
throw new UnexpectedTypeException($value, 'array or Traversable');
}
+1 -1
View File
@@ -33,7 +33,7 @@ class BicValidator extends ConstraintValidator
$canonicalize = str_replace(' ', '', $value);
// the bic must be either 8 or 11 characters long
if (!in_array(strlen($canonicalize), array(8, 11))) {
if (!\in_array(\strlen($canonicalize), array(8, 11))) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Bic::INVALID_LENGTH_ERROR)
+2 -2
View File
@@ -32,11 +32,11 @@ class Callback extends Constraint
public function __construct($options = null)
{
// Invocation through annotations with an array parameter only
if (is_array($options) && 1 === count($options) && isset($options['value'])) {
if (\is_array($options) && 1 === \count($options) && isset($options['value'])) {
$options = $options['value'];
}
if (is_array($options) && !isset($options['callback']) && !isset($options['groups']) && !isset($options['payload'])) {
if (\is_array($options) && !isset($options['callback']) && !isset($options['groups']) && !isset($options['payload'])) {
$options = array('callback' => $options);
}
+6 -6
View File
@@ -35,18 +35,18 @@ class CallbackValidator extends ConstraintValidator
$method = $constraint->callback;
if ($method instanceof \Closure) {
$method($object, $this->context, $constraint->payload);
} elseif (is_array($method)) {
if (!is_callable($method)) {
if (isset($method[0]) && is_object($method[0])) {
$method[0] = get_class($method[0]);
} elseif (\is_array($method)) {
if (!\is_callable($method)) {
if (isset($method[0]) && \is_object($method[0])) {
$method[0] = \get_class($method[0]);
}
throw new ConstraintDefinitionException(sprintf('%s targeted by Callback constraint is not a valid callable', json_encode($method)));
}
call_user_func($method, $object, $this->context, $constraint->payload);
\call_user_func($method, $object, $this->context, $constraint->payload);
} elseif (null !== $object) {
if (!method_exists($object, $method)) {
throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist in class %s', $method, get_class($object)));
throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist in class %s', $method, \get_class($object)));
}
$reflMethod = new \ReflectionMethod($object, $method);
+1 -1
View File
@@ -34,7 +34,7 @@ class Choice extends Constraint
public $choices;
public $callback;
public $multiple = false;
public $strict = false;
public $strict = true;
public $min;
public $max;
public $message = 'The value you selected is not a valid choice.';
+11 -11
View File
@@ -34,7 +34,7 @@ class ChoiceValidator extends ConstraintValidator
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
}
if (!is_array($constraint->choices) && !$constraint->callback) {
if (!\is_array($constraint->choices) && !$constraint->callback) {
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
}
@@ -42,29 +42,29 @@ class ChoiceValidator extends ConstraintValidator
return;
}
if ($constraint->multiple && !is_array($value)) {
if ($constraint->multiple && !\is_array($value)) {
throw new UnexpectedTypeException($value, 'array');
}
if ($constraint->callback) {
if (!is_callable($choices = array($this->context->getObject(), $constraint->callback))
&& !is_callable($choices = array($this->context->getClassName(), $constraint->callback))
&& !is_callable($choices = $constraint->callback)
if (!\is_callable($choices = array($this->context->getObject(), $constraint->callback))
&& !\is_callable($choices = array($this->context->getClassName(), $constraint->callback))
&& !\is_callable($choices = $constraint->callback)
) {
throw new ConstraintDefinitionException('The Choice constraint expects a valid callback');
}
$choices = call_user_func($choices);
$choices = \call_user_func($choices);
} else {
$choices = $constraint->choices;
}
if (false === $constraint->strict) {
@trigger_error('Setting the strict option of the Choice constraint to false is deprecated since Symfony 3.2 and will be removed in 4.0.', E_USER_DEPRECATED);
if (true !== $constraint->strict) {
throw new \RuntimeException('The "strict" option of the Choice constraint should not be used.');
}
if ($constraint->multiple) {
foreach ($value as $_value) {
if (!in_array($_value, $choices, $constraint->strict)) {
if (!\in_array($_value, $choices, true)) {
$this->context->buildViolation($constraint->multipleMessage)
->setParameter('{{ value }}', $this->formatValue($_value))
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
@@ -75,7 +75,7 @@ class ChoiceValidator extends ConstraintValidator
}
}
$count = count($value);
$count = \count($value);
if (null !== $constraint->min && $count < $constraint->min) {
$this->context->buildViolation($constraint->minMessage)
@@ -96,7 +96,7 @@ class ChoiceValidator extends ConstraintValidator
return;
}
} elseif (!in_array($value, $choices, $constraint->strict)) {
} elseif (!\in_array($value, $choices, true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
+3 -3
View File
@@ -41,7 +41,7 @@ class Collection extends Composite
public function __construct($options = null)
{
// no known options set? $options is the fields array
if (is_array($options)
if (\is_array($options)
&& !array_intersect(array_keys($options), array('groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'))) {
$options = array('fields' => $options);
}
@@ -56,14 +56,14 @@ class Collection extends Composite
{
parent::initializeNestedConstraints();
if (!is_array($this->fields)) {
if (!\is_array($this->fields)) {
throw new ConstraintDefinitionException(sprintf('The option "fields" is expected to be an array in constraint %s', __CLASS__));
}
foreach ($this->fields as $fieldName => $field) {
// the XmlFileLoader and YamlFileLoader pass the field Optional
// and Required constraint as an array with exactly one element
if (is_array($field) && 1 == count($field)) {
if (\is_array($field) && 1 == \count($field)) {
$this->fields[$fieldName] = $field = $field[0];
}
@@ -33,7 +33,7 @@ class CollectionValidator extends ConstraintValidator
return;
}
if (!is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
if (!\is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
throw new UnexpectedTypeException($value, 'array or Traversable and ArrayAccess');
}
@@ -50,11 +50,11 @@ class CollectionValidator extends ConstraintValidator
foreach ($constraint->fields as $field => $fieldConstraint) {
// bug fix issue #2779
$existsInArray = is_array($value) && array_key_exists($field, $value);
$existsInArray = \is_array($value) && array_key_exists($field, $value);
$existsInArrayAccess = $value instanceof \ArrayAccess && $value->offsetExists($field);
if ($existsInArray || $existsInArrayAccess) {
if (count($fieldConstraint->constraints) > 0) {
if (\count($fieldConstraint->constraints) > 0) {
$context->getValidator()
->inContext($context)
->atPath('['.$field.']')
+8 -8
View File
@@ -61,21 +61,21 @@ abstract class Composite extends Constraint
$compositeOption = $this->getCompositeOption();
$nestedConstraints = $this->$compositeOption;
if (!is_array($nestedConstraints)) {
if (!\is_array($nestedConstraints)) {
$nestedConstraints = array($nestedConstraints);
}
foreach ($nestedConstraints as $constraint) {
if (!$constraint instanceof Constraint) {
if (is_object($constraint)) {
$constraint = get_class($constraint);
if (\is_object($constraint)) {
$constraint = \get_class($constraint);
}
throw new ConstraintDefinitionException(sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, get_class($this)));
throw new ConstraintDefinitionException(sprintf('The value %s is not an instance of Constraint in constraint %s', $constraint, \get_class($this)));
}
if ($constraint instanceof Valid) {
throw new ConstraintDefinitionException(sprintf('The constraint Valid cannot be nested inside constraint %s. You can only declare the Valid constraint directly on a field or method.', get_class($this)));
throw new ConstraintDefinitionException(sprintf('The constraint Valid cannot be nested inside constraint %s. You can only declare the Valid constraint directly on a field or method.', \get_class($this)));
}
}
@@ -98,13 +98,13 @@ abstract class Composite extends Constraint
if (property_exists($constraint, 'groups')) {
$excessGroups = array_diff($constraint->groups, $this->groups);
if (count($excessGroups) > 0) {
if (\count($excessGroups) > 0) {
throw new ConstraintDefinitionException(sprintf(
'The group(s) "%s" passed to the constraint %s '.
'should also be passed to its containing constraint %s',
implode('", "', $excessGroups),
get_class($constraint),
get_class($this)
\get_class($constraint),
\get_class($this)
));
}
} else {
+1 -1
View File
@@ -38,7 +38,7 @@ class Count extends Constraint
public function __construct($options = null)
{
if (null !== $options && !is_array($options)) {
if (null !== $options && !\is_array($options)) {
$options = array(
'min' => $options,
'max' => $options,
+2 -2
View File
@@ -29,11 +29,11 @@ class CountValidator extends ConstraintValidator
return;
}
if (!is_array($value) && !$value instanceof \Countable) {
if (!\is_array($value) && !$value instanceof \Countable) {
throw new UnexpectedTypeException($value, 'array or \Countable');
}
$count = count($value);
$count = \count($value);
if (null !== $constraint->max && $count > $constraint->max) {
$this->context->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
+1 -1
View File
@@ -36,7 +36,7 @@ class CountryValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
+1 -1
View File
@@ -37,7 +37,7 @@ class CurrencyValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
+1 -6
View File
@@ -20,11 +20,6 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
*/
class DateTimeValidator extends DateValidator
{
/**
* @deprecated since version 3.1, to be removed in 4.0.
*/
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/';
/**
* {@inheritdoc}
*/
@@ -38,7 +33,7 @@ class DateTimeValidator extends DateValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
+1 -1
View File
@@ -51,7 +51,7 @@ class DateValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
+4 -15
View File
@@ -25,10 +25,7 @@ class EmailValidator extends ConstraintValidator
{
private $isStrict;
/**
* @param bool $strict
*/
public function __construct($strict = false)
public function __construct(bool $strict = false)
{
$this->isStrict = $strict;
}
@@ -46,7 +43,7 @@ class EmailValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
@@ -111,24 +108,16 @@ class EmailValidator extends ConstraintValidator
/**
* Check DNS Records for MX type.
*
* @param string $host Host
*
* @return bool
*/
private function checkMX($host)
private function checkMX(string $host): bool
{
return '' !== $host && checkdnsrr($host, 'MX');
}
/**
* Check if one of MX, A or AAAA DNS RR exists.
*
* @param string $host Host
*
* @return bool
*/
private function checkHost($host)
private function checkHost(string $host): bool
{
return '' !== $host && ($this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA')));
}
+4 -1
View File
@@ -59,6 +59,9 @@ class File extends Constraint
protected $maxSize;
/**
* {@inheritdoc}
*/
public function __construct($options = null)
{
parent::__construct($options);
@@ -110,7 +113,7 @@ class File extends Constraint
$this->binaryFormat = null === $this->binaryFormat ? false : $this->binaryFormat;
} elseif (preg_match('/^(\d++)('.implode('|', array_keys($factors)).')$/i', $maxSize, $matches)) {
$this->maxSize = $matches[1] * $factors[$unit = strtolower($matches[2])];
$this->binaryFormat = null === $this->binaryFormat ? 2 === strlen($unit) : $this->binaryFormat;
$this->binaryFormat = null === $this->binaryFormat ? 2 === \strlen($unit) : $this->binaryFormat;
} else {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $this->maxSize));
}
+2 -2
View File
@@ -113,7 +113,7 @@ class FileValidator extends ConstraintValidator
}
}
if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !$value instanceof FileObject && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
@@ -196,7 +196,7 @@ class FileValidator extends ConstraintValidator
private static function moreDecimalsThan($double, $numberOfDecimals)
{
return strlen((string) $double) > strlen(round($double, $numberOfDecimals));
return \strlen((string) $double) > \strlen(round($double, $numberOfDecimals));
}
/**
+2 -2
View File
@@ -148,7 +148,7 @@ class IbanValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
@@ -231,7 +231,7 @@ class IbanValidator extends ConstraintValidator
foreach ($chars as $char) {
// Convert uppercase characters to ordinals, starting with 10 for "A"
if (ctype_upper($char)) {
$bigInt .= (ord($char) - 55);
$bigInt .= (\ord($char) - 55);
continue;
}
+8
View File
@@ -25,6 +25,8 @@ class Image extends File
const TOO_NARROW_ERROR = '9afbd561-4f90-4a27-be62-1780fc43604a';
const TOO_HIGH_ERROR = '7efae81c-4877-47ba-aa65-d01ccb0d4645';
const TOO_LOW_ERROR = 'aef0cb6a-c07f-4894-bc08-1781420d7b4c';
const TOO_FEW_PIXEL_ERROR = '1b06b97d-ae48-474e-978f-038a74854c43';
const TOO_MANY_PIXEL_ERROR = 'ee0804e8-44db-4eac-9775-be91aaf72ce1';
const RATIO_TOO_BIG_ERROR = '70cafca6-168f-41c9-8c8c-4e47a52be643';
const RATIO_TOO_SMALL_ERROR = '59b8c6ef-bcf2-4ceb-afff-4642ed92f12e';
const SQUARE_NOT_ALLOWED_ERROR = '5d41425b-facb-47f7-a55a-de9fbe45cb46';
@@ -45,6 +47,8 @@ class Image extends File
self::TOO_NARROW_ERROR => 'TOO_NARROW_ERROR',
self::TOO_HIGH_ERROR => 'TOO_HIGH_ERROR',
self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
self::TOO_FEW_PIXEL_ERROR => 'TOO_FEW_PIXEL_ERROR',
self::TOO_MANY_PIXEL_ERROR => 'TOO_MANY_PIXEL_ERROR',
self::RATIO_TOO_BIG_ERROR => 'RATIO_TOO_BIG_ERROR',
self::RATIO_TOO_SMALL_ERROR => 'RATIO_TOO_SMALL_ERROR',
self::SQUARE_NOT_ALLOWED_ERROR => 'SQUARE_NOT_ALLOWED_ERROR',
@@ -60,6 +64,8 @@ class Image extends File
public $minHeight;
public $maxRatio;
public $minRatio;
public $minPixels;
public $maxPixels;
public $allowSquare = true;
public $allowLandscape = true;
public $allowPortrait = true;
@@ -72,6 +78,8 @@ class Image extends File
public $minWidthMessage = 'The image width is too small ({{ width }}px). Minimum width expected is {{ min_width }}px.';
public $maxHeightMessage = 'The image height is too big ({{ height }}px). Allowed maximum height is {{ max_height }}px.';
public $minHeightMessage = 'The image height is too small ({{ height }}px). Minimum height expected is {{ min_height }}px.';
public $minPixelsMessage = 'The image has too few pixels ({{ pixels }} pixels). Minimum amount expected is {{ min_pixels }} pixels.';
public $maxPixelsMessage = 'The image has too many pixels ({{ pixels }} pixels). Maximum amount expected is {{ max_pixels }} pixels.';
public $maxRatioMessage = 'The image ratio is too big ({{ ratio }}). Allowed maximum ratio is {{ max_ratio }}.';
public $minRatioMessage = 'The image ratio is too small ({{ ratio }}). Minimum ratio expected is {{ min_ratio }}.';
public $allowSquareMessage = 'The image is square ({{ width }}x{{ height }}px). Square images are not allowed.';
+40 -5
View File
@@ -34,11 +34,11 @@ class ImageValidator extends FileValidator
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Image');
}
$violations = count($this->context->getViolations());
$violations = \count($this->context->getViolations());
parent::validate($value, $constraint);
$failed = count($this->context->getViolations()) !== $violations;
$failed = \count($this->context->getViolations()) !== $violations;
if ($failed || null === $value || '' === $value) {
return;
@@ -46,6 +46,7 @@ class ImageValidator extends FileValidator
if (null === $constraint->minWidth && null === $constraint->maxWidth
&& null === $constraint->minHeight && null === $constraint->maxHeight
&& null === $constraint->minPixels && null === $constraint->maxPixels
&& null === $constraint->minRatio && null === $constraint->maxRatio
&& $constraint->allowSquare && $constraint->allowLandscape && $constraint->allowPortrait
&& !$constraint->detectCorrupted) {
@@ -67,7 +68,7 @@ class ImageValidator extends FileValidator
if ($constraint->minWidth) {
if (!ctype_digit((string) $constraint->minWidth)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth));
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width.', $constraint->minWidth));
}
if ($width < $constraint->minWidth) {
@@ -83,7 +84,7 @@ class ImageValidator extends FileValidator
if ($constraint->maxWidth) {
if (!ctype_digit((string) $constraint->maxWidth)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth));
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width.', $constraint->maxWidth));
}
if ($width > $constraint->maxWidth) {
@@ -127,6 +128,40 @@ class ImageValidator extends FileValidator
}
}
$pixels = $width * $height;
if (null !== $constraint->minPixels) {
if (!ctype_digit((string) $constraint->minPixels)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum amount of pixels', $constraint->minPixels));
}
if ($pixels < $constraint->minPixels) {
$this->context->buildViolation($constraint->minPixelsMessage)
->setParameter('{{ pixels }}', $pixels)
->setParameter('{{ min_pixels }}', $constraint->minPixels)
->setParameter('{{ height }}', $height)
->setParameter('{{ width }}', $width)
->setCode(Image::TOO_FEW_PIXEL_ERROR)
->addViolation();
}
}
if (null !== $constraint->maxPixels) {
if (!ctype_digit((string) $constraint->maxPixels)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum amount of pixels', $constraint->maxPixels));
}
if ($pixels > $constraint->maxPixels) {
$this->context->buildViolation($constraint->maxPixelsMessage)
->setParameter('{{ pixels }}', $pixels)
->setParameter('{{ max_pixels }}', $constraint->maxPixels)
->setParameter('{{ height }}', $height)
->setParameter('{{ width }}', $width)
->setCode(Image::TOO_MANY_PIXEL_ERROR)
->addViolation();
}
}
$ratio = round($width / $height, 2);
if (null !== $constraint->minRatio) {
@@ -182,7 +217,7 @@ class ImageValidator extends FileValidator
}
if ($constraint->detectCorrupted) {
if (!function_exists('imagecreatefromstring')) {
if (!\function_exists('imagecreatefromstring')) {
throw new RuntimeException('Corrupted images detection requires installed and enabled GD extension');
}
+1 -1
View File
@@ -79,7 +79,7 @@ class Ip extends Constraint
{
parent::__construct($options);
if (!in_array($this->version, self::$versions)) {
if (!\in_array($this->version, self::$versions)) {
throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s"', implode('", "', self::$versions)));
}
}
+1 -1
View File
@@ -36,7 +36,7 @@ class IpValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
+2 -2
View File
@@ -39,7 +39,7 @@ class IsbnValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
@@ -145,7 +145,7 @@ class IsbnValidator extends ConstraintValidator
return Isbn::INVALID_CHARACTERS_ERROR;
}
$length = strlen($isbn);
$length = \strlen($isbn);
if ($length < 13) {
return Isbn::TOO_SHORT_ERROR;
+2 -2
View File
@@ -38,7 +38,7 @@ class IssnValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
@@ -59,7 +59,7 @@ class IssnValidator extends ConstraintValidator
return;
}
$length = strlen($canonical);
$length = \strlen($canonical);
if ($length < 8) {
$this->context->buildViolation($constraint->message)
+1 -1
View File
@@ -36,7 +36,7 @@ class LanguageValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
+1 -1
View File
@@ -42,7 +42,7 @@ class Length extends Constraint
public function __construct($options = null)
{
if (null !== $options && !is_array($options)) {
if (null !== $options && !\is_array($options)) {
$options = array(
'min' => $options,
'max' => $options,
+1 -1
View File
@@ -33,7 +33,7 @@ class LengthValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
+2 -2
View File
@@ -36,7 +36,7 @@ class LocaleValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
@@ -44,7 +44,7 @@ class LocaleValidator extends ConstraintValidator
$locales = Intl::getLocaleBundle()->getLocaleNames();
$aliases = Intl::getLocaleBundle()->getAliases();
if (!isset($locales[$value]) && !in_array($value, $aliases)) {
if (!isset($locales[$value]) && !\in_array($value, $aliases)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Locale::NO_SUCH_LOCALE_ERROR)
+2 -2
View File
@@ -49,7 +49,7 @@ class LuhnValidator extends ConstraintValidator
// Work with strings only, because long numbers are represented as floats
// internally and don't work with strlen()
if (!is_string($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
@@ -65,7 +65,7 @@ class LuhnValidator extends ConstraintValidator
}
$checkSum = 0;
$length = strlen($value);
$length = \strlen($value);
// Starting with the last digit and walking left, add every second
// digit to the check sum
+2 -2
View File
@@ -50,11 +50,11 @@ class RangeValidator extends ConstraintValidator
// the DateTime constructor:
// http://php.net/manual/en/datetime.formats.php
if ($value instanceof \DateTimeInterface) {
if (is_string($min)) {
if (\is_string($min)) {
$min = new \DateTime($min);
}
if (is_string($max)) {
if (\is_string($max)) {
$max = new \DateTime($max);
}
}
+2 -2
View File
@@ -70,7 +70,7 @@ class Regex extends Constraint
}
// Quit if delimiters not at very beginning/end (e.g. when options are passed)
if ($this->pattern[0] !== $this->pattern[strlen($this->pattern) - 1]) {
if ($this->pattern[0] !== $this->pattern[\strlen($this->pattern) - 1]) {
return;
}
@@ -95,7 +95,7 @@ class Regex extends Constraint
$pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*'.$pattern;
// Trim trailing $, otherwise append .*
$pattern = '$' === $pattern[strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern.'.*';
$pattern = '$' === $pattern[\strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern.'.*';
return $pattern;
}
+1 -1
View File
@@ -36,7 +36,7 @@ class RegexValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
+1 -1
View File
@@ -51,7 +51,7 @@ class TimeValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
+1 -1
View File
@@ -25,7 +25,7 @@ class Traverse extends Constraint
public function __construct($options = null)
{
if (is_array($options) && array_key_exists('groups', $options)) {
if (\is_array($options) && array_key_exists('groups', $options)) {
throw new ConstraintDefinitionException(sprintf(
'The option "groups" is not supported by the constraint %s',
__CLASS__
+2 -2
View File
@@ -38,9 +38,9 @@ class TypeValidator extends ConstraintValidator
$isFunction = 'is_'.$type;
$ctypeFunction = 'ctype_'.$type;
if (function_exists($isFunction) && $isFunction($value)) {
if (\function_exists($isFunction) && $isFunction($value)) {
return;
} elseif (function_exists($ctypeFunction) && $ctypeFunction($value)) {
} elseif (\function_exists($ctypeFunction) && $ctypeFunction($value)) {
return;
} elseif ($value instanceof $constraint->type) {
return;
+15 -1
View File
@@ -21,6 +21,20 @@ use Symfony\Component\Validator\Constraint;
*/
class Url extends Constraint
{
const CHECK_DNS_TYPE_ANY = 'ANY';
const CHECK_DNS_TYPE_NONE = false;
const CHECK_DNS_TYPE_A = 'A';
const CHECK_DNS_TYPE_A6 = 'A6';
const CHECK_DNS_TYPE_AAAA = 'AAAA';
const CHECK_DNS_TYPE_CNAME = 'CNAME';
const CHECK_DNS_TYPE_MX = 'MX';
const CHECK_DNS_TYPE_NAPTR = 'NAPTR';
const CHECK_DNS_TYPE_NS = 'NS';
const CHECK_DNS_TYPE_PTR = 'PTR';
const CHECK_DNS_TYPE_SOA = 'SOA';
const CHECK_DNS_TYPE_SRV = 'SRV';
const CHECK_DNS_TYPE_TXT = 'TXT';
const INVALID_URL_ERROR = '57c2f299-1154-4870-89bb-ef3b1f5ad229';
protected static $errorNames = array(
@@ -30,5 +44,5 @@ class Url extends Constraint
public $message = 'This value is not a valid URL.';
public $dnsMessage = 'The host could not be resolved.';
public $protocols = array('http', 'https');
public $checkDNS = false;
public $checkDNS = self::CHECK_DNS_TYPE_NONE;
}
+20 -2
View File
@@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\InvalidOptionsException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
@@ -51,7 +52,7 @@ class UrlValidator extends ConstraintValidator
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
@@ -72,9 +73,26 @@ class UrlValidator extends ConstraintValidator
}
if ($constraint->checkDNS) {
if (!\in_array($constraint->checkDNS, array(
Url::CHECK_DNS_TYPE_ANY,
Url::CHECK_DNS_TYPE_A,
Url::CHECK_DNS_TYPE_A6,
Url::CHECK_DNS_TYPE_AAAA,
Url::CHECK_DNS_TYPE_CNAME,
Url::CHECK_DNS_TYPE_MX,
Url::CHECK_DNS_TYPE_NAPTR,
Url::CHECK_DNS_TYPE_NS,
Url::CHECK_DNS_TYPE_PTR,
Url::CHECK_DNS_TYPE_SOA,
Url::CHECK_DNS_TYPE_SRV,
Url::CHECK_DNS_TYPE_TXT,
), true)) {
throw new InvalidOptionsException(sprintf('Invalid value for option "checkDNS" in constraint %s', \get_class($constraint)), array('checkDNS'));
}
$host = parse_url($value, PHP_URL_HOST);
if (!is_string($host) || !checkdnsrr($host, 'ANY')) {
if (!\is_string($host) || !checkdnsrr($host, $constraint->checkDNS)) {
$this->context->buildViolation($constraint->dnsMessage)
->setParameter('{{ value }}', $this->formatValue($host))
->setCode(Url::INVALID_URL_ERROR)
+2 -2
View File
@@ -74,7 +74,7 @@ class UuidValidator extends ConstraintValidator
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Uuid');
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
@@ -235,7 +235,7 @@ class UuidValidator extends ConstraintValidator
}
// Check version
if (!in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) {
if (!\in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Uuid::INVALID_VERSION_ERROR)
+15 -8
View File
@@ -12,7 +12,6 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
/**
* @Annotation
@@ -24,15 +23,23 @@ class Valid extends Constraint
{
public $traverse = true;
public function __construct($options = null)
public function __get($option)
{
if (is_array($options) && array_key_exists('groups', $options)) {
throw new ConstraintDefinitionException(sprintf(
'The option "groups" is not supported by the constraint %s',
__CLASS__
));
if ('groups' === $option) {
// when this is reached, no groups have been configured
return null;
}
parent::__construct($options);
return parent::__get($option);
}
/**
* {@inheritdoc}
*/
public function addImplicitGroupName($group)
{
if (null !== $this->groups) {
parent::addImplicitGroupName($group);
}
}
}