You are here

class AnnotationLoader in Plug 7

Loads validation metadata using a Doctrine annotation {@link Reader}.

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

Expanded class hierarchy of AnnotationLoader

2 files declare their use of AnnotationLoader
AnnotationLoaderTest.php in lib/Symfony/validator/Symfony/Component/Validator/Tests/Mapping/Loader/AnnotationLoaderTest.php
ValidatorBuilder.php in lib/Symfony/validator/Symfony/Component/Validator/ValidatorBuilder.php

File

lib/Symfony/validator/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php, line 27

Namespace

Symfony\Component\Validator\Mapping\Loader
View source
class AnnotationLoader implements LoaderInterface {

  /**
   * @var Reader
   */
  protected $reader;
  public function __construct(Reader $reader) {
    $this->reader = $reader;
  }

  /**
   * {@inheritdoc}
   */
  public function loadClassMetadata(ClassMetadata $metadata) {
    $reflClass = $metadata
      ->getReflectionClass();
    $className = $reflClass->name;
    $success = false;
    foreach ($this->reader
      ->getClassAnnotations($reflClass) as $constraint) {
      if ($constraint instanceof GroupSequence) {
        $metadata
          ->setGroupSequence($constraint->groups);
      }
      elseif ($constraint instanceof GroupSequenceProvider) {
        $metadata
          ->setGroupSequenceProvider(true);
      }
      elseif ($constraint instanceof Constraint) {
        $metadata
          ->addConstraint($constraint);
      }
      $success = true;
    }
    foreach ($reflClass
      ->getProperties() as $property) {
      if ($property
        ->getDeclaringClass()->name === $className) {
        foreach ($this->reader
          ->getPropertyAnnotations($property) as $constraint) {
          if ($constraint instanceof Constraint) {
            $metadata
              ->addPropertyConstraint($property->name, $constraint);
          }
          $success = true;
        }
      }
    }
    foreach ($reflClass
      ->getMethods() as $method) {
      if ($method
        ->getDeclaringClass()->name === $className) {
        foreach ($this->reader
          ->getMethodAnnotations($method) as $constraint) {
          if ($constraint instanceof Callback) {
            $constraint->callback = $method
              ->getName();
            $constraint->methods = null;
            $metadata
              ->addConstraint($constraint);
          }
          elseif ($constraint instanceof Constraint) {
            if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
              $metadata
                ->addGetterConstraint(lcfirst($matches[2]), $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));
            }
          }
          $success = true;
        }
      }
    }
    return $success;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnnotationLoader::$reader protected property
AnnotationLoader::loadClassMetadata public function Loads validation metadata into a {@link ClassMetadata} instance. Overrides LoaderInterface::loadClassMetadata
AnnotationLoader::__construct public function