Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 53fec31

Browse filesBrowse files
committed
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
2 parents b14fa26 + e8b6978 commit 53fec31
Copy full SHA for 53fec31

File tree

Expand file treeCollapse file tree

9 files changed

+59
-3
lines changed
Filter options
Expand file treeCollapse file tree

9 files changed

+59
-3
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Mapping/GetterMetadata.php
+4-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
+2-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Fixtures/Entity.php
+16Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Mapping/GetterMetadataTest.php
+16Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderTest.php
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php
+3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Mapping/Loader/YamlFileLoaderTest.php
+3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Mapping/Loader/constraint-mapping.xml
+6Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Mapping/Loader/constraint-mapping.yml
+4Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.