merged branch dlsniper/validator-check-property-metadata (PR #6972)

This PR was squashed before being merged into the 2.2 branch (closes #6972).

Commits
-------

4cbdbcb [Validator] Add check for existing metadata on property

Discussion
----------

[Validator] Add check for existing metadata on property

| Q             | A
| ------------- | ---
| Bug fix?      | no (sort of)
| New feature?  | no
| BC breaks?    | no (I don't think so)
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ~
| License       | MIT
| Doc PR        | ~

Todo:
- [x] Check if the new method should be added to the interface as well.

This adds a check for metadata existence before usage else the code will emit a warning.

This should not be a BC break or a bug fix as the patched code is 2.2 specific.

@bschussek let me know if I should add the method in the interface as well. Thank you.

---------------------------------------------------------------------------

by vicb at 2013-02-05T10:40:56Z

Strictly speaking this is a BC break (you are adding a public method). But I guess we should better defined what should be describe as BC break - I have asked myself the same question more than one time /cc @fabpot

Please also add a UT and a note in the changelog.

---------------------------------------------------------------------------

by dlsniper at 2013-02-05T11:26:30Z

@vicb thanks for input. In this case I guess I should add the method to the interface as well.

As for unit tests I'll add them soon as well.

---------------------------------------------------------------------------

by fabpot at 2013-02-05T14:19:14Z

Can you add a test?

---------------------------------------------------------------------------

by dlsniper at 2013-02-05T15:57:12Z

I'll do it tonight as I've bumped into some work related issues.

---------------------------------------------------------------------------

by fabpot at 2013-02-11T11:11:12Z

Just for the record, this is not a BC break as the interface was added in 2.2.
This commit is contained in:
Fabien Potencier 2013-02-11 12:11:43 +01:00
commit c5fcf0d0cc
4 changed files with 29 additions and 0 deletions

View File

@ -301,6 +301,14 @@ class ClassMetadata extends ElementMetadata implements MetadataInterface, ClassB
return $this->members[$property];
}
/**
* {@inheritdoc}
*/
public function hasPropertyMetadata($property)
{
return array_key_exists($property, $this->members);
}
/**
* {@inheritdoc}
*/

View File

@ -18,6 +18,15 @@ namespace Symfony\Component\Validator;
*/
interface PropertyMetadataContainerInterface
{
/**
* Check if there's any metadata attached to the given named property.
*
* @param string $property The property name.
*
* @return Boolean
*/
public function hasPropertyMetadata($property);
/**
* Returns all metadata instances for the given named property.
*

View File

@ -187,6 +187,10 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
$result = $this->validator->validateProperty($entity, 'firstName');
$this->assertCount(1, $result);
$result = $this->validator->validateProperty($entity, 'lastName');
$this->assertCount(0, $result);
}
public function testValidatePropertyValue()

View File

@ -112,6 +112,10 @@ class Validator implements ValidatorInterface
}
foreach ($this->resolveGroups($groups) as $group) {
if (!$metadata->hasPropertyMetadata($property)) {
continue;
}
foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
$propMeta->accept($visitor, $propMeta->getPropertyValue($containingValue), $group, $property);
}
@ -139,6 +143,10 @@ class Validator implements ValidatorInterface
}
foreach ($this->resolveGroups($groups) as $group) {
if (!$metadata->hasPropertyMetadata($property)) {
continue;
}
foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
$propMeta->accept($visitor, $value, $group, $property);
}