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 method may throw an exception. Do not modify the class'
39 // state before it completes
40 $xml = $this->parseFile($this->file);
42 $this->classes = array();
44 foreach ($xml->namespace as $namespace) {
45 $this->addNamespaceAlias((string) $namespace['prefix'], trim((string) $namespace));
48 foreach ($xml->class as $class) {
49 $this->classes[(string) $class['name']] = $class;
53 if (isset($this->classes[$metadata->getClassName()])) {
54 $classDescription = $this->classes[$metadata->getClassName()];
56 $this->loadClassMetadataFromXml($metadata, $classDescription);
65 * Parses a collection of "constraint" XML nodes.
67 * @param \SimpleXMLElement $nodes The XML nodes
69 * @return array The Constraint instances
71 protected function parseConstraints(\SimpleXMLElement $nodes)
73 $constraints = array();
75 foreach ($nodes as $node) {
76 if (count($node) > 0) {
77 if (count($node->value) > 0) {
78 $options = $this->parseValues($node->value);
79 } elseif (count($node->constraint) > 0) {
80 $options = $this->parseConstraints($node->constraint);
81 } elseif (count($node->option) > 0) {
82 $options = $this->parseOptions($node->option);
86 } elseif (strlen((string) $node) > 0) {
87 $options = XmlUtils::phpize(trim($node));
92 $constraints[] = $this->newConstraint((string) $node['name'], $options);
99 * Parses a collection of "value" XML nodes.
101 * @param \SimpleXMLElement $nodes The XML nodes
103 * @return array The values
105 protected function parseValues(\SimpleXMLElement $nodes)
109 foreach ($nodes as $node) {
110 if (count($node) > 0) {
111 if (count($node->value) > 0) {
112 $value = $this->parseValues($node->value);
113 } elseif (count($node->constraint) > 0) {
114 $value = $this->parseConstraints($node->constraint);
119 $value = trim($node);
122 if (isset($node['key'])) {
123 $values[(string) $node['key']] = $value;
133 * Parses a collection of "option" XML nodes.
135 * @param \SimpleXMLElement $nodes The XML nodes
137 * @return array The options
139 protected function parseOptions(\SimpleXMLElement $nodes)
143 foreach ($nodes as $node) {
144 if (count($node) > 0) {
145 if (count($node->value) > 0) {
146 $value = $this->parseValues($node->value);
147 } elseif (count($node->constraint) > 0) {
148 $value = $this->parseConstraints($node->constraint);
153 $value = XmlUtils::phpize($node);
154 if (is_string($value)) {
155 $value = trim($value);
159 $options[(string) $node['name']] = $value;
166 * Loads the XML class descriptions from the given file.
168 * @param string $path The path of the XML file
170 * @return \SimpleXMLElement The class descriptions
172 * @throws MappingException If the file could not be loaded
174 protected function parseFile($path)
177 $dom = XmlUtils::loadFile($path, __DIR__.'/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd');
178 } catch (\Exception $e) {
179 throw new MappingException($e->getMessage(), $e->getCode(), $e);
182 return simplexml_import_dom($dom);
185 private function loadClassMetadataFromXml(ClassMetadata $metadata, \SimpleXMLElement $classDescription)
187 if (count($classDescription->{'group-sequence-provider'}) > 0) {
188 $metadata->setGroupSequenceProvider(true);
191 foreach ($classDescription->{'group-sequence'} as $groupSequence) {
192 if (count($groupSequence->value) > 0) {
193 $metadata->setGroupSequence($this->parseValues($groupSequence[0]->value));
197 foreach ($this->parseConstraints($classDescription->constraint) as $constraint) {
198 $metadata->addConstraint($constraint);
201 foreach ($classDescription->property as $property) {
202 foreach ($this->parseConstraints($property->constraint) as $constraint) {
203 $metadata->addPropertyConstraint((string) $property['name'], $constraint);
207 foreach ($classDescription->getter as $getter) {
208 foreach ($this->parseConstraints($getter->constraint) as $constraint) {
209 $metadata->addGetterConstraint((string) $getter['property'], $constraint);