XmlFileLoader.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Mapping\Loader;
  11. use Symfony\Component\Config\Util\XmlUtils;
  12. use Symfony\Component\Validator\Exception\MappingException;
  13. use Symfony\Component\Validator\Mapping\ClassMetadata;
  14. /**
  15. * Loads validation metadata from an XML file.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. class XmlFileLoader extends FileLoader
  20. {
  21. /**
  22. * The XML nodes of the mapping file.
  23. *
  24. * @var \SimpleXMLElement[]|null
  25. */
  26. protected $classes;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function loadClassMetadata(ClassMetadata $metadata)
  31. {
  32. if (null === $this->classes) {
  33. $this->loadClassesFromXml();
  34. }
  35. if (isset($this->classes[$metadata->getClassName()])) {
  36. $classDescription = $this->classes[$metadata->getClassName()];
  37. $this->loadClassMetadataFromXml($metadata, $classDescription);
  38. return true;
  39. }
  40. return false;
  41. }
  42. /**
  43. * Return the names of the classes mapped in this file.
  44. *
  45. * @return string[] The classes names
  46. */
  47. public function getMappedClasses()
  48. {
  49. if (null === $this->classes) {
  50. $this->loadClassesFromXml();
  51. }
  52. return array_keys($this->classes);
  53. }
  54. /**
  55. * Parses a collection of "constraint" XML nodes.
  56. *
  57. * @param \SimpleXMLElement $nodes The XML nodes
  58. *
  59. * @return array The Constraint instances
  60. */
  61. protected function parseConstraints(\SimpleXMLElement $nodes)
  62. {
  63. $constraints = [];
  64. foreach ($nodes as $node) {
  65. if (\count($node) > 0) {
  66. if (\count($node->value) > 0) {
  67. $options = $this->parseValues($node->value);
  68. } elseif (\count($node->constraint) > 0) {
  69. $options = $this->parseConstraints($node->constraint);
  70. } elseif (\count($node->option) > 0) {
  71. $options = $this->parseOptions($node->option);
  72. } else {
  73. $options = [];
  74. }
  75. } elseif (\strlen((string) $node) > 0) {
  76. $options = XmlUtils::phpize(trim($node));
  77. } else {
  78. $options = null;
  79. }
  80. $constraints[] = $this->newConstraint((string) $node['name'], $options);
  81. }
  82. return $constraints;
  83. }
  84. /**
  85. * Parses a collection of "value" XML nodes.
  86. *
  87. * @param \SimpleXMLElement $nodes The XML nodes
  88. *
  89. * @return array The values
  90. */
  91. protected function parseValues(\SimpleXMLElement $nodes)
  92. {
  93. $values = [];
  94. foreach ($nodes as $node) {
  95. if (\count($node) > 0) {
  96. if (\count($node->value) > 0) {
  97. $value = $this->parseValues($node->value);
  98. } elseif (\count($node->constraint) > 0) {
  99. $value = $this->parseConstraints($node->constraint);
  100. } else {
  101. $value = [];
  102. }
  103. } else {
  104. $value = trim($node);
  105. }
  106. if (isset($node['key'])) {
  107. $values[(string) $node['key']] = $value;
  108. } else {
  109. $values[] = $value;
  110. }
  111. }
  112. return $values;
  113. }
  114. /**
  115. * Parses a collection of "option" XML nodes.
  116. *
  117. * @param \SimpleXMLElement $nodes The XML nodes
  118. *
  119. * @return array The options
  120. */
  121. protected function parseOptions(\SimpleXMLElement $nodes)
  122. {
  123. $options = [];
  124. foreach ($nodes as $node) {
  125. if (\count($node) > 0) {
  126. if (\count($node->value) > 0) {
  127. $value = $this->parseValues($node->value);
  128. } elseif (\count($node->constraint) > 0) {
  129. $value = $this->parseConstraints($node->constraint);
  130. } else {
  131. $value = [];
  132. }
  133. } else {
  134. $value = XmlUtils::phpize($node);
  135. if (\is_string($value)) {
  136. $value = trim($value);
  137. }
  138. }
  139. $options[(string) $node['name']] = $value;
  140. }
  141. return $options;
  142. }
  143. /**
  144. * Loads the XML class descriptions from the given file.
  145. *
  146. * @return \SimpleXMLElement The class descriptions
  147. *
  148. * @throws MappingException If the file could not be loaded
  149. */
  150. protected function parseFile(string $path)
  151. {
  152. try {
  153. $dom = XmlUtils::loadFile($path, __DIR__.'/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd');
  154. } catch (\Exception $e) {
  155. throw new MappingException($e->getMessage(), $e->getCode(), $e);
  156. }
  157. return simplexml_import_dom($dom);
  158. }
  159. private function loadClassesFromXml()
  160. {
  161. // This method may throw an exception. Do not modify the class'
  162. // state before it completes
  163. $xml = $this->parseFile($this->file);
  164. $this->classes = [];
  165. foreach ($xml->namespace as $namespace) {
  166. $this->addNamespaceAlias((string) $namespace['prefix'], trim((string) $namespace));
  167. }
  168. foreach ($xml->class as $class) {
  169. $this->classes[(string) $class['name']] = $class;
  170. }
  171. }
  172. private function loadClassMetadataFromXml(ClassMetadata $metadata, \SimpleXMLElement $classDescription)
  173. {
  174. if (\count($classDescription->{'group-sequence-provider'}) > 0) {
  175. $metadata->setGroupSequenceProvider(true);
  176. }
  177. foreach ($classDescription->{'group-sequence'} as $groupSequence) {
  178. if (\count($groupSequence->value) > 0) {
  179. $metadata->setGroupSequence($this->parseValues($groupSequence[0]->value));
  180. }
  181. }
  182. foreach ($this->parseConstraints($classDescription->constraint) as $constraint) {
  183. $metadata->addConstraint($constraint);
  184. }
  185. foreach ($classDescription->property as $property) {
  186. foreach ($this->parseConstraints($property->constraint) as $constraint) {
  187. $metadata->addPropertyConstraint((string) $property['name'], $constraint);
  188. }
  189. }
  190. foreach ($classDescription->getter as $getter) {
  191. foreach ($this->parseConstraints($getter->constraint) as $constraint) {
  192. $metadata->addGetterConstraint((string) $getter['property'], $constraint);
  193. }
  194. }
  195. }
  196. }