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
YamlFileLoader.php
114 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator\Mapping\Loader; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Validator\Constraint; |
| 5 | use MailPoetVendor\Symfony\Component\Validator\Mapping\ClassMetadata; |
| 6 | use MailPoetVendor\Symfony\Component\Yaml\Exception\ParseException; |
| 7 | use MailPoetVendor\Symfony\Component\Yaml\Parser as YamlParser; |
| 8 | use MailPoetVendor\Symfony\Component\Yaml\Yaml; |
| 9 | class YamlFileLoader extends FileLoader |
| 10 | { |
| 11 | protected $classes = null; |
| 12 | private $yamlParser; |
| 13 | public function loadClassMetadata(ClassMetadata $metadata) |
| 14 | { |
| 15 | if (null === $this->classes) { |
| 16 | $this->loadClassesFromYaml(); |
| 17 | } |
| 18 | if (isset($this->classes[$metadata->getClassName()])) { |
| 19 | $classDescription = $this->classes[$metadata->getClassName()]; |
| 20 | $this->loadClassMetadataFromYaml($metadata, $classDescription); |
| 21 | return \true; |
| 22 | } |
| 23 | return \false; |
| 24 | } |
| 25 | public function getMappedClasses() |
| 26 | { |
| 27 | if (null === $this->classes) { |
| 28 | $this->loadClassesFromYaml(); |
| 29 | } |
| 30 | return \array_keys($this->classes); |
| 31 | } |
| 32 | protected function parseNodes(array $nodes) |
| 33 | { |
| 34 | $values = []; |
| 35 | foreach ($nodes as $name => $childNodes) { |
| 36 | if (\is_numeric($name) && \is_array($childNodes) && 1 === \count($childNodes)) { |
| 37 | $options = \current($childNodes); |
| 38 | if (\is_array($options)) { |
| 39 | $options = $this->parseNodes($options); |
| 40 | } |
| 41 | $values[] = $this->newConstraint(\key($childNodes), $options); |
| 42 | } else { |
| 43 | if (\is_array($childNodes)) { |
| 44 | $childNodes = $this->parseNodes($childNodes); |
| 45 | } |
| 46 | $values[$name] = $childNodes; |
| 47 | } |
| 48 | } |
| 49 | return $values; |
| 50 | } |
| 51 | private function parseFile(string $path) : array |
| 52 | { |
| 53 | try { |
| 54 | $classes = $this->yamlParser->parseFile($path, Yaml::PARSE_CONSTANT); |
| 55 | } catch (ParseException $e) { |
| 56 | throw new \InvalidArgumentException(\sprintf('The file "%s" does not contain valid YAML: ', $path) . $e->getMessage(), 0, $e); |
| 57 | } |
| 58 | // empty file |
| 59 | if (null === $classes) { |
| 60 | return []; |
| 61 | } |
| 62 | // not an array |
| 63 | if (!\is_array($classes)) { |
| 64 | throw new \InvalidArgumentException(\sprintf('The file "%s" must contain a YAML array.', $this->file)); |
| 65 | } |
| 66 | return $classes; |
| 67 | } |
| 68 | private function loadClassesFromYaml() |
| 69 | { |
| 70 | if (null === $this->yamlParser) { |
| 71 | $this->yamlParser = new YamlParser(); |
| 72 | } |
| 73 | $this->classes = $this->parseFile($this->file); |
| 74 | if (isset($this->classes['namespaces'])) { |
| 75 | foreach ($this->classes['namespaces'] as $alias => $namespace) { |
| 76 | $this->addNamespaceAlias($alias, $namespace); |
| 77 | } |
| 78 | unset($this->classes['namespaces']); |
| 79 | } |
| 80 | } |
| 81 | private function loadClassMetadataFromYaml(ClassMetadata $metadata, array $classDescription) |
| 82 | { |
| 83 | if (isset($classDescription['group_sequence_provider'])) { |
| 84 | $metadata->setGroupSequenceProvider((bool) $classDescription['group_sequence_provider']); |
| 85 | } |
| 86 | if (isset($classDescription['group_sequence'])) { |
| 87 | $metadata->setGroupSequence($classDescription['group_sequence']); |
| 88 | } |
| 89 | if (isset($classDescription['constraints']) && \is_array($classDescription['constraints'])) { |
| 90 | foreach ($this->parseNodes($classDescription['constraints']) as $constraint) { |
| 91 | $metadata->addConstraint($constraint); |
| 92 | } |
| 93 | } |
| 94 | if (isset($classDescription['properties']) && \is_array($classDescription['properties'])) { |
| 95 | foreach ($classDescription['properties'] as $property => $constraints) { |
| 96 | if (null !== $constraints) { |
| 97 | foreach ($this->parseNodes($constraints) as $constraint) { |
| 98 | $metadata->addPropertyConstraint($property, $constraint); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | if (isset($classDescription['getters']) && \is_array($classDescription['getters'])) { |
| 104 | foreach ($classDescription['getters'] as $getter => $constraints) { |
| 105 | if (null !== $constraints) { |
| 106 | foreach ($this->parseNodes($constraints) as $constraint) { |
| 107 | $metadata->addGetterConstraint($getter, $constraint); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 |