Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Target aware class constraints #16978

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

Closed
Closed
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.1.0
-----

* added Target Aware Constraints and its support in loaders for class constraints

2.8.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* This interface is to be implemented by constraints that need to be
* aware of the exact target they have been declared on.
*
* This interface only makes sense for class constraints. Which could be
* attached to multiple classes because of class inheritance.
*
* @since 3.1
*
* @author Mathieu Lemoine <[email protected]>
*/
interface TargetAwareConstraintInterface
{
/*
* Since constraints are implemented using public properties,
* the interface is intended as a tag and declare no method.
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* This trait is intended as a helper for implementing TargetAwareConstraintInterface.
*
* Since the interface interface only makes sense for class constraints,
* the default targets is set to class constraint.
*
* @since 3.1
*
* @author Mathieu Lemoine <[email protected]>
*/
trait TargetAwareConstraintTrait
{
public $target;

/**
* {@inheritdoc}
*/
public function getTargets()
{
return Constraint::CLASS_CONSTRAINT;
Copy link
Contributor

Choose a reason for hiding this comment

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

Just wondering: Why don't you think it could be useful for property constraints ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see target awareness like a scope widening feature.

So called class constraints are lexically applied to the class but usually have the validated instance as their scope (i.e. The constraint is checking that the current instance respects such and such invariant). A TA class constraint is using the class as its scope (i.e. The constraint is checking that the current instance does not break such and such invariant among all the other instances of the class).

Following the same reasoning, property constraints have the validated property as their scope (i.e. The constraint is checking that the current property respects such and such invariant). Widening the scope of such constraint to the current instance is already possible using a class constraint (with a custom path).

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\GroupSequenceProvider;
use Symfony\Component\Validator\Constraints\TargetAwareConstraintInterface;
use Symfony\Component\Validator\Exception\MappingException;
use Symfony\Component\Validator\Mapping\ClassMetadata;

Expand Down Expand Up @@ -51,6 +52,10 @@ public function loadClassMetadata(ClassMetadata $metadata)
} elseif ($constraint instanceof GroupSequenceProvider) {
$metadata->setGroupSequenceProvider(true);
} elseif ($constraint instanceof Constraint) {
if ($constraint instanceof TargetAwareConstraintInterface) {
$constraint->target = $metadata->getClassName();
}

$metadata->addConstraint($constraint);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Mapping\Loader;

use Symfony\Component\Config\Util\XmlUtils;
use Symfony\Component\Validator\Constraints\TargetAwareConstraintInterface;
use Symfony\Component\Validator\Exception\MappingException;
use Symfony\Component\Validator\Mapping\ClassMetadata;

Expand Down Expand Up @@ -201,6 +202,10 @@ private function loadClassMetadataFromXml(ClassMetadata $metadata, $classDescrip
}

foreach ($this->parseConstraints($classDescription->constraint) as $constraint) {
if ($constraint instanceof TargetAwareConstraintInterface) {
$constraint->target = $metadata->getClassName();
}

$metadata->addConstraint($constraint);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Mapping\Loader;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\TargetAwareConstraintInterface;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser;

Expand Down Expand Up @@ -157,6 +158,10 @@ private function loadClassMetadataFromYaml(ClassMetadata $metadata, array $class

if (isset($classDescription['constraints']) && is_array($classDescription['constraints'])) {
foreach ($this->parseNodes($classDescription['constraints']) as $constraint) {
if ($constraint instanceof TargetAwareConstraintInterface) {
$constraint->target = $metadata->getClassName();
}

$metadata->addConstraint($constraint);
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Validator/Tests/Fixtures/ConstraintD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Fixtures;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\TargetAwareConstraintInterface;
use Symfony\Component\Validator\Constraints\TargetAwareConstraintTrait;

/** @Annotation */
class ConstraintD extends Constraint implements TargetAwareConstraintInterface
{
use TargetAwareConstraintTrait;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Symfony\Component\Validator\Constraints\NotNull;

/**
* @Symfony\Component\Validator\Tests\Fixtures\ConstraintD
*/
class EntityParent
{
protected $firstName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintD;

class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -89,6 +90,7 @@ public function testLoadParentClassMetadata()
$loader->loadClassMetadata($parent_metadata);

$expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$expected_parent->addConstraint(new ConstraintD(array('target' => 'Symfony\Component\Validator\Tests\Fixtures\EntityParent')));
$expected_parent->addPropertyConstraint('other', new NotNull());
$expected_parent->getReflectionClass();

Expand All @@ -113,6 +115,7 @@ public function testLoadClassMetadataAndMerge()
$loader->loadClassMetadata($metadata);

$expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$expected_parent->addConstraint(new ConstraintD(array('target' => 'Symfony\Component\Validator\Tests\Fixtures\EntityParent')));
$expected_parent->addPropertyConstraint('other', new NotNull());
$expected_parent->getReflectionClass();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintD;

class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -77,6 +78,74 @@ public function testLoadClassMetadata()
$this->assertEquals($expected, $metadata);
}

/**
* Test MetaData merge with parent annotation.
*/
public function testLoadParentClassMetadata()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');

// Load Parent MetaData
$parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$loader->loadClassMetadata($parent_metadata);

$expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$expected_parent->addConstraint(new ConstraintD(array('target' => 'Symfony\Component\Validator\Tests\Fixtures\EntityParent')));
$expected_parent->addPropertyConstraint('other', new NotNull());

$this->assertEquals($expected_parent, $parent_metadata);
}
/**
* Test MetaData merge with parent annotation.
*/
public function testLoadClassMetadataAndMerge()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');

// Load Parent MetaData
$parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$loader->loadClassMetadata($parent_metadata);

$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');

// Merge parent metaData.
$metadata->mergeConstraints($parent_metadata);

$loader->loadClassMetadata($metadata);

$expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$expected_parent->addConstraint(new ConstraintD(array('target' => 'Symfony\Component\Validator\Tests\Fixtures\EntityParent')));
$expected_parent->addPropertyConstraint('other', new NotNull());

$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$expected->mergeConstraints($expected_parent);

$expected->setGroupSequence(array('Foo', 'Entity'));
$expected->addConstraint(new ConstraintA());
$expected->addConstraint(new ConstraintB());
$expected->addConstraint(new Callback('validateMe'));
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
$expected->addPropertyConstraint('firstName', new NotNull());
$expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
$expected->addPropertyConstraint('firstName', new Choice(array('A', 'B')));
$expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
$expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
$expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
'foo' => array(new NotNull(), new Range(array('min' => 3))),
'bar' => array(new Range(array('min' => 5))),
))));
$expected->addPropertyConstraint('firstName', new Choice(array(
'message' => 'Must be one of %choices%',
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());

$this->assertEquals($expected, $metadata);
}

public function testLoadClassMetadataWithNonStrings()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping-non-strings.xml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintD;

class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -119,6 +120,74 @@ public function testLoadClassMetadata()
$this->assertEquals($expected, $metadata);
}

/**
* Test MetaData merge with parent annotation.
*/
public function testLoadParentClassMetadata()
{
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');

// Load Parent MetaData
$parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$loader->loadClassMetadata($parent_metadata);

$expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$expected_parent->addConstraint(new ConstraintD(array('target' => 'Symfony\Component\Validator\Tests\Fixtures\EntityParent')));
$expected_parent->addPropertyConstraint('other', new NotNull());

$this->assertEquals($expected_parent, $parent_metadata);
}
/**
* Test MetaData merge with parent annotation.
*/
public function testLoadClassMetadataAndMerge()
{
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');

// Load Parent MetaData
$parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$loader->loadClassMetadata($parent_metadata);

$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');

// Merge parent metaData.
$metadata->mergeConstraints($parent_metadata);

$loader->loadClassMetadata($metadata);

$expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$expected_parent->addConstraint(new ConstraintD(array('target' => 'Symfony\Component\Validator\Tests\Fixtures\EntityParent')));
$expected_parent->addPropertyConstraint('other', new NotNull());

$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$expected->mergeConstraints($expected_parent);

$expected->setGroupSequence(array('Foo', 'Entity'));
$expected->addConstraint(new ConstraintA());
$expected->addConstraint(new ConstraintB());
$expected->addConstraint(new Callback('validateMe'));
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
$expected->addPropertyConstraint('firstName', new NotNull());
$expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
$expected->addPropertyConstraint('firstName', new Choice(array('A', 'B')));
$expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
$expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
$expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
'foo' => array(new NotNull(), new Range(array('min' => 3))),
'bar' => array(new Range(array('min' => 5))),
))));
$expected->addPropertyConstraint('firstName', new Choice(array(
'message' => 'Must be one of %choices%',
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());

$this->assertEquals($expected, $metadata);
}

public function testLoadGroupSequenceProvider()
{
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

<namespace prefix="custom">Symfony\Component\Validator\Tests\Fixtures\</namespace>

<class name="Symfony\Component\Validator\Tests\Fixtures\EntityParent">
<!-- Target aware constraint -->
<constraint name="Symfony\Component\Validator\Tests\Fixtures\ConstraintD" />

<property name="other">
<constraint name="NotNull" />
</property>
</class>

<class name="Symfony\Component\Validator\Tests\Fixtures\Entity">

<group-sequence>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
namespaces:
custom: Symfony\Component\Validator\Tests\Fixtures\

Symfony\Component\Validator\Tests\Fixtures\EntityParent:
constraints:
# Target aware constraint
- Symfony\Component\Validator\Tests\Fixtures\ConstraintD: ~
properties:
other:
- NotNull: ~

Symfony\Component\Validator\Tests\Fixtures\Entity:
group_sequence:
- Foo
Expand Down