Skip to content

Commit

Permalink
[Validator] Reverted the changes done to the Size constraint in 3a5e84f
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Jul 11, 2012
1 parent d84b689 commit d661837
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 510 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ public function guessTypeForConstraint(Constraint $constraint)
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);

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\Max':
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);
Expand Down Expand Up @@ -211,12 +203,6 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
case 'Symfony\Component\Validator\Constraints\MaxLength':
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':
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
Expand All @@ -225,6 +211,9 @@ public function guessMaxLengthForConstraint(Constraint $constraint)

case 'Symfony\Component\Validator\Constraints\Max':
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);
}
}

Expand All @@ -241,25 +230,6 @@ public function guessPatternForConstraint(Constraint $constraint)
case 'Symfony\Component\Validator\Constraints\MinLength':
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':
$htmlPattern = $constraint->getHtmlPattern();

Expand All @@ -271,6 +241,9 @@ public function guessPatternForConstraint(Constraint $constraint)
case 'Symfony\Component\Validator\Constraints\Min':
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':
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
Expand Down
1 change: 0 additions & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ CHANGELOG
-----

* 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
* added support for MIME with wildcard in FileValidator
* changed Collection validator to add "missing" and "extra" errors to
Expand Down
69 changes: 8 additions & 61 deletions src/Symfony/Component/Validator/Constraints/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,70 +20,17 @@
*/
class Size extends Constraint
{
const TYPE_STRING = 'string';
const TYPE_COLLECTION = 'collection';

public $minMessage;
public $maxMessage;
public $exactMessage;
public $type;
public $minMessage = 'This value should be {{ limit }} or more';
public $maxMessage = 'This value should be {{ limit }} or less';
public $invalidMessage = 'This value should be a valid number';
public $min;
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.';
private $stringExactMessage = 'This value should have exactly {{ limit }} characters.';

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 $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)
/**
* {@inheritDoc}
*/
public function getRequiredOptions()
{
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.');
}
return array('min', 'max');
}
}
153 changes: 27 additions & 126 deletions src/Symfony/Component/Validator/Constraints/SizeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,151 +13,52 @@

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

/**
* @author Bernhard Schussek <[email protected]>
*
* @api
*/
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)
{
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) {
return;
}

if (!is_array($value) && !$value instanceof \Countable) {
throw new UnexpectedTypeException($value, 'array or Countable');
}

$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
);
if (!is_numeric($value)) {
$this->context->addViolation($constraint->invalidMessage, array(
'{{ value }}' => $value,
));

return;
}

if (null !== $constraint->max && $size > $constraint->max) {
$this->context->addViolation(
$constraint->getMaxMessage($type),
array_merge(array('{{ limit }}' => $constraint->max), $parameters),
null,
(int) $constraint->max
);
if ($value > $constraint->max) {
$this->context->addViolation($constraint->maxMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $constraint->max,
));

return;
}

if (null !== $constraint->min && $size < $constraint->min) {
$this->context->addViolation(
$constraint->getMinMessage($type),
array_merge(array('{{ limit }}' => $constraint->min), $parameters),
null,
(int) $constraint->min
);
if ($value < $constraint->min) {
$this->context->addViolation($constraint->minMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $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()'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@
<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>
</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>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@
<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>
</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>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@
<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>
</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>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@
<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>
</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>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@
<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>
</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>
</file>
</xliff>
</xliff>
Loading

0 comments on commit d661837

Please sign in to comment.