schema
3 years ago
AbstractLoader.php
4 years ago
AnnotationLoader.php
1 year ago
AutoMappingTrait.php
1 year ago
FileLoader.php
4 years ago
FilesLoader.php
4 years ago
LoaderChain.php
4 years ago
LoaderInterface.php
4 years ago
PropertyInfoLoader.php
1 year ago
StaticMethodLoader.php
4 years ago
XmlFileLoader.php
4 years ago
XmlFilesLoader.php
4 years ago
YamlFileLoader.php
4 years ago
YamlFilesLoader.php
4 years ago
index.php
3 years ago
XmlFileLoader.php
145 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator\Mapping\Loader; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Config\Util\XmlUtils; |
| 5 | use MailPoetVendor\Symfony\Component\Validator\Constraint; |
| 6 | use MailPoetVendor\Symfony\Component\Validator\Exception\MappingException; |
| 7 | use MailPoetVendor\Symfony\Component\Validator\Mapping\ClassMetadata; |
| 8 | class XmlFileLoader extends FileLoader |
| 9 | { |
| 10 | protected $classes; |
| 11 | public function loadClassMetadata(ClassMetadata $metadata) |
| 12 | { |
| 13 | if (null === $this->classes) { |
| 14 | $this->loadClassesFromXml(); |
| 15 | } |
| 16 | if (isset($this->classes[$metadata->getClassName()])) { |
| 17 | $classDescription = $this->classes[$metadata->getClassName()]; |
| 18 | $this->loadClassMetadataFromXml($metadata, $classDescription); |
| 19 | return \true; |
| 20 | } |
| 21 | return \false; |
| 22 | } |
| 23 | public function getMappedClasses() |
| 24 | { |
| 25 | if (null === $this->classes) { |
| 26 | $this->loadClassesFromXml(); |
| 27 | } |
| 28 | return \array_keys($this->classes); |
| 29 | } |
| 30 | protected function parseConstraints(\SimpleXMLElement $nodes) |
| 31 | { |
| 32 | $constraints = []; |
| 33 | foreach ($nodes as $node) { |
| 34 | if (\count($node) > 0) { |
| 35 | if (\count($node->value) > 0) { |
| 36 | $options = $this->parseValues($node->value); |
| 37 | } elseif (\count($node->constraint) > 0) { |
| 38 | $options = $this->parseConstraints($node->constraint); |
| 39 | } elseif (\count($node->option) > 0) { |
| 40 | $options = $this->parseOptions($node->option); |
| 41 | } else { |
| 42 | $options = []; |
| 43 | } |
| 44 | } elseif ('' !== (string) $node) { |
| 45 | $options = XmlUtils::phpize(\trim($node)); |
| 46 | } else { |
| 47 | $options = null; |
| 48 | } |
| 49 | $constraints[] = $this->newConstraint((string) $node['name'], $options); |
| 50 | } |
| 51 | return $constraints; |
| 52 | } |
| 53 | protected function parseValues(\SimpleXMLElement $nodes) |
| 54 | { |
| 55 | $values = []; |
| 56 | foreach ($nodes as $node) { |
| 57 | if (\count($node) > 0) { |
| 58 | if (\count($node->value) > 0) { |
| 59 | $value = $this->parseValues($node->value); |
| 60 | } elseif (\count($node->constraint) > 0) { |
| 61 | $value = $this->parseConstraints($node->constraint); |
| 62 | } else { |
| 63 | $value = []; |
| 64 | } |
| 65 | } else { |
| 66 | $value = \trim($node); |
| 67 | } |
| 68 | if (isset($node['key'])) { |
| 69 | $values[(string) $node['key']] = $value; |
| 70 | } else { |
| 71 | $values[] = $value; |
| 72 | } |
| 73 | } |
| 74 | return $values; |
| 75 | } |
| 76 | protected function parseOptions(\SimpleXMLElement $nodes) |
| 77 | { |
| 78 | $options = []; |
| 79 | foreach ($nodes as $node) { |
| 80 | if (\count($node) > 0) { |
| 81 | if (\count($node->value) > 0) { |
| 82 | $value = $this->parseValues($node->value); |
| 83 | } elseif (\count($node->constraint) > 0) { |
| 84 | $value = $this->parseConstraints($node->constraint); |
| 85 | } else { |
| 86 | $value = []; |
| 87 | } |
| 88 | } else { |
| 89 | $value = XmlUtils::phpize($node); |
| 90 | if (\is_string($value)) { |
| 91 | $value = \trim($value); |
| 92 | } |
| 93 | } |
| 94 | $options[(string) $node['name']] = $value; |
| 95 | } |
| 96 | return $options; |
| 97 | } |
| 98 | protected function parseFile(string $path) |
| 99 | { |
| 100 | try { |
| 101 | $dom = XmlUtils::loadFile($path, __DIR__ . '/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd'); |
| 102 | } catch (\Exception $e) { |
| 103 | throw new MappingException($e->getMessage(), $e->getCode(), $e); |
| 104 | } |
| 105 | return \simplexml_import_dom($dom); |
| 106 | } |
| 107 | private function loadClassesFromXml() |
| 108 | { |
| 109 | // This method may throw an exception. Do not modify the class' |
| 110 | // state before it completes |
| 111 | $xml = $this->parseFile($this->file); |
| 112 | $this->classes = []; |
| 113 | foreach ($xml->namespace as $namespace) { |
| 114 | $this->addNamespaceAlias((string) $namespace['prefix'], \trim((string) $namespace)); |
| 115 | } |
| 116 | foreach ($xml->class as $class) { |
| 117 | $this->classes[(string) $class['name']] = $class; |
| 118 | } |
| 119 | } |
| 120 | private function loadClassMetadataFromXml(ClassMetadata $metadata, \SimpleXMLElement $classDescription) |
| 121 | { |
| 122 | if (\count($classDescription->{'group-sequence-provider'}) > 0) { |
| 123 | $metadata->setGroupSequenceProvider(\true); |
| 124 | } |
| 125 | foreach ($classDescription->{'group-sequence'} as $groupSequence) { |
| 126 | if (\count($groupSequence->value) > 0) { |
| 127 | $metadata->setGroupSequence($this->parseValues($groupSequence[0]->value)); |
| 128 | } |
| 129 | } |
| 130 | foreach ($this->parseConstraints($classDescription->constraint) as $constraint) { |
| 131 | $metadata->addConstraint($constraint); |
| 132 | } |
| 133 | foreach ($classDescription->property as $property) { |
| 134 | foreach ($this->parseConstraints($property->constraint) as $constraint) { |
| 135 | $metadata->addPropertyConstraint((string) $property['name'], $constraint); |
| 136 | } |
| 137 | } |
| 138 | foreach ($classDescription->getter as $getter) { |
| 139 | foreach ($this->parseConstraints($getter->constraint) as $constraint) { |
| 140 | $metadata->addGetterConstraint((string) $getter['property'], $constraint); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 |