You are here

public function AnnotationLoader::loadClassMetadata in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/validator/Mapping/Loader/AnnotationLoader.php \Symfony\Component\Validator\Mapping\Loader\AnnotationLoader::loadClassMetadata()
  2. 8 vendor/symfony/serializer/Mapping/Loader/AnnotationLoader.php \Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader::loadClassMetadata()
Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Mapping/Loader/AnnotationLoader.php \Symfony\Component\Validator\Mapping\Loader\AnnotationLoader::loadClassMetadata()

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

vendor/symfony/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;
}