8000 [Validator] Added hasser support for entity method validation · symfony/symfony@e8b6978 · GitHub
[go: up one dir, main page]

Skip to content

Commit e8b6978

Browse files
bicpifabpot
authored andcommitted
[Validator] Added hasser support for entity method validation
1 parent b14fa26 commit e8b6978

File tree

9 files changed

+59
-3
lines changed

9 files changed

+59
-3
lines changed

src/Symfony/Component/Validator/Mapping/GetterMetadata.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ public function __construct($class, $property)
2727
{
2828
$getMethod = 'get'.ucfirst($property);
2929
$isMethod = 'is'.ucfirst($property);
30+
$hasMethod = 'has'.ucfirst($property);
3031

3132
if (method_exists($class, $getMethod)) {
3233
$method = $getMethod;
3334
} elseif (method_exists($class, $isMethod)) {
3435
$method = $isMethod;
36+
} elseif (method_exists($class, $hasMethod)) {
37+
$method = $hasMethod;
3538
} else {
36-
throw new ValidatorException(sprintf('Neither method %s nor %s exists in class %s', $getMethod, $isMethod, $class));
39+
throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
3740
}
3841

3942
parent::__construct($class, $method, $property);

src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public function loadClassMetadata(ClassMetadata $metadata)
7070

7171
$metadata->addConstraint($constraint);
7272
} elseif ($constraint instanceof Constraint) {
73-
if (preg_match('/^(get|is)(.+)$/i', $method->name, $matches)) {
73+
if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
7474
$metadata->addGetterConstraint(lcfirst($matches[2]), $constraint);
7575
} else {
76-
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));
76+
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));
7777
}
7878
}
7979

src/Symfony/Component/Validator/Tests/Fixtures/Entity.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ public function getLastName()
5656
return $this->lastName;
5757
}
5858

59+
/**
60+
* @Assert\True
61+
*/
62+
public function isValid()
63+
{
64+
return 'valid';
65+
}
66+
67+
/**
68+
* @Assert\True
69+
*/
70+
public function hasPermissions()
71+
{
72+
return 'permissions';
73+
}
74+
5975
public function getData()
6076
{
6177
return 'Overridden data';

src/Symfony/Component/Validator/Tests/Mapping/GetterMetadataTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,20 @@ public function testGetPropertyValueFromOverriddenPublicGetter()
4343

4444
$this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
4545
}
46+
47+
public function testGetPropertyValueFromIsser()
48+
{
49+
$entity = new Entity();
50+
$metadata = new GetterMetadata(self::CLASSNAME, 'valid');
51+
52+
$this->assertEquals('valid', $metadata->getPropertyValue($entity));
53+
}
54+
55+
public function testGetPropertyValueFromHasser()
56+
{
57+
$entity = new Entity();
58+
$metadata = new GetterMetadata(self::CLASSNAME, 'permissions');
59+
60+
$this->assertEquals('permissions', $metadata->getPropertyValue($entity));
61+
}
4662
}

src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Validator\Constraints\NotNull;
1919
use Symfony\Component\Validator\Constraints\Range;
2020
use Symfony\Component\Validator\Constraints\Choice;
21+
use Symfony\Component\Validator\Constraints\True;
2122
use Symfony\Component\Validator\Mapping\ClassMetadata;
2223
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
2324
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@@ -67,6 +68,8 @@ public function testLoadClassMetadata()
6768
'choices' => array('A', 'B'),
6869
)));
6970
$expected->addGetterConstraint('lastName', new NotNull());
71+
$expected->addGetterConstraint('valid', new True());
72+
$expected->addGetterConstraint('permissions', new True());
7073

7174
// load reflection class so that the comparison passes
7275
$expected->getReflectionClass();
@@ -134,6 +137,8 @@ public function testLoadClassMetadataAndMerge()
134137
'choices' => array('A', 'B'),
135138
)));
136139
$expected->addGetterConstraint('lastName', new NotNull());
140+
$expected->addGetterConstraint('valid', new True());
141+
$expected->addGetterConstraint('permissions', new True());
137142

138143
// load reflection class so that the comparison passes
139144
$expected->getReflectionClass();

src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Validator\Constraints\Range;
1919
use Symfony\Component\Validator\Constraints\Choice;
2020
use Symfony\Component\Validator\Constraints\Regex;
21+
use Symfony\Component\Validator\Constraints\True;
2122
use Symfony\Component\Validator\Mapping\ClassMetadata;
2223
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
2324
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@@ -69,6 +70,8 @@ public function testLoadClassMetadata()
6970
'choices' => array('A', 'B'),
7071
)));
7172
$expected->addGetterConstraint('lastName', new NotNull());
73+
$expected->addGetterConstraint('valid', new True());
74+
$expected->addGetterConstraint('permissions', new True());
7275

7376
$this->assertEquals($expected, $metadata);
7477
}

src/Symfony/Component/Validator/Tests/Mapping/Loader/YamlFileLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Validator\Constraints\NotNull;
1818
use Symfony\Component\Validator\Constraints\Range;
1919
use Symfony\Component\Validator\Constraints\Choice;
20+
use Symfony\Component\Validator\Constraints\True;
2021
use Symfony\Component\Validator\Mapping\ClassMetadata;
2122
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
2223
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@@ -86,6 +87,8 @@ public function testLoadClassMetadata()
8687
'choices' => array('A', 'B'),
8788
)));
8889
$expected->addGetterConstraint('lastName', new NotNull());
90+
$expected->addGetterConstraint('valid', new True());
91+
$expected->addGetterConstraint('permissions', new True());
8992

9093
$this->assertEquals($expected, $metadata);
9194
}

src/Symfony/Component/Validator/Tests/Mapping/Loader/constraint-mapping.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@
102102
<getter property="lastName">
103103
<constraint name="NotNull" />
104104
</getter>
105+
<getter property="valid">
106+
<constraint name="True" />
107+
</getter>
108+
<getter property="permissions">
109+
<constraint name="True" />
110+
</getter>
105111
</class>
106112

107113
<class name="Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity">

src/Symfony/Component/Validator/Tests/Mapping/Loader/constraint-mapping.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Symfony\Component\Validator\Tests\Fixtures\Entity:
5353
getters:
5454
lastName:
5555
- NotNull: ~
56+
valid:
57+
- "True": ~
58+
permissions:
59+
- "True": ~
5660

5761
Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity:
5862
group_sequence_provider: true

0 commit comments

Comments
 (0)
0