Merge branch '3.2'
* 3.2: [Serializer] Removed duplicate operation in camelcase denormalization [Validator] do not guess getter method names
This commit is contained in:
commit
5037c2adbd
@ -76,7 +76,7 @@ class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (null === $this->attributes || in_array($camelCasedName, $this->attributes)) {
|
if (null === $this->attributes || in_array($camelCasedName, $this->attributes)) {
|
||||||
return $this->lowerCamelCase ? lcfirst($camelCasedName) : $camelCasedName;
|
return $camelCasedName;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $propertyName;
|
return $propertyName;
|
||||||
|
@ -259,6 +259,7 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
|
|||||||
*
|
*
|
||||||
* @param string $property The name of the property
|
* @param string $property The name of the property
|
||||||
* @param Constraint $constraint The constraint
|
* @param Constraint $constraint The constraint
|
||||||
|
* @param string|null $method The method that is called to retrieve the value being validated (null for auto-detection)
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@ -277,6 +278,30 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
|
|||||||
return $this;
|
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 string $property
|
||||||
* @param Constraint[] $constraints
|
* @param Constraint[] $constraints
|
||||||
@ -292,6 +317,22 @@ class ClassMetadata extends GenericMetadata implements ClassMetadataInterface
|
|||||||
return $this;
|
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.
|
* Merges the constraints of the given metadata into this object.
|
||||||
*
|
*
|
||||||
|
@ -37,11 +37,13 @@ class GetterMetadata extends MemberMetadata
|
|||||||
*
|
*
|
||||||
* @param string $class The class the getter is defined on
|
* @param string $class The class the getter is defined on
|
||||||
* @param string $property The property which the getter returns
|
* @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
|
* @throws ValidatorException
|
||||||
*/
|
*/
|
||||||
public function __construct($class, $property)
|
public function __construct($class, $property, $method = null)
|
||||||
{
|
{
|
||||||
|
if (null === $method) {
|
||||||
$getMethod = 'get'.ucfirst($property);
|
$getMethod = 'get'.ucfirst($property);
|
||||||
$isMethod = 'is'.ucfirst($property);
|
$isMethod = 'is'.ucfirst($property);
|
||||||
$hasMethod = 'has'.ucfirst($property);
|
$hasMethod = 'has'.ucfirst($property);
|
||||||
@ -55,6 +57,9 @@ class GetterMetadata extends MemberMetadata
|
|||||||
} else {
|
} else {
|
||||||
throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
|
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);
|
parent::__construct($class, $method, $property);
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ class AnnotationLoader implements LoaderInterface
|
|||||||
$metadata->addConstraint($constraint);
|
$metadata->addConstraint($constraint);
|
||||||
} elseif ($constraint instanceof Constraint) {
|
} elseif ($constraint instanceof Constraint) {
|
||||||
if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
|
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 {
|
} 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));
|
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));
|
||||||
}
|
}
|
||||||
|
@ -71,6 +71,10 @@ class Entity extends EntityParent implements EntityInterfaceB
|
|||||||
return $this->lastName;
|
return $this->lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getValid()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Assert\IsTrue
|
* @Assert\IsTrue
|
||||||
*/
|
*/
|
||||||
|
@ -48,7 +48,7 @@ class GetterMetadataTest extends TestCase
|
|||||||
public function testGetPropertyValueFromIsser()
|
public function testGetPropertyValueFromIsser()
|
||||||
{
|
{
|
||||||
$entity = new Entity();
|
$entity = new Entity();
|
||||||
$metadata = new GetterMetadata(self::CLASSNAME, 'valid');
|
$metadata = new GetterMetadata(self::CLASSNAME, 'valid', 'isValid');
|
||||||
|
|
||||||
$this->assertEquals('valid', $metadata->getPropertyValue($entity));
|
$this->assertEquals('valid', $metadata->getPropertyValue($entity));
|
||||||
}
|
}
|
||||||
@ -60,4 +60,13 @@ class GetterMetadataTest extends TestCase
|
|||||||
|
|
||||||
$this->assertEquals('permissions', $metadata->getPropertyValue($entity));
|
$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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ class AnnotationLoaderTest extends TestCase
|
|||||||
$expected->addPropertyConstraint('childA', new Valid());
|
$expected->addPropertyConstraint('childA', new Valid());
|
||||||
$expected->addPropertyConstraint('childB', new Valid());
|
$expected->addPropertyConstraint('childB', new Valid());
|
||||||
$expected->addGetterConstraint('lastName', new NotNull());
|
$expected->addGetterConstraint('lastName', new NotNull());
|
||||||
$expected->addGetterConstraint('valid', new IsTrue());
|
$expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
|
||||||
$expected->addGetterConstraint('permissions', new IsTrue());
|
$expected->addGetterConstraint('permissions', new IsTrue());
|
||||||
|
|
||||||
// load reflection class so that the comparison passes
|
// load reflection class so that the comparison passes
|
||||||
@ -144,7 +144,7 @@ class AnnotationLoaderTest extends TestCase
|
|||||||
$expected->addPropertyConstraint('childA', new Valid());
|
$expected->addPropertyConstraint('childA', new Valid());
|
||||||
$expected->addPropertyConstraint('childB', new Valid());
|
$expected->addPropertyConstraint('childB', new Valid());
|
||||||
$expected->addGetterConstraint('lastName', new NotNull());
|
$expected->addGetterConstraint('lastName', new NotNull());
|
||||||
$expected->addGetterConstraint('valid', new IsTrue());
|
$expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
|
||||||
$expected->addGetterConstraint('permissions', new IsTrue());
|
$expected->addGetterConstraint('permissions', new IsTrue());
|
||||||
|
|
||||||
// load reflection class so that the comparison passes
|
// load reflection class so that the comparison passes
|
||||||
|
Reference in New Issue
Block a user