You are here

public function AnnotationLoader::loadClassMetadata in Plug 7

Loads validation metadata into a {@link ClassMetadata} instance.

Parameters

ClassMetadata $metadata The metadata to load:

Return value

bool Whether the loader succeeded

Overrides LoaderInterface::loadClassMetadata

File

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

Class

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

Namespace

Symfony\Component\Validator\Mapping\Loader

Code

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;
}