Skip to content

Commit

Permalink
[Validator] forced all validation annotations to be in the validation…
Browse files Browse the repository at this point in the history
… namespace to avoid collisions, removed the need for the wrapping @Validation annotation

Before:

    /**
     * @Validation({@datetime()})
     */

After:

    /**
     * @Validation:DateTime()
     */

The @Validation:Validation() construct is not needed anymore (it is still supported
as this is useful when you have several annotations with the same class).

So, the above is equivalent to:

    /**
     * @Validation:Validation({@Validation:DateTime()})
     */
  • Loading branch information
fabpot committed Oct 2, 2010
1 parent 3a4d9cb commit 0fc8906
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
35 changes: 24 additions & 11 deletions src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Validator\Exception\MappingException;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Validator\Constraints\Validation;
use Symfony\Component\Validator\Constraint;

class AnnotationLoader implements LoaderInterface
{
Expand All @@ -22,30 +24,37 @@ class AnnotationLoader implements LoaderInterface
public function __construct()
{
$this->reader = new AnnotationReader();
$this->reader->setDefaultAnnotationNamespace('Symfony\Component\Validator\Constraints\\');
$this->reader->setAutoloadAnnotations(true);
$this->reader->setAnnotationNamespaceAlias('Symfony\Component\Validator\Constraints\\', 'validation');
}

/**
* {@inheritDoc}
*/
public function loadClassMetadata(ClassMetadata $metadata)
{
$annotClass = 'Symfony\Component\Validator\Constraints\Validation';
$reflClass = $metadata->getReflectionClass();
$loaded = false;

if ($annot = $this->reader->getClassAnnotation($reflClass, $annotClass)) {
foreach ($annot->constraints as $constraint) {
foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) {
if ($constraint instanceof Validation) {
foreach ($constraint->constraints as $constraint) {
$metadata->addConstraint($constraint);
}
} elseif ($constraint instanceof Constraint) {
$metadata->addConstraint($constraint);
}

$loaded = true;
}

foreach ($reflClass->getProperties() as $property) {
if ($annot = $this->reader->getPropertyAnnotation($property, $annotClass)) {
foreach ($annot->constraints as $constraint) {
foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
if ($constraint instanceof Validation) {
foreach ($constraint->constraints as $constraint) {
$metadata->addPropertyConstraint($property->getName(), $constraint);
}
} elseif ($constraint instanceof Constraint) {
$metadata->addPropertyConstraint($property->getName(), $constraint);
}

Expand All @@ -54,11 +63,15 @@ public function loadClassMetadata(ClassMetadata $metadata)
}

foreach ($reflClass->getMethods() as $method) {
if ($annot = $this->reader->getMethodAnnotation($method, $annotClass)) {
foreach ($annot->constraints as $constraint) {
// TODO: clean this up
$name = lcfirst(substr($method->getName(), 0, 3)=='get' ? substr($method->getName(), 3) : substr($method->getName(), 2));
foreach ($this->reader->getMethodAnnotations($method) as $constraint) {
// TODO: clean this up
$name = lcfirst(substr($method->getName(), 0, 3)=='get' ? substr($method->getName(), 3) : substr($method->getName(), 2));

if ($constraint instanceof Validation) {
foreach ($constraint->constraints as $constraint) {
$metadata->addGetterConstraint($name, $constraint);
}
} elseif ($constraint instanceof Constraint) {
$metadata->addGetterConstraint($name, $constraint);
}

Expand All @@ -68,4 +81,4 @@ public function loadClassMetadata(ClassMetadata $metadata)

return $loaded;
}
}
}
32 changes: 14 additions & 18 deletions tests/Symfony/Tests/Component/Validator/Fixtures/Entity.php
Expand Up @@ -6,25 +6,23 @@
require_once __DIR__.'/EntityInterface.php';

/**
* @Validation({
* @NotNull,
* @Symfony\Tests\Component\Validator\Fixtures\ConstraintA,
* @Min(3),
* @Choice({"A", "B"}),
* @All({@NotNull, @Min(3)}),
* @All(constraints={@NotNull, @Min(3)}),
* @Collection(fields={
* "foo" = {@NotNull, @Min(3)},
* "bar" = @Min(5)
* })
* @validation:NotNull
* @Symfony\Tests\Component\Validator\Fixtures\ConstraintA
* @validation:Min(3)
* @validation:Choice({"A", "B"})
* @validation:Validation({
* @validation:All({@validation:NotNull, @validation:Min(3)}),
* @validation:All(constraints={@validation:NotNull, @validation:Min(3)})
* })
* @validation:Collection(fields={
* "foo" = {@validation:NotNull, @validation:Min(3)},
* "bar" = @validation:Min(5)
* })
*/
class Entity extends EntityParent implements EntityInterface
{
/**
* @Validation({
* @Choice(choices={"A", "B"}, message="Must be one of %choices%")
* })
* @validation:Choice(choices={"A", "B"}, message="Must be one of %choices%")
*/
protected $firstName;

Expand All @@ -43,12 +41,10 @@ public function getInternal()
}

/**
* @Validation({
* @NotNull
* })
* @validation:NotNull
*/
public function getLastName()
{
return $this->lastName;
}
}
}

0 comments on commit 0fc8906

Please sign in to comment.