| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Mapping\Loader; |
| 13 |
|
| 14 |
use Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException; |
| 15 |
use Symfony\Component\PropertyAccess\PropertyPath; |
| 16 |
use Symfony\Component\Serializer\Exception\MappingException; |
| 17 |
use Symfony\Component\Serializer\Mapping\AttributeMetadata; |
| 18 |
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; |
| 19 |
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface; |
| 20 |
use Symfony\Component\Yaml\Parser; |
| 21 |
use Symfony\Component\Yaml\Yaml; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
> |
| 27 |
|
| 28 |
class YamlFileLoader extends FileLoader |
| 29 |
{ |
| 30 |
private ?Parser $yamlParser = null; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
private ?array $classes = null; |
| 36 |
|
| 37 |
public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool |
| 38 |
{ |
| 39 |
if (!$this->classes ??= $this->getClassesFromYaml()) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
if (!isset($this->classes[$classMetadata->getName()])) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
$yaml = $this->classes[$classMetadata->getName()]; |
| 48 |
|
| 49 |
if (isset($yaml['attributes']) && \is_array($yaml['attributes'])) { |
| 50 |
$attributesMetadata = $classMetadata->getAttributesMetadata(); |
| 51 |
|
| 52 |
foreach ($yaml['attributes'] as $attribute => $data) { |
| 53 |
if (isset($attributesMetadata[$attribute])) { |
| 54 |
$attributeMetadata = $attributesMetadata[$attribute]; |
| 55 |
} else { |
| 56 |
$attributeMetadata = new AttributeMetadata($attribute); |
| 57 |
$classMetadata->addAttributeMetadata($attributeMetadata); |
| 58 |
} |
| 59 |
|
| 60 |
if (isset($data['groups'])) { |
| 61 |
if (!\is_array($data['groups'])) { |
| 62 |
throw new MappingException(\sprintf('The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName())); |
| 63 |
} |
| 64 |
|
| 65 |
foreach ($data['groups'] as $group) { |
| 66 |
if (!\is_string($group)) { |
| 67 |
throw new MappingException(\sprintf('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName())); |
| 68 |
} |
| 69 |
|
| 70 |
$attributeMetadata->addGroup($group); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if (isset($data['max_depth'])) { |
| 75 |
if (!\is_int($data['max_depth'])) { |
| 76 |
throw new MappingException(\sprintf('The "max_depth" value must be an integer in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName())); |
| 77 |
} |
| 78 |
|
| 79 |
$attributeMetadata->setMaxDepth($data['max_depth']); |
| 80 |
} |
| 81 |
|
| 82 |
if (isset($data['serialized_name'])) { |
| 83 |
if (!\is_string($data['serialized_name']) || '' === $data['serialized_name']) { |
| 84 |
throw new MappingException(\sprintf('The "serialized_name" value must be a non-empty string in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName())); |
| 85 |
} |
| 86 |
|
| 87 |
$attributeMetadata->setSerializedName($data['serialized_name']); |
| 88 |
} |
| 89 |
|
| 90 |
if (isset($data['serialized_path'])) { |
| 91 |
try { |
| 92 |
$attributeMetadata->setSerializedPath(new PropertyPath((string) $data['serialized_path'])); |
| 93 |
} catch (InvalidPropertyPathException) { |
| 94 |
throw new MappingException(\sprintf('The "serialized_path" value must be a valid property path in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName())); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if (isset($data['ignore'])) { |
| 99 |
if (!\is_bool($data['ignore'])) { |
| 100 |
throw new MappingException(\sprintf('The "ignore" value must be a boolean in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName())); |
| 101 |
} |
| 102 |
|
| 103 |
$attributeMetadata->setIgnore($data['ignore']); |
| 104 |
} |
| 105 |
|
| 106 |
foreach ($data['contexts'] ?? [] as $line) { |
| 107 |
$groups = $line['groups'] ?? []; |
| 108 |
|
| 109 |
if ($context = $line['context'] ?? false) { |
| 110 |
$attributeMetadata->setNormalizationContextForGroups($context, $groups); |
| 111 |
$attributeMetadata->setDenormalizationContextForGroups($context, $groups); |
| 112 |
} |
| 113 |
|
| 114 |
if ($context = $line['normalization_context'] ?? false) { |
| 115 |
$attributeMetadata->setNormalizationContextForGroups($context, $groups); |
| 116 |
} |
| 117 |
|
| 118 |
if ($context = $line['denormalization_context'] ?? false) { |
| 119 |
$attributeMetadata->setDenormalizationContextForGroups($context, $groups); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (isset($yaml['discriminator_map'])) { |
| 126 |
if (!isset($yaml['discriminator_map']['type_property'])) { |
| 127 |
throw new MappingException(\sprintf('The "type_property" key must be set for the discriminator map of the class "%s" in "%s".', $classMetadata->getName(), $this->file)); |
| 128 |
} |
| 129 |
|
| 130 |
if (!isset($yaml['discriminator_map']['mapping'])) { |
| 131 |
throw new MappingException(\sprintf('The "mapping" key must be set for the discriminator map of the class "%s" in "%s".', $classMetadata->getName(), $this->file)); |
| 132 |
} |
| 133 |
|
| 134 |
$classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping( |
| 135 |
$yaml['discriminator_map']['type_property'], |
| 136 |
$yaml['discriminator_map']['mapping'], |
| 137 |
$yaml['discriminator_map']['default_type'] ?? null |
| 138 |
)); |
| 139 |
} |
| 140 |
|
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function getMappedClasses(): array |
| 150 |
{ |
| 151 |
return array_keys($this->classes ??= $this->getClassesFromYaml()); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
private function getClassesFromYaml(): array |
| 158 |
{ |
| 159 |
if (!stream_is_local($this->file)) { |
| 160 |
throw new MappingException(\sprintf('This is not a local file "%s".', $this->file)); |
| 161 |
} |
| 162 |
|
| 163 |
$this->yamlParser ??= new Parser(); |
| 164 |
|
| 165 |
$classes = $this->yamlParser->parseFile($this->file, Yaml::PARSE_CONSTANT); |
| 166 |
|
| 167 |
if (!$classes) { |
| 168 |
return []; |
| 169 |
} |
| 170 |
|
| 171 |
if (!\is_array($classes)) { |
| 172 |
throw new MappingException(\sprintf('The file "%s" must contain a YAML array.', $this->file)); |
| 173 |
} |
| 174 |
|
| 175 |
return $classes; |
| 176 |
} |
| 177 |
} |