Merge pull request #1452 from BigZ/feature/add-new-assert-uses

feature: Add new validation from constraints.
This commit is contained in:
Guilhem N 2018-12-24 01:24:14 +01:00 committed by GitHub
commit 61b1ed35ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 2 deletions

View File

@ -63,8 +63,6 @@ class SymfonyConstraintAnnotationReader
$property->setMaxLength($annotation->max);
} elseif ($annotation instanceof Assert\Regex) {
$this->appendPattern($property, $annotation->getHtmlPattern());
} elseif ($annotation instanceof Assert\DateTime) {
$this->appendPattern($property, $annotation->format);
} elseif ($annotation instanceof Assert\Count) {
$property->setMinItems($annotation->min);
$property->setMaxItems($annotation->max);
@ -72,6 +70,13 @@ class SymfonyConstraintAnnotationReader
$property->setEnum($annotation->callback ? call_user_func(is_array($annotation->callback) ? $annotation->callback : [$reflectionProperty->class, $annotation->callback]) : $annotation->choices);
} elseif ($annotation instanceof Assert\Expression) {
$this->appendPattern($property, $annotation->message);
} elseif ($annotation instanceof Assert\Range) {
$property->setMinimum($annotation->min);
$property->setMaximum($annotation->max);
} elseif ($annotation instanceof Assert\LessThan) {
$property->setExclusiveMaximum($annotation->value);
} elseif ($annotation instanceof Assert\LessThanOrEqual) {
$property->setMaximum($annotation->value);
}
}
}

View File

@ -81,6 +81,27 @@ class SymfonyConstraints
*/
private $propertyExpression;
/**
* @var int
*
* @Assert\Range(min=1, max=5)
*/
private $propertyRange;
/**
* @var int
*
* @Assert\LessThan(42)
*/
private $propertyLessThan;
/**
* @var int
*
* @Assert\LessThanOrEqual(23)
*/
private $propertyLessThanOrEqual;
/**
* @param int $propertyNotBlank
*/
@ -153,6 +174,30 @@ class SymfonyConstraints
$this->propertyExpression = $propertyExpression;
}
/**
* @param int $propertyRange
*/
public function setPropertyRange(int $propertyRange): void
{
$this->propertyRange = $propertyRange;
}
/**
* @param int $propertyLessThan
*/
public function setPropertyLessThan(int $propertyLessThan): void
{
$this->propertyLessThan = $propertyLessThan;
}
/**
* @param int $propertyLessThanOrEqual
*/
public function setPropertyLessThanOrEqual(int $propertyLessThanOrEqual): void
{
$this->propertyLessThanOrEqual = $propertyLessThanOrEqual;
}
/**
* @return array
*/

View File

@ -385,6 +385,19 @@ class FunctionalTest extends WebTestCase
'type' => 'integer',
'pattern' => 'If this is a tech post, the category should be either php or symfony!',
],
'propertyRange' => [
'type' => 'integer',
'maximum' => 5,
'minimum' => 1,
],
'propertyLessThan' => [
'type' => 'integer',
'exclusiveMaximum' => 42,
],
'propertyLessThanOrEqual' => [
'type' => 'integer',
'maximum' => 23,
],
],
'type' => 'object',
], $this->getModel('SymfonyConstraints')->toArray());