1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace JMS\Serializer\Metadata\Driver;
use Doctrine\Common\Annotations\Reader;
use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\AccessorOrder;
use JMS\Serializer\Annotation\AccessType;
use JMS\Serializer\Annotation\Discriminator;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\ExcludeIf;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\HandlerCallback;
use JMS\Serializer\Annotation\Inline;
use JMS\Serializer\Annotation\MaxDepth;
use JMS\Serializer\Annotation\PostDeserialize;
use JMS\Serializer\Annotation\PostSerialize;
use JMS\Serializer\Annotation\PreSerialize;
use JMS\Serializer\Annotation\ReadOnly;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Since;
use JMS\Serializer\Annotation\SkipWhenEmpty;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\Until;
use JMS\Serializer\Annotation\VirtualProperty;
use JMS\Serializer\Annotation\XmlAttribute;
use JMS\Serializer\Annotation\XmlAttributeMap;
use JMS\Serializer\Annotation\XmlDiscriminator;
use JMS\Serializer\Annotation\XmlElement;
use JMS\Serializer\Annotation\XmlKeyValuePairs;
use JMS\Serializer\Annotation\XmlList;
use JMS\Serializer\Annotation\XmlMap;
use JMS\Serializer\Annotation\XmlNamespace;
use JMS\Serializer\Annotation\XmlRoot;
use JMS\Serializer\Annotation\XmlValue;
use JMS\Serializer\Exception\InvalidArgumentException;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\ExpressionPropertyMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Metadata\VirtualPropertyMetadata;
use Metadata\Driver\DriverInterface;
use Metadata\MethodMetadata;
class AnnotationDriver implements DriverInterface
{
private $reader;
public function __construct(Reader $reader)
{
$this->reader = $reader;
}
public function loadMetadataForClass(\ReflectionClass $class)
{
$classMetadata = new ClassMetadata($name = $class->name);
$classMetadata->fileResources[] = $class->getFilename();
$propertiesMetadata = array();
$propertiesAnnotations = array();
$exclusionPolicy = 'NONE';
$excludeAll = false;
$classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY;
$readOnlyClass = false;
foreach ($this->reader->getClassAnnotations($class) as $annot) {
if ($annot instanceof ExclusionPolicy) {
$exclusionPolicy = $annot->policy;
} elseif ($annot instanceof XmlRoot) {
$classMetadata->xmlRootName = $annot->name;
$classMetadata->xmlRootNamespace = $annot->namespace;
} elseif ($annot instanceof XmlNamespace) {
$classMetadata->registerNamespace($annot->uri, $annot->prefix);
} elseif ($annot instanceof Exclude) {
$excludeAll = true;
} elseif ($annot instanceof AccessType) {
$classAccessType = $annot->type;
} elseif ($annot instanceof ReadOnly) {
$readOnlyClass = true;
} elseif ($annot instanceof AccessorOrder) {
$classMetadata->setAccessorOrder($annot->order, $annot->custom);
} elseif ($annot instanceof Discriminator) {
if ($annot->disabled) {
$classMetadata->discriminatorDisabled = true;
} else {
$classMetadata->setDiscriminator($annot->field, $annot->map, $annot->groups);
}
} elseif ($annot instanceof XmlDiscriminator) {
$classMetadata->xmlDiscriminatorAttribute = (bool)$annot->attribute;
$classMetadata->xmlDiscriminatorCData = (bool)$annot->cdata;
$classMetadata->xmlDiscriminatorNamespace = $annot->namespace ? (string)$annot->namespace : null;
} elseif ($annot instanceof VirtualProperty) {
$virtualPropertyMetadata = new ExpressionPropertyMetadata($name, $annot->name, $annot->exp);
$propertiesMetadata[] = $virtualPropertyMetadata;
$propertiesAnnotations[] = $annot->options;
}
}
foreach ($class->getMethods() as $method) {
if ($method->class !== $name) {
continue;
}
$methodAnnotations = $this->reader->getMethodAnnotations($method);
foreach ($methodAnnotations as $annot) {
if ($annot instanceof PreSerialize) {
$classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->name));
continue 2;
} elseif ($annot instanceof PostDeserialize) {
$classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->name));
continue 2;
} elseif ($annot instanceof PostSerialize) {
$classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->name));
continue 2;
} elseif ($annot instanceof VirtualProperty) {
$virtualPropertyMetadata = new VirtualPropertyMetadata($name, $method->name);
$propertiesMetadata[] = $virtualPropertyMetadata;
$propertiesAnnotations[] = $methodAnnotations;
continue 2;
} elseif ($annot instanceof HandlerCallback) {
$classMetadata->addHandlerCallback(GraphNavigator::parseDirection($annot->direction), $annot->format, $method->name);
continue 2;
}
}
}
if (!$excludeAll) {
foreach ($class->getProperties() as $property) {
if ($property->class !== $name || (isset($property->info) && $property->info['class'] !== $name)) {
continue;
}
$propertiesMetadata[] = new PropertyMetadata($name, $property->getName());
$propertiesAnnotations[] = $this->reader->getPropertyAnnotations($property);
}
foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) {
$isExclude = false;
$isExpose = $propertyMetadata instanceof VirtualPropertyMetadata
|| $propertyMetadata instanceof ExpressionPropertyMetadata;
$propertyMetadata->readOnly = $propertyMetadata->readOnly || $readOnlyClass;
$accessType = $classAccessType;
$accessor = array(null, null);
$propertyAnnotations = $propertiesAnnotations[$propertyKey];
foreach ($propertyAnnotations as $annot) {
if ($annot instanceof Since) {
$propertyMetadata->sinceVersion = $annot->version;
} elseif ($annot instanceof Until) {
$propertyMetadata->untilVersion = $annot->version;
} elseif ($annot instanceof SerializedName) {
$propertyMetadata->serializedName = $annot->name;
} elseif ($annot instanceof SkipWhenEmpty) {
$propertyMetadata->skipWhenEmpty = true;
} elseif ($annot instanceof Expose) {
$isExpose = true;
if (null !== $annot->if) {
$propertyMetadata->excludeIf = "!(" . $annot->if . ")";
}
} elseif ($annot instanceof Exclude) {
if (null !== $annot->if) {
$propertyMetadata->excludeIf = $annot->if;
} else {
$isExclude = true;
}
} elseif ($annot instanceof Type) {
$propertyMetadata->setType($annot->name);
} elseif ($annot instanceof XmlElement) {
$propertyMetadata->xmlAttribute = false;
$propertyMetadata->xmlElementCData = $annot->cdata;
$propertyMetadata->xmlNamespace = $annot->namespace;
} elseif ($annot instanceof XmlList) {
$propertyMetadata->xmlCollection = true;
$propertyMetadata->xmlCollectionInline = $annot->inline;
$propertyMetadata->xmlEntryName = $annot->entry;
$propertyMetadata->xmlEntryNamespace = $annot->namespace;
$propertyMetadata->xmlCollectionSkipWhenEmpty = $annot->skipWhenEmpty;
} elseif ($annot instanceof XmlMap) {
$propertyMetadata->xmlCollection = true;
$propertyMetadata->xmlCollectionInline = $annot->inline;
$propertyMetadata->xmlEntryName = $annot->entry;
$propertyMetadata->xmlEntryNamespace = $annot->namespace;
$propertyMetadata->xmlKeyAttribute = $annot->keyAttribute;
} elseif ($annot instanceof XmlKeyValuePairs) {
$propertyMetadata->xmlKeyValuePairs = true;
} elseif ($annot instanceof XmlAttribute) {
$propertyMetadata->xmlAttribute = true;
$propertyMetadata->xmlNamespace = $annot->namespace;
} elseif ($annot instanceof XmlValue) {
$propertyMetadata->xmlValue = true;
$propertyMetadata->xmlElementCData = $annot->cdata;
} elseif ($annot instanceof XmlElement) {
$propertyMetadata->xmlElementCData = $annot->cdata;
} elseif ($annot instanceof AccessType) {
$accessType = $annot->type;
} elseif ($annot instanceof ReadOnly) {
$propertyMetadata->readOnly = $annot->readOnly;
} elseif ($annot instanceof Accessor) {
$accessor = array($annot->getter, $annot->setter);
} elseif ($annot instanceof Groups) {
$propertyMetadata->groups = $annot->groups;
foreach ((array)$propertyMetadata->groups as $groupName) {
if (false !== strpos($groupName, ',')) {
throw new InvalidArgumentException(sprintf(
'Invalid group name "%s" on "%s", did you mean to create multiple groups?',
implode(', ', $propertyMetadata->groups),
$propertyMetadata->class . '->' . $propertyMetadata->name
));
}
}
} elseif ($annot instanceof Inline) {
$propertyMetadata->inline = true;
} elseif ($annot instanceof XmlAttributeMap) {
$propertyMetadata->xmlAttributeMap = true;
} elseif ($annot instanceof MaxDepth) {
$propertyMetadata->maxDepth = $annot->depth;
}
}
if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
|| (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)
) {
$propertyMetadata->setAccessor($accessType, $accessor[0], $accessor[1]);
$classMetadata->addPropertyMetadata($propertyMetadata);
}
}
}
return $classMetadata;
}
}