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\Config\Util\XmlUtils;
15 use Symfony\Component\Validator\Exception\MappingException;
16 use Symfony\Component\Validator\Mapping\ClassMetadata;
19 * Loads validation metadata from an XML file.
21 * @author Bernhard Schussek <bschussek@gmail.com>
23 class XmlFileLoader extends FileLoader
26 * The XML nodes of the mapping file.
28 * @var \SimpleXMLElement[]|null
35 public function loadClassMetadata(ClassMetadata $metadata)
37 if (null === $this->classes) {
38 $this->loadClassesFromXml();
41 if (isset($this->classes[$metadata->getClassName()])) {
42 $classDescription = $this->classes[$metadata->getClassName()];
44 $this->loadClassMetadataFromXml($metadata, $classDescription);
53 * Return the names of the classes mapped in this file.
55 * @return string[] The classes names
57 public function getMappedClasses()
59 if (null === $this->classes) {
60 $this->loadClassesFromXml();
63 return array_keys($this->classes);
67 * Parses a collection of "constraint" XML nodes.
69 * @param \SimpleXMLElement $nodes The XML nodes
71 * @return array The Constraint instances
73 protected function parseConstraints(\SimpleXMLElement $nodes)
75 $constraints = array();
77 foreach ($nodes as $node) {
78 if (count($node) > 0) {
79 if (count($node->value) > 0) {
80 $options = $this->parseValues($node->value);
81 } elseif (count($node->constraint) > 0) {
82 $options = $this->parseConstraints($node->constraint);
83 } elseif (count($node->option) > 0) {
84 $options = $this->parseOptions($node->option);
88 } elseif (strlen((string) $node) > 0) {
89 $options = XmlUtils::phpize(trim($node));
94 $constraints[] = $this->newConstraint((string) $node['name'], $options);
101 * Parses a collection of "value" XML nodes.
103 * @param \SimpleXMLElement $nodes The XML nodes
105 * @return array The values
107 protected function parseValues(\SimpleXMLElement $nodes)
111 foreach ($nodes as $node) {
112 if (count($node) > 0) {
113 if (count($node->value) > 0) {
114 $value = $this->parseValues($node->value);
115 } elseif (count($node->constraint) > 0) {
116 $value = $this->parseConstraints($node->constraint);
121 $value = trim($node);
124 if (isset($node['key'])) {
125 $values[(string) $node['key']] = $value;
135 * Parses a collection of "option" XML nodes.
137 * @param \SimpleXMLElement $nodes The XML nodes
139 * @return array The options
141 protected function parseOptions(\SimpleXMLElement $nodes)
145 foreach ($nodes as $node) {
146 if (count($node) > 0) {
147 if (count($node->value) > 0) {
148 $value = $this->parseValues($node->value);
149 } elseif (count($node->constraint) > 0) {
150 $value = $this->parseConstraints($node->constraint);
155 $value = XmlUtils::phpize($node);
156 if (is_string($value)) {
157 $value = trim($value);
161 $options[(string) $node['name']] = $value;
168 * Loads the XML class descriptions from the given file.
170 * @param string $path The path of the XML file
172 * @return \SimpleXMLElement The class descriptions
174 * @throws MappingException If the file could not be loaded
176 protected function parseFile($path)
179 $dom = XmlUtils::loadFile($path, __DIR__.'/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd');
180 } catch (\Exception $e) {
181 throw new MappingException($e->getMessage(), $e->getCode(), $e);
184 return simplexml_import_dom($dom);
187 private function loadClassesFromXml()
189 // This method may throw an exception. Do not modify the class'
190 // state before it completes
191 $xml = $this->parseFile($this->file);
193 $this->classes = array();
195 foreach ($xml->namespace as $namespace) {
196 $this->addNamespaceAlias((string) $namespace['prefix'], trim((string) $namespace));
199 foreach ($xml->class as $class) {
200 $this->classes[(string) $class['name']] = $class;
204 private function loadClassMetadataFromXml(ClassMetadata $metadata, \SimpleXMLElement $classDescription)
206 if (count($classDescription->{'group-sequence-provider'}) > 0) {
207 $metadata->setGroupSequenceProvider(true);
210 foreach ($classDescription->{'group-sequence'} as $groupSequence) {
211 if (count($groupSequence->value) > 0) {
212 $metadata->setGroupSequence($this->parseValues($groupSequence[0]->value));
216 foreach ($this->parseConstraints($classDescription->constraint) as $constraint) {
217 $metadata->addConstraint($constraint);
220 foreach ($classDescription->property as $property) {
221 foreach ($this->parseConstraints($property->constraint) as $constraint) {
222 $metadata->addPropertyConstraint((string) $property['name'], $constraint);
226 foreach ($classDescription->getter as $getter) {
227 foreach ($this->parseConstraints($getter->constraint) as $constraint) {
228 $metadata->addGetterConstraint((string) $getter['property'], $constraint);