You are here

class StaticMethodLoader in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/Mapping/Loader/StaticMethodLoader.php \Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader

Loads validation metadata by calling a static method on the loaded class.

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

Expanded class hierarchy of StaticMethodLoader

2 files declare their use of StaticMethodLoader
StaticMethodLoaderTest.php in vendor/symfony/validator/Tests/Mapping/Loader/StaticMethodLoaderTest.php
ValidatorBuilder.php in vendor/symfony/validator/ValidatorBuilder.php

File

vendor/symfony/validator/Mapping/Loader/StaticMethodLoader.php, line 22

Namespace

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

  /**
   * The name of the method to call.
   *
   * @var string
   */
  protected $methodName;

  /**
   * Creates a new loader.
   *
   * @param string $methodName The name of the static method to call
   */
  public function __construct($methodName = 'loadValidatorMetadata') {
    $this->methodName = $methodName;
  }

  /**
   * {@inheritdoc}
   */
  public function loadClassMetadata(ClassMetadata $metadata) {

    /** @var \ReflectionClass $reflClass */
    $reflClass = $metadata
      ->getReflectionClass();
    if (!$reflClass
      ->isInterface() && $reflClass
      ->hasMethod($this->methodName)) {
      $reflMethod = $reflClass
        ->getMethod($this->methodName);
      if ($reflMethod
        ->isAbstract()) {
        return false;
      }
      if (!$reflMethod
        ->isStatic()) {
        throw new MappingException(sprintf('The method %s::%s should be static', $reflClass->name, $this->methodName));
      }
      if ($reflMethod
        ->getDeclaringClass()->name != $reflClass->name) {
        return false;
      }
      $reflMethod
        ->invoke(null, $metadata);
      return true;
    }
    return false;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StaticMethodLoader::$methodName protected property The name of the method to call.
StaticMethodLoader::loadClassMetadata public function Loads validation metadata into a {@link ClassMetadata} instance. Overrides LoaderInterface::loadClassMetadata
StaticMethodLoader::__construct public function Creates a new loader.