4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Mapping\Loader;
14 use Symfony\Component\Validator\Mapping\ClassMetadata;
15 use Symfony\Component\Yaml\Exception\ParseException;
16 use Symfony\Component\Yaml\Parser as YamlParser;
19 * Loads validation metadata from a YAML file.
21 * @author Bernhard Schussek <bschussek@gmail.com>
23 class YamlFileLoader extends FileLoader
26 * An array of YAML class descriptions.
30 protected $classes = null;
33 * Caches the used YAML parser.
42 public function loadClassMetadata(ClassMetadata $metadata)
44 if (null === $this->classes) {
45 $this->loadClassesFromYaml();
48 if (isset($this->classes[$metadata->getClassName()])) {
49 $classDescription = $this->classes[$metadata->getClassName()];
51 $this->loadClassMetadataFromYaml($metadata, $classDescription);
60 * Return the names of the classes mapped in this file.
62 * @return string[] The classes names
64 public function getMappedClasses()
66 if (null === $this->classes) {
67 $this->loadClassesFromYaml();
70 return array_keys($this->classes);
74 * Parses a collection of YAML nodes.
76 * @param array $nodes The YAML nodes
78 * @return array An array of values or Constraint instances
80 protected function parseNodes(array $nodes)
84 foreach ($nodes as $name => $childNodes) {
85 if (is_numeric($name) && is_array($childNodes) && 1 === count($childNodes)) {
86 $options = current($childNodes);
88 if (is_array($options)) {
89 $options = $this->parseNodes($options);
92 $values[] = $this->newConstraint(key($childNodes), $options);
94 if (is_array($childNodes)) {
95 $childNodes = $this->parseNodes($childNodes);
98 $values[$name] = $childNodes;
106 * Loads the YAML class descriptions from the given file.
108 * @param string $path The path of the YAML file
110 * @return array The class descriptions
112 * @throws \InvalidArgumentException If the file could not be loaded or did
113 * not contain a YAML array
115 private function parseFile($path)
118 $classes = $this->yamlParser->parse(file_get_contents($path));
119 } catch (ParseException $e) {
120 throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
124 if (null === $classes) {
129 if (!is_array($classes)) {
130 throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file));
136 private function loadClassesFromYaml()
138 if (null === $this->yamlParser) {
139 $this->yamlParser = new YamlParser();
142 $this->classes = $this->parseFile($this->file);
144 if (isset($this->classes['namespaces'])) {
145 foreach ($this->classes['namespaces'] as $alias => $namespace) {
146 $this->addNamespaceAlias($alias, $namespace);
149 unset($this->classes['namespaces']);
153 private function loadClassMetadataFromYaml(ClassMetadata $metadata, array $classDescription)
155 if (isset($classDescription['group_sequence_provider'])) {
156 $metadata->setGroupSequenceProvider(
157 (bool) $classDescription['group_sequence_provider']
161 if (isset($classDescription['group_sequence'])) {
162 $metadata->setGroupSequence($classDescription['group_sequence']);
165 if (isset($classDescription['constraints']) && is_array($classDescription['constraints'])) {
166 foreach ($this->parseNodes($classDescription['constraints']) as $constraint) {
167 $metadata->addConstraint($constraint);
171 if (isset($classDescription['properties']) && is_array($classDescription['properties'])) {
172 foreach ($classDescription['properties'] as $property => $constraints) {
173 if (null !== $constraints) {
174 foreach ($this->parseNodes($constraints) as $constraint) {
175 $metadata->addPropertyConstraint($property, $constraint);
181 if (isset($classDescription['getters']) && is_array($classDescription['getters'])) {
182 foreach ($classDescription['getters'] as $getter => $constraints) {
183 if (null !== $constraints) {
184 foreach ($this->parseNodes($constraints) as $constraint) {
185 $metadata->addGetterConstraint($getter, $constraint);