Skip to content

Commit

Permalink
feature #11917 [Validator] Add ClassMetadata plural methods for convi…
Browse files Browse the repository at this point in the history
…nience (jakzal)

This PR was squashed before being merged into the 2.6-dev branch (closes #11917).

Discussion
----------

[Validator] Add ClassMetadata plural methods for convinience

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #4143
| License       | MIT
| Doc PR        | -

I realised there's no specific place to document this methods, as the code examples always include all the formats. I think it's enough if IDE autocompletes these methods.

Commits
-------

0fd6769 [Validator] Add ClassMetadata plural methods for convinience
  • Loading branch information
webmozart committed Sep 15, 2014
2 parents e86fe91 + 0fd6769 commit 05c7207
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Expand Up @@ -182,7 +182,7 @@ public function __sleep()
'members',
'name',
'properties',
'defaultGroup'
'defaultGroup',
));
}

Expand Down Expand Up @@ -276,6 +276,21 @@ public function addPropertyConstraint($property, Constraint $constraint)
return $this;
}

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

return $this;
}

/**
* Adds a constraint to the getter of the given property.
*
Expand All @@ -302,6 +317,21 @@ public function addGetterConstraint($property, Constraint $constraint)
return $this;
}

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

return $this;
}

/**
* Merges the constraints of the given metadata into this object.
*
Expand Down
Expand Up @@ -58,6 +58,55 @@ public function testAddPropertyConstraints()
$this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
}

public function testAddMultiplePropertyConstraints()
{
$this->metadata->addPropertyConstraints('lastName', array(new ConstraintA(), new ConstraintB()));

$constraints = array(
new ConstraintA(array('groups' => array('Default', 'Entity'))),
new ConstraintB(array('groups' => array('Default', 'Entity'))),
);

$properties = $this->metadata->getPropertyMetadata('lastName');

$this->assertCount(1, $properties);
$this->assertEquals('lastName', $properties[0]->getName());
$this->assertEquals($constraints, $properties[0]->getConstraints());
}

public function testAddGetterConstraints()
{
$this->metadata->addGetterConstraint('lastName', new ConstraintA());
$this->metadata->addGetterConstraint('lastName', new ConstraintB());

$constraints = array(
new ConstraintA(array('groups' => array('Default', 'Entity'))),
new ConstraintB(array('groups' => array('Default', 'Entity'))),
);

$properties = $this->metadata->getPropertyMetadata('lastName');

$this->assertCount(1, $properties);
$this->assertEquals('getLastName', $properties[0]->getName());
$this->assertEquals($constraints, $properties[0]->getConstraints());
}

public function testAddMultipleGetterConstraints()
{
$this->metadata->addGetterConstraints('lastName', array(new ConstraintA(), new ConstraintB()));

$constraints = array(
new ConstraintA(array('groups' => array('Default', 'Entity'))),
new ConstraintB(array('groups' => array('Default', 'Entity'))),
);

$properties = $this->metadata->getPropertyMetadata('lastName');

$this->assertCount(1, $properties);
$this->assertEquals('getLastName', $properties[0]->getName());
$this->assertEquals($constraints, $properties[0]->getConstraints());
}

public function testMergeConstraintsMergesClassConstraints()
{
$parent = new ClassMetadata(self::PARENTCLASS);
Expand Down

0 comments on commit 05c7207

Please sign in to comment.