feature #9097 [Validator] Added hasser support for entity method validation (bicpi)
This PR was squashed before being merged into the 2.5-dev branch (closes #9097).
Discussion
----------
[Validator] Added hasser support for entity method validation
Hasser support was added in addition to existing getter and isser support for more consistency between components
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | symfony/symfony-docs#3418
Commits
-------
e8b6978
[Validator] Added hasser support for entity method validation
This commit is contained in:
commit
53fec31566
@ -27,13 +27,16 @@ class GetterMetadata extends MemberMetadata
|
|||||||
{
|
{
|
||||||
$getMethod = 'get'.ucfirst($property);
|
$getMethod = 'get'.ucfirst($property);
|
||||||
$isMethod = 'is'.ucfirst($property);
|
$isMethod = 'is'.ucfirst($property);
|
||||||
|
$hasMethod = 'has'.ucfirst($property);
|
||||||
|
|
||||||
if (method_exists($class, $getMethod)) {
|
if (method_exists($class, $getMethod)) {
|
||||||
$method = $getMethod;
|
$method = $getMethod;
|
||||||
} elseif (method_exists($class, $isMethod)) {
|
} elseif (method_exists($class, $isMethod)) {
|
||||||
$method = $isMethod;
|
$method = $isMethod;
|
||||||
|
} elseif (method_exists($class, $hasMethod)) {
|
||||||
|
$method = $hasMethod;
|
||||||
} else {
|
} else {
|
||||||
throw new ValidatorException(sprintf('Neither method %s nor %s exists in class %s', $getMethod, $isMethod, $class));
|
throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::__construct($class, $method, $property);
|
parent::__construct($class, $method, $property);
|
||||||
|
@ -70,10 +70,10 @@ class AnnotationLoader implements LoaderInterface
|
|||||||
|
|
||||||
$metadata->addConstraint($constraint);
|
$metadata->addConstraint($constraint);
|
||||||
} elseif ($constraint instanceof Constraint) {
|
} elseif ($constraint instanceof Constraint) {
|
||||||
if (preg_match('/^(get|is)(.+)$/i', $method->name, $matches)) {
|
if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
|
||||||
$metadata->addGetterConstraint(lcfirst($matches[2]), $constraint);
|
$metadata->addGetterConstraint(lcfirst($matches[2]), $constraint);
|
||||||
} else {
|
} else {
|
||||||
throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get" or "is".', $className, $method->name));
|
throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,6 +56,22 @@ class Entity extends EntityParent implements EntityInterface
|
|||||||
return $this->lastName;
|
return $this->lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Assert\True
|
||||||
|
*/
|
||||||
|
public function isValid()
|
||||||
|
{
|
||||||
|
return 'valid';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Assert\True
|
||||||
|
*/
|
||||||
|
public function hasPermissions()
|
||||||
|
{
|
||||||
|
return 'permissions';
|
||||||
|
}
|
||||||
|
|
||||||
public function getData()
|
public function getData()
|
||||||
{
|
{
|
||||||
return 'Overridden data';
|
return 'Overridden data';
|
||||||
|
@ -43,4 +43,20 @@ class GetterMetadataTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
|
$this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetPropertyValueFromIsser()
|
||||||
|
{
|
||||||
|
$entity = new Entity();
|
||||||
|
$metadata = new GetterMetadata(self::CLASSNAME, 'valid');
|
||||||
|
|
||||||
|
$this->assertEquals('valid', $metadata->getPropertyValue($entity));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetPropertyValueFromHasser()
|
||||||
|
{
|
||||||
|
$entity = new Entity();
|
||||||
|
$metadata = new GetterMetadata(self::CLASSNAME, 'permissions');
|
||||||
|
|
||||||
|
$this->assertEquals('permissions', $metadata->getPropertyValue($entity));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ use Symfony\Component\Validator\Constraints\Collection;
|
|||||||
use Symfony\Component\Validator\Constraints\NotNull;
|
use Symfony\Component\Validator\Constraints\NotNull;
|
||||||
use Symfony\Component\Validator\Constraints\Range;
|
use Symfony\Component\Validator\Constraints\Range;
|
||||||
use Symfony\Component\Validator\Constraints\Choice;
|
use Symfony\Component\Validator\Constraints\Choice;
|
||||||
|
use Symfony\Component\Validator\Constraints\True;
|
||||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||||
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
|
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
|
||||||
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
|
||||||
@ -67,6 +68,8 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
'choices' => array('A', 'B'),
|
'choices' => array('A', 'B'),
|
||||||
)));
|
)));
|
||||||
$expected->addGetterConstraint('lastName', new NotNull());
|
$expected->addGetterConstraint('lastName', new NotNull());
|
||||||
|
$expected->addGetterConstraint('valid', new True());
|
||||||
|
$expected->addGetterConstraint('permissions', new True());
|
||||||
|
|
||||||
// load reflection class so that the comparison passes
|
// load reflection class so that the comparison passes
|
||||||
$expected->getReflectionClass();
|
$expected->getReflectionClass();
|
||||||
@ -134,6 +137,8 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
'choices' => array('A', 'B'),
|
'choices' => array('A', 'B'),
|
||||||
)));
|
)));
|
||||||
$expected->addGetterConstraint('lastName', new NotNull());
|
$expected->addGetterConstraint('lastName', new NotNull());
|
||||||
|
$expected->addGetterConstraint('valid', new True());
|
||||||
|
$expected->addGetterConstraint('permissions', new True());
|
||||||
|
|
||||||
// load reflection class so that the comparison passes
|
// load reflection class so that the comparison passes
|
||||||
$expected->getReflectionClass();
|
$expected->getReflectionClass();
|
||||||
|
@ -18,6 +18,7 @@ use Symfony\Component\Validator\Constraints\NotNull;
|
|||||||
use Symfony\Component\Validator\Constraints\Range;
|
use Symfony\Component\Validator\Constraints\Range;
|
||||||
use Symfony\Component\Validator\Constraints\Choice;
|
use Symfony\Component\Validator\Constraints\Choice;
|
||||||
use Symfony\Component\Validator\Constraints\Regex;
|
use Symfony\Component\Validator\Constraints\Regex;
|
||||||
|
use Symfony\Component\Validator\Constraints\True;
|
||||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||||
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
|
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
|
||||||
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
|
||||||
@ -69,6 +70,8 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
'choices' => array('A', 'B'),
|
'choices' => array('A', 'B'),
|
||||||
)));
|
)));
|
||||||
$expected->addGetterConstraint('lastName', new NotNull());
|
$expected->addGetterConstraint('lastName', new NotNull());
|
||||||
|
$expected->addGetterConstraint('valid', new True());
|
||||||
|
$expected->addGetterConstraint('permissions', new True());
|
||||||
|
|
||||||
$this->assertEquals($expected, $metadata);
|
$this->assertEquals($expected, $metadata);
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ use Symfony\Component\Validator\Constraints\Collection;
|
|||||||
use Symfony\Component\Validator\Constraints\NotNull;
|
use Symfony\Component\Validator\Constraints\NotNull;
|
||||||
use Symfony\Component\Validator\Constraints\Range;
|
use Symfony\Component\Validator\Constraints\Range;
|
||||||
use Symfony\Component\Validator\Constraints\Choice;
|
use Symfony\Component\Validator\Constraints\Choice;
|
||||||
|
use Symfony\Component\Validator\Constraints\True;
|
||||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||||
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
|
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
|
||||||
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
|
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
|
||||||
@ -86,6 +87,8 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
'choices' => array('A', 'B'),
|
'choices' => array('A', 'B'),
|
||||||
)));
|
)));
|
||||||
$expected->addGetterConstraint('lastName', new NotNull());
|
$expected->addGetterConstraint('lastName', new NotNull());
|
||||||
|
$expected->addGetterConstraint('valid', new True());
|
||||||
|
$expected->addGetterConstraint('permissions', new True());
|
||||||
|
|
||||||
$this->assertEquals($expected, $metadata);
|
$this->assertEquals($expected, $metadata);
|
||||||
}
|
}
|
||||||
|
@ -102,6 +102,12 @@
|
|||||||
<getter property="lastName">
|
<getter property="lastName">
|
||||||
<constraint name="NotNull" />
|
<constraint name="NotNull" />
|
||||||
</getter>
|
</getter>
|
||||||
|
<getter property="valid">
|
||||||
|
<constraint name="True" />
|
||||||
|
</getter>
|
||||||
|
<getter property="permissions">
|
||||||
|
<constraint name="True" />
|
||||||
|
</getter>
|
||||||
</class>
|
</class>
|
||||||
|
|
||||||
<class name="Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity">
|
<class name="Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity">
|
||||||
|
@ -53,6 +53,10 @@ Symfony\Component\Validator\Tests\Fixtures\Entity:
|
|||||||
getters:
|
getters:
|
||||||
lastName:
|
lastName:
|
||||||
- NotNull: ~
|
- NotNull: ~
|
||||||
|
valid:
|
||||||
|
- "True": ~
|
||||||
|
permissions:
|
||||||
|
- "True": ~
|
||||||
|
|
||||||
Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity:
|
Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity:
|
||||||
group_sequence_provider: true
|
group_sequence_provider: true
|
||||||
|
Reference in New Issue
Block a user