8000 feature #11703 [Validator] deprecate member metadata accessors (Tobion) · symfony/symfony@77aa12f · GitHub
[go: up one dir, main page]

Skip to content

Commit 77aa12f

Browse files
committed
feature #11703 [Validator] deprecate member metadata accessors (Tobion)
This PR was merged into the 2.6-dev branch. Discussion ---------- [Validator] deprecate member metadata accessors | Q | A | ------------- | --- | Bug fix? | sort of | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | https://github.com/symfony/symfony/pull/11614/files#r16385109 | License | MIT | Doc PR | deprecate member metadata accessors in favor of existing property metadata accessors Commits ------- 14d3f97 [Validator] add getConstraints to MetadataInterface 04eb61b [Validator] deprecate member metadata accessors in favor of existing property metadata accessors
2 parents 69d4dd9 + 14d3f97 commit 77aa12f

File tree

11 files changed

+55
-47
lines changed

11 files changed

+55
-47
lines changed

src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
use Symfony\Component\Form\Guess\Guess;
1616
use Symfony\Component\Form\Guess\TypeGuess;
1717
use Symfony\Component\Form\Guess\ValueGuess;
18-
use Symfony\Component\Validator\MetadataFactoryInterface;
1918
use Symfony\Component\Validator\Constraint;
19+
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
20+
use Symfony\Component\Validator\MetadataFactoryInterface;
2021

