8000 [Validator] do not guess getter method names by xabbuh · Pull Request #21115 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Validator] do not guess getter method names #21115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ public function addPropertyConstraints($property, array $constraints)
* The name of the getter is assumed to be the name of the property with an
* uppercased first letter and either the prefix "get" or "is".
*
* @param string $property The name of the property
* @param Constraint $constraint The constraint
* @param string $property The name of the property
* @param Constraint $constraint The constraint
* @param string|null $method The method that is called to retrieve the value being validated (null for auto-detection)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong PHPDoc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, see #21993

*
* @return $this
*/
Expand All @@ -319,6 +320,30 @@ public function addGetterConstraint($property, Constraint $constraint)
return $this;
}

/**
* Adds a constraint to the getter of the given property.
*
* @param string $property The name of the property
* @param string $method The name of the getter method
* @param Constraint $constraint The constraint
*
* @return $this
*/
public function addGetterMethodConstraint($property, $method, Constraint $constraint)
{
if (!isset($this->getters[$property])) {
$this->getters[$property] = new GetterMetadata($this->getClassName(), $property, $method);

$this->addPropertyMetadata($this->getters[$property]);
}

$constraint->addImplicitGroupName($this->getDefaultGroup());

$this->getters[$property]->addConstraint($constraint);

return $this;
}

/**
* @param string $property
* @param Constraint[] $constraints
Expand All @@ -334,6 +359,22 @@ public function addGetterConstraints($property, array $constraints)
return $this;
}

/**
* @param string $property
* @param string $method
* @param Constraint[] $constraints
*
* @return $this
*/
public function addGetterMethodConstraints($property, $method, array $constraints)
{
foreach ($constraints as $constraint) {
$this->addGetterMethodConstraint($property, $method, $constraint);
}

return $this;
}

/**
* Merges the constraints of the given metadata into this object.
*
Expand Down
33 changes: 19 additions & 14 deletions src/Symfony/Component/Validator/Mapping/GetterMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,30 @@ class GetterMetadata extends MemberMetadata
/**
* Constructor.
*
* @param string $class The class the getter is defined on
* @param string $property The property which the getter returns
* @param string $class The class the getter is defined on
* @param string $property The property which the getter returns
* @param string|null $method The method that is called to retrieve the value being validated (null for auto-detection)
*
* @throws ValidatorException
*/
public function __construct($class, $property)
public function __construct($class, $property, $method = null)
{
$getMethod = 'get'.ucfirst($property);
$isMethod = 'is'.ucfirst($property);
$hasMethod = 'has'.ucfirst($property);
if (null === $method) {
$getMethod = 'get'.ucfirst($property);
$isMethod = 'is'.ucfirst($property);
$hasMethod = 'has'.ucfirst($property);

if (method_exists($class, $getMethod)) {
$method = $getMethod;
} elseif (method_exists($class, $isMethod)) {
$method = $isMethod;
} elseif (method_exists($class, $hasMethod)) {
$method = $hasMethod;
} else {
throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
if (method_exists($class, $getMethod)) {
$method = $getMethod;
} elseif (method_exists($class, $isMethod)) {
$method = $isMethod;
} elseif (method_exists($class, $hasMethod)) {
$method = $hasMethod;
} else {
throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
}
} elseif (!method_exists($class, $method)) {
throw new ValidatorException(sprintf('The %s() method does not exist in class %s.', $method, $class));
}

parent::__construct($class, $method, $property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function loadClassMetadata(ClassMetadata $metadata)
$metadata->addConstraint($constraint);
} elseif ($constraint instanceof Constraint) {
if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
$metadata->addGetterConstraint(lcfirst($matches[2]), $constraint);
$metadata->addGetterMethodConstraint(lcfirst($matches[2]), $matches[0], $constraint);
} else {
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));
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Tests/Fixtures/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function getLastName()
return $this->lastName;
}

public function getValid()
{
}

/**
* @Assert\IsTrue
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testGetPropertyValueFromOverriddenPublicGetter()
public function testGetPropertyValueFromIsser()
{
$entity = new Entity();
$metadata = new GetterMetadata(self::CLASSNAME, 'valid');
$metadata = new GetterMetadata(self::CLASSNAME, 'valid', 'isValid');

$this->assertEquals('valid', $metadata->getPropertyValue($entity));
}
Expand All @@ -59,4 +59,13 @@ public function testGetPropertyValueFromHasser()

$this->assertEquals('permissions', $metadata->getPropertyValue($entity));
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ValidatorException
* @expectedExceptionMessage The hasLastName() method does not exist in class Symfony\Component\Validator\Tests\Fixtures\Entity.
*/
public function testUndefinedMethodNameThrowsException()
{
new GetterMetadata(self::CLASSNAME, 'lastName', 'hasLastName');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testLoadClassMetadata()
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new IsTrue());
$expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());

// load reflection class so that the comparison passes
Expand Down Expand Up @@ -138,7 +138,7 @@ public function testLoadClassMetadataAndMerge()
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new IsTrue());
$expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());

// load reflection class so that the comparison passes
Expand Down
0