You are here

class YamlFileLoader in Plug 7

Same name in this branch
  1. 7 lib/Symfony/translation/Loader/YamlFileLoader.php \Symfony\Component\Translation\Loader\YamlFileLoader
  2. 7 lib/Symfony/validator/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php \Symfony\Component\Validator\Mapping\Loader\YamlFileLoader

Loads validation metadata from a YAML file.

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

Expanded class hierarchy of YamlFileLoader

2 files declare their use of YamlFileLoader
ValidatorBuilder.php in lib/Symfony/validator/Symfony/Component/Validator/ValidatorBuilder.php
YamlFileLoaderTest.php in lib/Symfony/validator/Symfony/Component/Validator/Tests/Mapping/Loader/YamlFileLoaderTest.php
1 string reference to 'YamlFileLoader'
TranslatorTest::getTransFileTests in lib/Symfony/translation/Tests/TranslatorTest.php

File

lib/Symfony/validator/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php, line 22

Namespace

Symfony\Component\Validator\Mapping\Loader
View source
class YamlFileLoader extends FileLoader {

  /**
   * An array of YAML class descriptions.
   *
   * @var array
   */
  protected $classes = null;

  /**
   * Caches the used YAML parser.
   *
   * @var YamlParser
   */
  private $yamlParser;

  /**
   * {@inheritdoc}
   */
  public function loadClassMetadata(ClassMetadata $metadata) {
    if (null === $this->classes) {
      if (null === $this->yamlParser) {
        $this->yamlParser = new YamlParser();
      }

      // This method may throw an exception. Do not modify the class'
      // state before it completes
      if (false === ($classes = $this
        ->parseFile($this->file))) {
        return false;
      }
      $this->classes = $classes;
      if (isset($this->classes['namespaces'])) {
        foreach ($this->classes['namespaces'] as $alias => $namespace) {
          $this
            ->addNamespaceAlias($alias, $namespace);
        }
        unset($this->classes['namespaces']);
      }
    }
    if (isset($this->classes[$metadata
      ->getClassName()])) {
      $classDescription = $this->classes[$metadata
        ->getClassName()];
      $this
        ->loadClassMetadataFromYaml($metadata, $classDescription);
      return true;
    }
    return false;
  }

  /**
   * Parses a collection of YAML nodes.
   *
   * @param array $nodes The YAML nodes
   *
   * @return array An array of values or Constraint instances
   */
  protected function parseNodes(array $nodes) {
    $values = array();
    foreach ($nodes as $name => $childNodes) {
      if (is_numeric($name) && is_array($childNodes) && 1 === count($childNodes)) {
        $options = current($childNodes);
        if (is_array($options)) {
          $options = $this
            ->parseNodes($options);
        }
        $values[] = $this
          ->newConstraint(key($childNodes), $options);
      }
      else {
        if (is_array($childNodes)) {
          $childNodes = $this
            ->parseNodes($childNodes);
        }
        $values[$name] = $childNodes;
      }
    }
    return $values;
  }

  /**
   * Loads the YAML class descriptions from the given file.
   *
   * @param string $path The path of the YAML file
   *
   * @return array|null The class descriptions or null, if the file was empty
   *
   * @throws \InvalidArgumentException If the file could not be loaded or did
   *                                   not contain a YAML array
   */
  private function parseFile($path) {
    $classes = $this->yamlParser
      ->parse(file_get_contents($path));

    // empty file
    if (null === $classes) {
      return;
    }

    // not an array
    if (!is_array($classes)) {
      throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file));
    }
    return $classes;
  }

  /**
   * Loads the validation metadata from the given YAML class description.
   *
   * @param ClassMetadata $metadata         The metadata to load
   * @param array         $classDescription The YAML class description
   */
  private function loadClassMetadataFromYaml(ClassMetadata $metadata, array $classDescription) {
    if (isset($classDescription['group_sequence_provider'])) {
      $metadata
        ->setGroupSequenceProvider((bool) $classDescription['group_sequence_provider']);
    }
    if (isset($classDescription['group_sequence'])) {
      $metadata
        ->setGroupSequence($classDescription['group_sequence']);
    }
    if (isset($classDescription['constraints']) && is_array($classDescription['constraints'])) {
      foreach ($this
        ->parseNodes($classDescription['constraints']) as $constraint) {
        $metadata
          ->addConstraint($constraint);
      }
    }
    if (isset($classDescription['properties']) && is_array($classDescription['properties'])) {
      foreach ($classDescription['properties'] as $property => $constraints) {
        if (null !== $constraints) {
          foreach ($this
            ->parseNodes($constraints) as $constraint) {
            $metadata
              ->addPropertyConstraint($property, $constraint);
          }
        }
      }
    }
    if (isset($classDescription['getters']) && is_array($classDescription['getters'])) {
      foreach ($classDescription['getters'] as $getter => $constraints) {
        if (null !== $constraints) {
          foreach ($this
            ->parseNodes($constraints) as $constraint) {
            $metadata
              ->addGetterConstraint($getter, $constraint);
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractLoader::$namespaces protected property
AbstractLoader::addNamespaceAlias protected function Adds a namespace alias.
AbstractLoader::DEFAULT_NAMESPACE constant The namespace to load constraints from by default.
AbstractLoader::newConstraint protected function Creates a new constraint instance for the given constraint name.
FileLoader::$file protected property The file to load.
FileLoader::__construct public function Creates a new loader.
YamlFileLoader::$classes protected property An array of YAML class descriptions.
YamlFileLoader::$yamlParser private property Caches the used YAML parser.
YamlFileLoader::loadClassMetadata public function Loads validation metadata into a {@link ClassMetadata} instance. Overrides LoaderInterface::loadClassMetadata
YamlFileLoader::loadClassMetadataFromYaml private function Loads the validation metadata from the given YAML class description.
YamlFileLoader::parseFile private function Loads the YAML class descriptions from the given file.
YamlFileLoader::parseNodes protected function Parses a collection of YAML nodes.