YamlFileLoader.php in Zircon Profile 8.0
File
vendor/symfony/validator/Mapping/Loader/YamlFileLoader.php
View source
<?php
namespace Symfony\Component\Validator\Mapping\Loader;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser;
class YamlFileLoader extends FileLoader {
protected $classes = null;
private $yamlParser;
public function loadClassMetadata(ClassMetadata $metadata) {
if (null === $this->classes) {
if (null === $this->yamlParser) {
$this->yamlParser = new YamlParser();
}
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;
}
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;
}
private function parseFile($path) {
try {
$classes = $this->yamlParser
->parse(file_get_contents($path));
} catch (ParseException $e) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
}
if (null === $classes) {
return;
}
if (!is_array($classes)) {
throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file));
}
return $classes;
}
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);
}
}
}
}
}
}