[Validator] Reverted the changes done to the Size constraint in 3a5e84f4a7
This commit is contained in:
parent
d84b689529
commit
d661837ec0
@ -158,14 +158,6 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
|||||||
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
|
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
|
||||||
|
|
||||||
case 'Symfony\Component\Validator\Constraints\Size':
|
case 'Symfony\Component\Validator\Constraints\Size':
|
||||||
switch ($constraint->type) {
|
|
||||||
case 'string':
|
|
||||||
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
|
|
||||||
case 'collection':
|
|
||||||
return new TypeGuess('collection', array(), Guess::MEDIUM_CONFIDENCE);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'Symfony\Component\Validator\Constraints\Min':
|
case 'Symfony\Component\Validator\Constraints\Min':
|
||||||
case 'Symfony\Component\Validator\Constraints\Max':
|
case 'Symfony\Component\Validator\Constraints\Max':
|
||||||
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
|
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
|
||||||
@ -211,12 +203,6 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
|||||||
case 'Symfony\Component\Validator\Constraints\MaxLength':
|
case 'Symfony\Component\Validator\Constraints\MaxLength':
|
||||||
return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
|
return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);
|
||||||
|
|
||||||
case 'Symfony\Component\Validator\Constraints\Size':
|
|
||||||
if ('string' === $constraint->type && null !== $constraint->max) {
|
|
||||||
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'Symfony\Component\Validator\Constraints\Type':
|
case 'Symfony\Component\Validator\Constraints\Type':
|
||||||
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
|
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
|
||||||
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
|
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
|
||||||
@ -225,6 +211,9 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
|||||||
|
|
||||||
case 'Symfony\Component\Validator\Constraints\Max':
|
case 'Symfony\Component\Validator\Constraints\Max':
|
||||||
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
|
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);
|
||||||
|
|
||||||
|
case 'Symfony\Component\Validator\Constraints\Size':
|
||||||
|
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,25 +230,6 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
|||||||
case 'Symfony\Component\Validator\Constraints\MinLength':
|
case 'Symfony\Component\Validator\Constraints\MinLength':
|
||||||
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->limit), Guess::LOW_CONFIDENCE);
|
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->limit), Guess::LOW_CONFIDENCE);
|
||||||
|
|
||||||
case 'Symfony\Component\Validator\Constraints\Size':
|
|
||||||
if ('string' !== $constraint->type) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($constraint->min === $constraint->max) {
|
|
||||||
return new ValueGuess(sprintf('.{%s}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (null === $constraint->min) {
|
|
||||||
return new ValueGuess(sprintf('.{0,%s}', (string) $constraint->max), Guess::LOW_CONFIDENCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (null === $constraint->max) {
|
|
||||||
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ValueGuess(sprintf('.{%s,%s}', (string) $constraint->min, (string) $constraint->max), Guess::LOW_CONFIDENCE);
|
|
||||||
|
|
||||||
case 'Symfony\Component\Validator\Constraints\Regex':
|
case 'Symfony\Component\Validator\Constraints\Regex':
|
||||||
$htmlPattern = $constraint->getHtmlPattern();
|
$htmlPattern = $constraint->getHtmlPattern();
|
||||||
|
|
||||||
@ -271,6 +241,9 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
|||||||
case 'Symfony\Component\Validator\Constraints\Min':
|
case 'Symfony\Component\Validator\Constraints\Min':
|
||||||
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE);
|
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE);
|
||||||
|
|
||||||
|
case 'Symfony\Component\Validator\Constraints\Size':
|
||||||
|
return new ValueGuess(sprintf('.{%s,%s}', strlen((string) $constraint->min), strlen((string) $constraint->max)), Guess::LOW_CONFIDENCE);
|
||||||
|
|
||||||
case 'Symfony\Component\Validator\Constraints\Type':
|
case 'Symfony\Component\Validator\Constraints\Type':
|
||||||
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
|
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
|
||||||
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
|
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
|
||||||
|
@ -5,7 +5,6 @@ CHANGELOG
|
|||||||
-----
|
-----
|
||||||
|
|
||||||
* added support for `ctype_*` assertions in `TypeValidator`
|
* added support for `ctype_*` assertions in `TypeValidator`
|
||||||
* added a Size validator for string & collections
|
|
||||||
* improved the ImageValidator with min width, max width, min height, and max height constraints
|
* improved the ImageValidator with min width, max width, min height, and max height constraints
|
||||||
* added support for MIME with wildcard in FileValidator
|
* added support for MIME with wildcard in FileValidator
|
||||||
* changed Collection validator to add "missing" and "extra" errors to
|
* changed Collection validator to add "missing" and "extra" errors to
|
||||||
|
@ -20,70 +20,17 @@ use Symfony\Component\Validator\Constraint;
|
|||||||
*/
|
*/
|
||||||
class Size extends Constraint
|
class Size extends Constraint
|
||||||
{
|
{
|
||||||
const TYPE_STRING = 'string';
|
public $minMessage = 'This value should be {{ limit }} or more';
|
||||||
const TYPE_COLLECTION = 'collection';
|
public $maxMessage = 'This value should be {{ limit }} or less';
|
||||||
|
public $invalidMessage = 'This value should be a valid number';
|
||||||
public $minMessage;
|
|
||||||
public $maxMessage;
|
|
||||||
public $exactMessage;
|
|
||||||
public $type;
|
|
||||||
public $min;
|
public $min;
|
||||||
public $max;
|
public $max;
|
||||||
public $charset = 'UTF-8';
|
|
||||||
|
|
||||||
private $stringMinMessage = 'This value is too short. It should have {{ limit }} characters or more.';
|
/**
|
||||||
private $stringMaxMessage = 'This value is too long. It should have {{ limit }} characters or less.';
|
* {@inheritDoc}
|
||||||
private $stringExactMessage = 'This value should have exactly {{ limit }} characters.';
|
*/
|
||||||
|
public function getRequiredOptions()
|
||||||
private $collectionMinMessage = 'This collection should contain {{ limit }} elements or more.';
|
|
||||||
private $collectionMaxMessage = 'This collection should contain {{ limit }} elements or less.';
|
|
||||||
private $collectionExactMessage = 'This collection should contain exactly {{ limit }} elements.';
|
|
||||||
|
|
||||||
public function getMinMessage($type)
|
|
||||||
{
|
{
|
||||||
if (null !== $this->minMessage) {
|
return array('min', 'max');
|
||||||
return $this->minMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($type) {
|
|
||||||
case static::TYPE_STRING:
|
|
||||||
return $this->stringMinMessage;
|
|
||||||
case static::TYPE_COLLECTION:
|
|
||||||
return $this->collectionMinMessage;
|
|
||||||
default:
|
|
||||||
throw new \InvalidArgumentException('Invalid type specified.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getMaxMessage($type)
|
|
||||||
{
|
|
||||||
if (null !== $this->maxMessage) {
|
|
||||||
return $this->maxMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($type) {
|
|
||||||
case static::TYPE_STRING:
|
|
||||||
return $this->stringMaxMessage;
|
|
||||||
case static::TYPE_COLLECTION:
|
|
||||||
return $this->collectionMaxMessage;
|
|
||||||
default:
|
|
||||||
throw new \InvalidArgumentException('Invalid type specified.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getExactMessage($type)
|
|
||||||
{
|
|
||||||
if (null !== $this->exactMessage) {
|
|
||||||
return $this->exactMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($type) {
|
|
||||||
case static::TYPE_STRING:
|
|
||||||
return $this->stringExactMessage;
|
|
||||||
case static::TYPE_COLLECTION:
|
|
||||||
return $this->collectionExactMessage;
|
|
||||||
default:
|
|
||||||
throw new \InvalidArgumentException('Invalid type specified.');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,151 +13,52 @@ namespace Symfony\Component\Validator\Constraints;
|
|||||||
|
|
||||||
use Symfony\Component\Validator\Constraint;
|
use Symfony\Component\Validator\Constraint;
|
||||||
use Symfony\Component\Validator\ConstraintValidator;
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|
||||||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
class SizeValidator extends ConstraintValidator
|
class SizeValidator extends ConstraintValidator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* Checks if the passed value is valid.
|
||||||
|
*
|
||||||
|
* @param mixed $value The value that should be validated
|
||||||
|
* @param Constraint $constraint The constraint for the validation
|
||||||
|
*
|
||||||
|
* @return Boolean Whether or not the value is valid
|
||||||
|
*
|
||||||
|
* @api
|
||||||
*/
|
*/
|
||||||
public function validate($value, Constraint $constraint)
|
public function validate($value, Constraint $constraint)
|
||||||
{
|
|
||||||
if (null === $constraint->min && null === $constraint->max) {
|
|
||||||
throw new ConstraintDefinitionException(
|
|
||||||
'Either "min" or "max" must be specified on constraint Size'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (null === $constraint->type) {
|
|
||||||
$type = $this->guessType($value);
|
|
||||||
} else {
|
|
||||||
$type = $constraint->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($type) {
|
|
||||||
case Size::TYPE_STRING:
|
|
||||||
return $this->validateString($value, $constraint);
|
|
||||||
case Size::TYPE_COLLECTION:
|
|
||||||
return $this->validateCollection($value, $constraint);
|
|
||||||
default:
|
|
||||||
throw new ConstraintDefinitionException(sprintf(
|
|
||||||
'The "type" on constraint Size must be either "%s" or "%s", "%s" given.',
|
|
||||||
Size::TYPE_STRING,
|
|
||||||
Size::TYPE_COLLECTION,
|
|
||||||
$type
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private function validateString($value, Constraint $constraint)
|
|
||||||
{
|
|
||||||
if (null === $value || '' === $value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
|
|
||||||
throw new UnexpectedTypeException($value, 'string, scalar or object with __toString()');
|
|
||||||
}
|
|
||||||
|
|
||||||
$value = (string) $value;
|
|
||||||
|
|
||||||
if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
|
|
||||||
$length = grapheme_strlen($value);
|
|
||||||
} elseif (function_exists('mb_strlen')) {
|
|
||||||
$length = mb_strlen($value, $constraint->charset);
|
|
||||||
} else {
|
|
||||||
$length = strlen($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->validateSize(
|
|
||||||
$constraint,
|
|
||||||
$length,
|
|
||||||
Size::TYPE_STRING,
|
|
||||||
array('{{ value }}' => $value)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function validateCollection($value, Constraint $constraint)
|
|
||||||
{
|
{
|
||||||
if (null === $value) {
|
if (null === $value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_array($value) && !$value instanceof \Countable) {
|
if (!is_numeric($value)) {
|
||||||
throw new UnexpectedTypeException($value, 'array or Countable');
|
$this->context->addViolation($constraint->invalidMessage, array(
|
||||||
}
|
'{{ value }}' => $value,
|
||||||
|
));
|
||||||
$count = count($value);
|
|
||||||
|
|
||||||
$this->validateSize(
|
|
||||||
$constraint,
|
|
||||||
$count,
|
|
||||||
Size::TYPE_COLLECTION,
|
|
||||||
array('{{ count }}' => $count)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function validateSize(Constraint $constraint, $size, $type, array $parameters)
|
|
||||||
{
|
|
||||||
if ($constraint->min == $constraint->max && $size != $constraint->max) {
|
|
||||||
$this->context->addViolation(
|
|
||||||
$constraint->getExactMessage($type),
|
|
||||||
array_merge(array('{{ limit }}' => $constraint->max), $parameters),
|
|
||||||
null,
|
|
||||||
(int) $constraint->max
|
|
||||||
);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $constraint->max && $size > $constraint->max) {
|
if ($value > $constraint->max) {
|
||||||
$this->context->addViolation(
|
$this->context->addViolation($constraint->maxMessage, array(
|
||||||
$constraint->getMaxMessage($type),
|
'{{ value }}' => $value,
|
||||||
array_merge(array('{{ limit }}' => $constraint->max), $parameters),
|
'{{ limit }}' => $constraint->max,
|
||||||
null,
|
));
|
||||||
(int) $constraint->max
|
|
||||||
);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $constraint->min && $size < $constraint->min) {
|
if ($value < $constraint->min) {
|
||||||
$this->context->addViolation(
|
$this->context->addViolation($constraint->minMessage, array(
|
||||||
$constraint->getMinMessage($type),
|
'{{ value }}' => $value,
|
||||||
array_merge(array('{{ limit }}' => $constraint->min), $parameters),
|
'{{ limit }}' => $constraint->min,
|
||||||
null,
|
));
|
||||||
(int) $constraint->min
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function guessType($value)
|
|
||||||
{
|
|
||||||
if (null === $value || is_scalar($value)) {
|
|
||||||
return Size::TYPE_STRING;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_object($value) && method_exists($value, '__toString')) {
|
|
||||||
if ($value instanceof \Countable) {
|
|
||||||
throw new \RuntimeException(
|
|
||||||
'The "type" must be specified on constraint Size because the '.
|
|
||||||
'validator is not able to guess it since the value is an object '.
|
|
||||||
'implementing both the __toString() method and the Countable '.
|
|
||||||
'interface.'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Size::TYPE_STRING;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_array($value) || $value instanceof \Countable) {
|
|
||||||
return Size::TYPE_COLLECTION;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new UnexpectedTypeException(
|
|
||||||
$value, 'scalar, string, array, Countable or object with __toString()'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -210,10 +210,6 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.</target>
|
<target>This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="56">
|
|
||||||
<source>This collection should contain exactly {{ limit }} elements.</source>
|
|
||||||
<target>This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.</target>
|
|
||||||
</trans-unit>
|
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,10 +210,6 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Cette collection doit contenir {{ limit }} élément ou moins.|Cette collection doit contenir {{ limit }} éléments ou moins.</target>
|
<target>Cette collection doit contenir {{ limit }} élément ou moins.|Cette collection doit contenir {{ limit }} éléments ou moins.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="56">
|
|
||||||
<source>This collection should contain exactly {{ limit }} elements.</source>
|
|
||||||
<target>Cette collection doit contenir exactement {{ limit }} élément.|Cette collection doit contenir exactement {{ limit }} éléments.</target>
|
|
||||||
</trans-unit>
|
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,10 +210,6 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Questa collezione dovrebbe contenere massimo {{ limit }} elemento.|Questa collezione dovrebbe contenere massimo {{ limit }} elementi.</target>
|
<target>Questa collezione dovrebbe contenere massimo {{ limit }} elemento.|Questa collezione dovrebbe contenere massimo {{ limit }} elementi.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="56">
|
|
||||||
<source>This collection should contain exactly {{ limit }} elements.</source>
|
|
||||||
<target>Questa collezione dovrebbe contenere esattamente {{ limit }} elemento.|Questa collezione dovrebbe contenere esattamente {{ limit }} elementi.</target>
|
|
||||||
</trans-unit>
|
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,10 +210,6 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Deze collectie moet {{ limit }} element of minder bevatten.|Deze collectie moet {{ limit }} elementen of minder bevatten.</target>
|
<target>Deze collectie moet {{ limit }} element of minder bevatten.|Deze collectie moet {{ limit }} elementen of minder bevatten.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="56">
|
|
||||||
<source>This collection should contain exactly {{ limit }} elements.</source>
|
|
||||||
<target>Deze collectie moet exact {{ limit }} element bevatten.|Deze collectie moet exact {{ limit }} elementen bevatten.</target>
|
|
||||||
</trans-unit>
|
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,10 +210,6 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos.</target>
|
<target>Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="56">
|
|
||||||
<source>This collection should contain exactly {{ limit }} elements.</source>
|
|
||||||
<target>Esta coleção deve conter exatamente {{ limit }} elemento.|Esta coleção deve conter exatamente {{ limit }} elementos.</target>
|
|
||||||
</trans-unit>
|
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,10 +210,6 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos.</target>
|
<target>Esta coleção deve conter {{ limit }} elemento ou menos.|Esta coleção deve conter {{ limit }} elementos ou menos.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="56">
|
|
||||||
<source>This collection should contain exactly {{ limit }} elements.</source>
|
|
||||||
<target>Esta coleção deve conter exatamente {{ limit }} elemento.|Esta coleção deve conter exatamente {{ limit }} elementos.</target>
|
|
||||||
</trans-unit>
|
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,10 +210,6 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Эта коллекция должна содержать {{ limit }} элемент или меньше.|Эта коллекция должна содержать {{ limit }} элемента или меньше.|Эта коллекция должна содержать {{ limit }} элементов или меньше.</target>
|
<target>Эта коллекция должна содержать {{ limit }} элемент или меньше.|Эта коллекция должна содержать {{ limit }} элемента или меньше.|Эта коллекция должна содержать {{ limit }} элементов или меньше.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="56">
|
|
||||||
<source>This collection should contain exactly {{ limit }} elements.</source>
|
|
||||||
<target>Эта коллекция должна содержать ровно {{ limit }} элемент.|Эта коллекция должна содержать ровно {{ limit }} элемента.|Эта коллекция должна содержать ровно {{ limit }} элементов.</target>
|
|
||||||
</trans-unit>
|
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,10 +210,6 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Ta zbirka bi morala vsebovati {{ limit }} ali manj elementov.</target>
|
<target>Ta zbirka bi morala vsebovati {{ limit }} ali manj elementov.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="56">
|
|
||||||
<source>This collection should contain exactly {{ limit }} elements.</source>
|
|
||||||
<target>Ta zbirka bi morala vsebovati točno {{ limit }} element.|Ta zbirka bi morala vsebovati točno {{ limit }} elementa.|Ta zbirka bi morala vsebovati točno {{ limit }} elemente.|Ta zbirka bi morala vsebovati točno {{ limit }} elementov.</target>
|
|
||||||
</trans-unit>
|
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,10 +210,6 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.</target>
|
<target>Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.|Ова колекција треба да садржи {{ limit }} или мање елемената.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="56">
|
|
||||||
<source>This collection should contain exactly {{ limit }} elements.</source>
|
|
||||||
<target>Ова колекција треба да садржи тачно {{ limit }} елемент.|Ова колекција треба да садржи тачно {{ limit }} елемента.|Ова колекција треба да садржи тачно {{ limit }} елемената.</target>
|
|
||||||
</trans-unit>
|
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -210,10 +210,6 @@
|
|||||||
<source>This collection should contain {{ limit }} elements or less.</source>
|
<source>This collection should contain {{ limit }} elements or less.</source>
|
||||||
<target>Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata.|Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata.|Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata.</target>
|
<target>Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata.|Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata.|Ova kolekcija treba da sadrži {{ limit }} ili manje elemenata.</target>
|
||||||
</trans-unit>
|
</trans-unit>
|
||||||
<trans-unit id="56">
|
|
||||||
<source>This collection should contain exactly {{ limit }} elements.</source>
|
|
||||||
<target>Ova kolekcija treba da sadrži tačno {{ limit }} element.|Ova kolekcija treba da sadrži tačno {{ limit }} elementa.|Ova kolekcija treba da sadrži tačno {{ limit }} elemenata.</target>
|
|
||||||
</trans-unit>
|
|
||||||
</body>
|
</body>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
@ -1,98 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Symfony\Component\Validator\Tests\Constraints;
|
|
||||||
|
|
||||||
use Symfony\Component\Validator\Constraints\Size;
|
|
||||||
|
|
||||||
class SizeTest extends \PHPUnit_Framework_TestCase
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @dataProvider getMinMessageData
|
|
||||||
*/
|
|
||||||
public function testGetMinMessage($options, $type, $expected)
|
|
||||||
{
|
|
||||||
$size = new Size($options);
|
|
||||||
$this->assertEquals($expected, $size->getMinMessage($type));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getMinMessageData()
|
|
||||||
{
|
|
||||||
$size = new Size();
|
|
||||||
|
|
||||||
return array(
|
|
||||||
array(array(), Size::TYPE_STRING, $this->readAttribute($size, 'stringMinMessage')),
|
|
||||||
array(array(), Size::TYPE_COLLECTION, $this->readAttribute($size, 'collectionMinMessage')),
|
|
||||||
array(array('minMessage' => 'Custom min message'), Size::TYPE_STRING, 'Custom min message'),
|
|
||||||
array(array('minMessage' => 'Custom min message'), Size::TYPE_COLLECTION, 'Custom min message'),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function testGetMinMessageWithInvalidType()
|
|
||||||
{
|
|
||||||
$size = new Size();
|
|
||||||
$size->getMinMessage('foo');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider getMaxMessageData
|
|
||||||
*/
|
|
||||||
public function testGetMaxMessage($options, $type, $expected)
|
|
||||||
{
|
|
||||||
$size = new Size($options);
|
|
||||||
$this->assertEquals($expected, $size->getMaxMessage($type));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getMaxMessageData()
|
|
||||||
{
|
|
||||||
$size = new Size();
|
|
||||||
|
|
||||||
return array(
|
|
||||||
array(array(), Size::TYPE_STRING, $this->readAttribute($size, 'stringMaxMessage')),
|
|
||||||
array(array(), Size::TYPE_COLLECTION, $this->readAttribute($size, 'collectionMaxMessage')),
|
|
||||||
array(array('maxMessage' => 'Custom max message'), Size::TYPE_STRING, 'Custom max message'),
|
|
||||||
array(array('maxMessage' => 'Custom max message'), Size::TYPE_COLLECTION, 'Custom max message'),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function testGetMaxMessageWithInvalidType()
|
|
||||||
{
|
|
||||||
$size = new Size();
|
|
||||||
$size->getMaxMessage('foo');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider getExactMessageData
|
|
||||||
*/
|
|
||||||
public function testGetExactMessage($options, $type, $expected)
|
|
||||||
{
|
|
||||||
$size = new Size($options);
|
|
||||||
$this->assertEquals($expected, $size->getExactMessage($type));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getExactMessageData()
|
|
||||||
{
|
|
||||||
$size = new Size();
|
|
||||||
|
|
||||||
return array(
|
|
||||||
array(array(), Size::TYPE_STRING, $this->readAttribute($size, 'stringExactMessage')),
|
|
||||||
array(array(), Size::TYPE_COLLECTION, $this->readAttribute($size, 'collectionExactMessage')),
|
|
||||||
array(array('exactMessage' => 'Custom exact message'), Size::TYPE_STRING, 'Custom exact message'),
|
|
||||||
array(array('exactMessage' => 'Custom exact message'), Size::TYPE_COLLECTION, 'Custom exact message'),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException InvalidArgumentException
|
|
||||||
*/
|
|
||||||
public function testGetExactMessageWithInvalidType()
|
|
||||||
{
|
|
||||||
$size = new Size();
|
|
||||||
$size->getExactMessage('foo');
|
|
||||||
}
|
|
||||||
}
|
|
@ -14,19 +14,6 @@ namespace Symfony\Component\Validator\Tests\Constraints;
|
|||||||
use Symfony\Component\Validator\Constraints\Size;
|
use Symfony\Component\Validator\Constraints\Size;
|
||||||
use Symfony\Component\Validator\Constraints\SizeValidator;
|
use Symfony\Component\Validator\Constraints\SizeValidator;
|
||||||
|
|
||||||
class SizeAmbiguous implements \Countable
|
|
||||||
{
|
|
||||||
public function __toString()
|
|
||||||
{
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function count()
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SizeValidatorTest extends \PHPUnit_Framework_TestCase
|
class SizeValidatorTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
protected $context;
|
protected $context;
|
||||||
@ -41,176 +28,94 @@ class SizeValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testNullIsValid()
|
public function testNullIsValid()
|
||||||
{
|
{
|
||||||
$this->context->expects($this->never())->method('addViolation');
|
$this->context->expects($this->never())
|
||||||
|
->method('addViolation');
|
||||||
|
|
||||||
$this->validator->validate(null, new Size(array('min' => 10, 'max' => 20)));
|
$this->validator->validate(null, new Size(array('min' => 10, 'max' => 20)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNullIsValidAsAString()
|
|
||||||
{
|
|
||||||
$this->context->expects($this->never())->method('addViolation');
|
|
||||||
|
|
||||||
$this->validator->validate(null, new Size(array('type' => 'string', 'min' => 10, 'max' => 20)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testNullIsValidAsACollectionCollection()
|
|
||||||
{
|
|
||||||
$this->context->expects($this->never())->method('addViolation');
|
|
||||||
|
|
||||||
$this->validator->validate(null, new Size(array('type' => 'collection', 'min' => 10, 'max' => 20)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testEmptyStringIsValid()
|
|
||||||
{
|
|
||||||
$this->context->expects($this->never())->method('addViolation');
|
|
||||||
|
|
||||||
$this->validator->validate('', new Size(array('min' => 10, 'max' => 20)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testEmptyStringIsValidAsAString()
|
|
||||||
{
|
|
||||||
$this->context->expects($this->never())->method('addViolation');
|
|
||||||
|
|
||||||
$this->validator->validate('', new Size(array('type' => 'string', 'min' => 10, 'max' => 20)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getValidStringValues
|
* @dataProvider getValidValues
|
||||||
*/
|
*/
|
||||||
public function testValidStringValues($value, $mbOnly = false)
|
public function testValidValues($value)
|
||||||
{
|
{
|
||||||
if ($mbOnly && !function_exists('mb_strlen')) {
|
|
||||||
return $this->markTestSkipped('mb_strlen does not exist');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolation');
|
->method('addViolation');
|
||||||
|
|
||||||
$constraint = new Size(array('min' => 6, 'max' => 10));
|
$constraint = new Size(array('min' => 10, 'max' => 20));
|
||||||
$this->validator->validate($value, $constraint);
|
$this->validator->validate($value, $constraint);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getValidStringValues()
|
public function getValidValues()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array(123456),
|
array(10.00001),
|
||||||
array(1234567890),
|
array(19.99999),
|
||||||
array('123456'),
|
array('10.00001'),
|
||||||
array('1234567890'),
|
array('19.99999'),
|
||||||
array('üüüüüü', true),
|
array(10),
|
||||||
array('üüüüüüüüüü', true),
|
array(20),
|
||||||
array('éééééé', true),
|
array(10.0),
|
||||||
array('éééééééééé', true),
|
array(20.0),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getInvalidStringValues
|
* @dataProvider getInvalidValues
|
||||||
*/
|
*/
|
||||||
public function testInvalidStringValues($value, $mbOnly = false)
|
public function testInvalidValues($value)
|
||||||
{
|
{
|
||||||
if ($mbOnly && !function_exists('mb_strlen')) {
|
$this->context->expects($this->once())
|
||||||
return $this->markTestSkipped('mb_strlen does not exist');
|
->method('addViolation');
|
||||||
}
|
|
||||||
|
|
||||||
$this->context->expects($this->once())->method('addViolation');
|
$constraint = new Size(array('min' => 10, 'max' => 20));
|
||||||
|
$this->validator->validate($value, $constraint);
|
||||||
$this->validator->validate($value, new Size(array('min' => 6, 'max' => 10)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInvalidStringValues()
|
public function getInvalidValues()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array(12345),
|
array(9.999999),
|
||||||
array(12345678901),
|
array(20.000001),
|
||||||
array('12345'),
|
array('9.999999'),
|
||||||
array('12345678901'),
|
array('20.000001'),
|
||||||
array('üüüüü', true),
|
array(new \stdClass()),
|
||||||
array('üüüüüüüüüüü', true),
|
|
||||||
array('ééééé', true),
|
|
||||||
array('ééééééééééé', true),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testMinMessageIsSet()
|
||||||
* @dataProvider getValidCollectionValues
|
|
||||||
*/
|
|
||||||
public function testValidCollectionValue($value)
|
|
||||||
{
|
{
|
||||||
$this->context->expects($this->never())->method('addViolation');
|
$constraint = new Size(array(
|
||||||
|
'min' => 10,
|
||||||
|
'max' => 20,
|
||||||
|
'minMessage' => 'myMessage',
|
||||||
|
));
|
||||||
|
|
||||||
$this->validator->validate($value, new Size(array('min' => 10, 'max' => 20)));
|
$this->context->expects($this->once())
|
||||||
|
->method('addViolation')
|
||||||
|
->with('myMessage', array(
|
||||||
|
'{{ value }}' => 9,
|
||||||
|
'{{ limit }}' => 10,
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->validator->validate(9, $constraint);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getValidCollectionValues()
|
public function testMaxMessageIsSet()
|
||||||
{
|
{
|
||||||
$countable = $this->getMock('Countable');
|
$constraint = new Size(array(
|
||||||
$countable->expects($this->any())->method('count')->will($this->returnValue(15));
|
'min' => 10,
|
||||||
|
'max' => 20,
|
||||||
|
'maxMessage' => 'myMessage',
|
||||||
|
));
|
||||||
|
|
||||||
return array(
|
$this->context->expects($this->once())
|
||||||
array($countable),
|
->method('addViolation')
|
||||||
array(range(1, 15)),
|
->with('myMessage', array(
|
||||||
);
|
'{{ value }}' => 21,
|
||||||
}
|
'{{ limit }}' => 20,
|
||||||
|
));
|
||||||
|
|
||||||
/**
|
$this->validator->validate(21, $constraint);
|
||||||
* @dataProvider getInvalidCollectionValues
|
|
||||||
*/
|
|
||||||
public function testInvalidCollectionValue($value)
|
|
||||||
{
|
|
||||||
$this->context->expects($this->once())->method('addViolation');
|
|
||||||
|
|
||||||
$this->validator->validate($value, new Size(array('min' => 10, 'max' => 20)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getInvalidCollectionValues()
|
|
||||||
{
|
|
||||||
$tooSmallCountable = $this->getMock('Countable');
|
|
||||||
$tooSmallCountable->expects($this->any())->method('count')->will($this->returnValue(5));
|
|
||||||
|
|
||||||
$tooBigCountable = $this->getmock('countable');
|
|
||||||
$tooBigCountable->expects($this->any())->method('count')->will($this->returnValue(25));
|
|
||||||
|
|
||||||
return array(
|
|
||||||
array($tooSmallCountable),
|
|
||||||
array($tooBigCountable),
|
|
||||||
array(array()),
|
|
||||||
array(range(1, 5)),
|
|
||||||
array(range(1, 25)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException RuntimeException
|
|
||||||
*/
|
|
||||||
public function throwsAnExceptionWhenOnAmbiguousValue()
|
|
||||||
{
|
|
||||||
$this->validator->validate(new SizeAmbiguous(), new Size(array('min' => 10, 'max' => 20)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
|
||||||
*/
|
|
||||||
public function testExpectsEitherStringOrCollectionCompatible()
|
|
||||||
{
|
|
||||||
$this->validator->validate(new \stdCLass(), new Size(array('min' => 10, 'max' => 20)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
|
||||||
*/
|
|
||||||
public function testExpectsStringCompatibleType()
|
|
||||||
{
|
|
||||||
$countable = $this->getMock('Countable');
|
|
||||||
|
|
||||||
$this->validator->validate($countable, new Size(array('type' => 'string', 'min' => 6, 'max' => 10)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
|
|
||||||
*/
|
|
||||||
public function testExpectsCollectionCompatibleType()
|
|
||||||
{
|
|
||||||
$this->validator->validate('string', new Size(array('type' => 'collection', 'min' => 6, 'max' => 10)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user