2122
class ValidatorTypeGuesser implements FormTypeGuesserInterface
2223
{
@@ -264,8 +265,8 @@ protected function guess($class, $property, \Closure $closure, $defaultValue = n
264265
$guesses = array();
265266
$classMetadata = $this->metadataFactory->getMetadataFor($class);
266267

267-
if ($classMetadata->hasMemberMetadatas($property)) {
268-
$memberMetadatas = $classMetadata->getMemberMetadatas($property);
268+
if ($classMetadata instanceof ClassMetadataInterface && $classMetadata->hasPropertyMetadata($property)) {
269+
$memberMetadatas = $classMetadata->getPropertyMetadata($property);
269270

270271
foreach ($memberMetadatas as $memberMetadata) {
271272
$constraints = $memberMetadata->getConstraints();

src/Symfony/Component/Form/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"symfony/property-access": "~2.3"
2424
},
2525
"require-dev": {
26-
"symfony/validator": "~2.2",
26+
"symfony/validator": "~2.3",
2727
"symfony/http-foundation": "~2.2",
2828
"symfony/http-kernel": "~2.4",
2929
"symfony/security-csrf": "~2.4",

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ CHANGELOG
99
* [BC BREAK] added internal `ExecutionContextInterface::setConstraint()`
1010
* added `ConstraintViolation::getConstraint()`
1111
* [BC BREAK] The `ExpressionValidator` will now evaluate the Expression even when the property value is null or an empty string
12+
* deprecated `ClassMetadata::hasMemberMetadatas()`
13+
* deprecated `ClassMetadata::getMemberMetadatas()`
14+
* deprecated `ClassMetadata::addMemberMetadata()`
15+
* [BC BREAK] added `Mapping\MetadataInterface::getConstraints()`
1216

1317
2.5.0
1418
-----

src/Symfony/Component/Validator/Context/ExecutionContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class ExecutionContext implements ExecutionContextInterface
8989
/**
9090
* The current validation metadata.
9191
*
92-
* @var MetadataInterface
92+
* @var MetadataInterface|null
9393
*/
9494
private $metadata;
9595

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function accept(ValidationVisitorInterface $visitor, $value, $group, $pro
158158
$pathPrefix = empty($propertyPath) ? '' : $propertyPath.'.';
159159

160160
foreach ($this->getConstrainedProperties() as $property) {
161-
foreach ($this->getMemberMetadatas($property) as $member) {
161+
foreach ($this->getPropertyMetadata($property) as $member) {
162162
$member->accept($visitor, $member->getPropertyValue($value), $group, $pathPrefix.$property, $propagatedGroup);
163163
}
164164
}
@@ -266,7 +266,7 @@ public function addPropertyConstraint($property, Constraint $constraint)
266266
if (!isset($this->properties[$property])) {
267267
$this->properties[$property] = new PropertyMetadata($this->getClassName(), $property);
268268

269-
$this->addMemberMetadata($this->properties[$property]);
269+
$this->addPropertyMetadata($this->properties[$property]);
270270
}
271271

272272
$constraint->addImplicitGroupName($this->getDefaultGroup());
@@ -292,7 +292,7 @@ public function addGetterConstraint($property, Constraint $constraint)
292292
if (!isset($this->getters[$property])) {
293293
$this->getters[$property] = new GetterMetadata($this->getClassName(), $property);
294294

295-
$this->addMemberMetadata($this->getters[$property]);
295+
$this->addPropertyMetadata($this->getters[$property]);
296296
}
297297

298298
$constraint->addImplicitGroupName($this->getDefaultGroup());
@@ -314,16 +314,16 @@ public function mergeConstraints(ClassMetadata $source)
314314
}
315315

316316
foreach ($source->getConstrainedProperties() as $property) {
317-
foreach ($source->getMemberMetadatas($property) as $member) {
317+
foreach ($source->getPropertyMetadata($property) as $member) {
318318
$member = clone $member;
319319

320320
foreach ($member->getConstraints() as $constraint) {
321321
$constraint->addImplicitGroupName($this->getDefaultGroup());
322322
}
323323

324-
$this->addMemberMetadata($member);
324+
$this->addPropertyMetadata($member);
325325

326-
if (!$member->isPrivate($this->name)) {
326+
if ($member instanceof MemberMetadata && !$member->isPrivate($this->name)) {
327327
$property = $member->getPropertyName();
328328

329329
if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
@@ -340,12 +340,12 @@ public function mergeConstraints(ClassMetadata $source)
340340
* Adds a member metadata.
341341
*
342342
* @param MemberMetadata $metadata
343+
*
344+
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
343345
*/
344346
protected function addMemberMetadata(MemberMetadata $metadata)
345347
{
346-
$property = $metadata->getPropertyName();
347-
348-
$this->members[$property][] = $metadata;
348+
$this->addPropertyMetadata($metadata);
349349
}
350350

351351
/**
@@ -354,10 +354,12 @@ protected function addMemberMetadata(MemberMetadata $metadata)
354354
* @param string $property The name of the property
355355
*
356356
* @return bool
357+
*
358+
* @deprecated Deprecated since version 2.6, to be removed in 3.0. Use {@link hasPropertyMetadata} instead.
357359
*/
358360
public function hasMemberMetadatas($property)
359361
{
360-
return array_key_exists($property, $this->members);
362+
return $this->hasPropertyMetadata($property);
361363
}
362364

363365
/**
@@ -366,14 +368,12 @@ public function hasMemberMetadatas($property)
366368
* @param string $property The name of the property
367369
*
368370
* @return MemberMetadata[] An array of MemberMetadata
371+
*
372+
* @deprecated Deprecated since version 2.6, to be removed in 3.0. Use {@link getPropertyMetadata} instead.
369373
*/
370374
public function getMemberMetadatas($property)
371375
{
372-
if (!isset($this->members[$property])) {
373-
return array();
374-
}
375-
376-
return $this->members[$property];
376+
return $this->getPropertyMetadata($property);
377377
}
378378

379379
/**
@@ -503,4 +503,16 @@ public function getCascadingStrategy()
503503
{
504504
return CascadingStrategy::NONE;
505505
}
506+
507+
/**
508+
* Adds a property metadata.
509+
*
510+
* @param PropertyMetadataInterface $metadata
511+
*/
512+
private function addPropertyMetadata(PropertyMetadataInterface $metadata)
513+
{
514+
$property = $metadata->getPropertyName();
515+
516+
$this->members[$property][] = $metadata;
517+
}
506518
}

src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __construct(LoaderInterface $loader = null, CacheInterface $cach
7474
}
7575

7676
/**
77-
* Returns the metadata for the given class name or object.
77+
* {@inheritdoc}
7878
*
7979
* If the method was called with the same class name (or an object of that
8080
* class) before, the same metadata instance is returned.
@@ -87,12 +87,6 @@ public function __construct(LoaderInterface $loader = null, CacheInterface $cach
8787
* configured with a loader, the metadata is passed to the
8888
* {@link LoaderInterface::loadClassMetadata()} method for further
8989
* configuration. At last, the new object is returned.
90-
*
91-
* @param string|object $value A class name or an object
92-
*
93-
* @return MetadataInterface The metadata for the value
94-
*
95-
* @throws NoSuchMetadataException If no metadata exists for the given value
9690
*/
9791
public function getMetadataFor($value)
9892
{
@@ -141,12 +135,7 @@ public function getMetadataFor($value)
141135
}
142136

143137
/**
144-
* Returns whether the factory is able to return metadata for the given
145-
* class name or object.
146-
*
147-
* @param string|object $value A class name or an object
148-
*
149-
* @return bool Whether metadata can be returned for that class
138+
* {@inheritdoc}
150139
*/
151140
public function hasMetadataFor($value)
152141
{

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ public function addConstraints(array $constraints)
180180
}
181181

182182
/**
183-
* Returns all constraints of this element.
184-
*
185-
* @return Constraint[] A list of Constraint instances
183+
* {@inheritdoc}
186184
*/
187185
public function getConstraints()
188186
{
@@ -200,12 +198,9 @@ public function hasConstraints()
200198
}
201199

202200
/**
203-
* Returns the constraints of the given group and global ones (* group).
204-
*
205-
* @param string $group The group name
201+
* {@inheritdoc}
206202
*
207-
* @return Constraint[] An list of all the Constraint instances belonging
208-
* to the group
203+
* Aware of the global group (* group).
209204
*/
210205
public function findConstraints($group)
211206
{

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@ public function getCascadingStrategy();
4848
* @see TraversalStrategy
4949
*/
5050
public function getTraversalStrategy();
51+
52+
/**
53+
* Returns all constraints of this element.
54+
*
55+
* @return Constraint[] A list of Constraint instances
56+
*/
57+
public function getConstraints();
5158
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class FakeClassMetadata extends ClassMetadata
1717
{
18-
public function addPropertyMetadata($propertyName, $metadata)
18+
public function addCustomPropertyMetadata($propertyName, $metadata)
1919
{
2020
if (!isset($this->members[$propertyName])) {
2121
$this->members[$propertyName] = array();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testMergeConstraintsMergesMemberConstraints()
101101
))),
102102
);
103103

104-
$members = $this->metadata->getMemberMetadatas('firstName');
104+
$members = $this->metadata->getPropertyMetadata('firstName');
105105

106106
$this->assertCount(1, $members);
107107
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
@@ -112,8 +112,8 @@ public function testMemberMetadatas()
112112
{
113113
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
114114

115-
$this->assertTrue($this->metadata->hasMemberMetadatas('firstName'));
116-
$this->assertFalse($this->metadata->hasMemberMetadatas('non_existant_field'));
115+
$this->assertTrue($this->metadata->hasPropertyMetadata('firstName'));
116+
$this->assertFalse($this->metadata->hasPropertyMetadata('non_existant_field'));
117117
}
118118

119119
public function testMergeConstraintsKeepsPrivateMembersSeparate()
@@ -138,7 +138,7 @@ public function testMergeConstraintsKeepsPrivateMembersSeparate()
138138
))),
139139
);
140140

141-
$members = $this->metadata->getMemberMetadatas('internal');
141+
$members = $this->metadata->getPropertyMetadata('internal');
142142

143143
$this->assertCount(2, $members);
144144
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());

src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ public function testPropertyMetadataMustImplementPropertyMetadataInterface()
633633
// Legacy interface
634634
$propertyMetadata = $this->getMock('Symfony\Component\Validator\MetadataInterface');
635635
$metadata = new FakeClassMetadata(get_class($entity));
636-
$metadata->addPropertyMetadata('firstName', $propertyMetadata);
636+
$metadata->addCustomPropertyMetadata('firstName', $propertyMetadata);
637637

638638
$this->metadataFactory->addMetadata($metadata);
639639

0 commit comments

Comments
 (0)
0