PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/jms/serializer/src/JMS/Serializer/Metadata/Driver/AnnotationDriver.php

https://gitlab.com/cuza/Clinic_Recods
PHP | 225 lines | 192 code | 18 blank | 15 comment | 14 complexity | 5ef9e84f8aeb70240d794fbb01ba8719 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\Serializer\Metadata\Driver;
  18. use JMS\Serializer\Annotation\Discriminator;
  19. use JMS\Serializer\GraphNavigator;
  20. use JMS\Serializer\Annotation\HandlerCallback;
  21. use JMS\Serializer\Annotation\AccessorOrder;
  22. use JMS\Serializer\Annotation\Accessor;
  23. use JMS\Serializer\Annotation\AccessType;
  24. use JMS\Serializer\Annotation\XmlMap;
  25. use JMS\Serializer\Annotation\XmlRoot;
  26. use JMS\Serializer\Annotation\XmlNamespace;
  27. use JMS\Serializer\Annotation\XmlAttribute;
  28. use JMS\Serializer\Annotation\XmlList;
  29. use JMS\Serializer\Annotation\XmlValue;
  30. use JMS\Serializer\Annotation\XmlKeyValuePairs;
  31. use JMS\Serializer\Annotation\XmlElement;
  32. use JMS\Serializer\Annotation\PostSerialize;
  33. use JMS\Serializer\Annotation\PostDeserialize;
  34. use JMS\Serializer\Annotation\PreSerialize;
  35. use JMS\Serializer\Annotation\VirtualProperty;
  36. use Metadata\MethodMetadata;
  37. use Doctrine\Common\Annotations\Reader;
  38. use JMS\Serializer\Annotation\Type;
  39. use JMS\Serializer\Annotation\Exclude;
  40. use JMS\Serializer\Annotation\Groups;
  41. use JMS\Serializer\Annotation\Expose;
  42. use JMS\Serializer\Annotation\SerializedName;
  43. use JMS\Serializer\Annotation\Until;
  44. use JMS\Serializer\Annotation\Since;
  45. use JMS\Serializer\Annotation\ExclusionPolicy;
  46. use JMS\Serializer\Annotation\Inline;
  47. use JMS\Serializer\Annotation\ReadOnly;
  48. use JMS\Serializer\Metadata\ClassMetadata;
  49. use JMS\Serializer\Metadata\PropertyMetadata;
  50. use JMS\Serializer\Metadata\VirtualPropertyMetadata;
  51. use JMS\Serializer\Exception\InvalidArgumentException;
  52. use JMS\Serializer\Annotation\XmlAttributeMap;
  53. use Metadata\Driver\DriverInterface;
  54. use JMS\Serializer\Annotation\MaxDepth;
  55. class AnnotationDriver implements DriverInterface
  56. {
  57. private $reader;
  58. public function __construct(Reader $reader)
  59. {
  60. $this->reader = $reader;
  61. }
  62. public function loadMetadataForClass(\ReflectionClass $class)
  63. {
  64. $classMetadata = new ClassMetadata($name = $class->name);
  65. $classMetadata->fileResources[] = $class->getFilename();
  66. $propertiesMetadata = array();
  67. $propertiesAnnotations = array();
  68. $exclusionPolicy = 'NONE';
  69. $excludeAll = false;
  70. $classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY;
  71. $readOnlyClass = false;
  72. foreach ($this->reader->getClassAnnotations($class) as $annot) {
  73. if ($annot instanceof ExclusionPolicy) {
  74. $exclusionPolicy = $annot->policy;
  75. } elseif ($annot instanceof XmlRoot) {
  76. $classMetadata->xmlRootName = $annot->name;
  77. $classMetadata->xmlRootNamespace = $annot->namespace;
  78. } elseif ($annot instanceof XmlNamespace) {
  79. $classMetadata->registerNamespace($annot->uri, $annot->prefix);
  80. } elseif ($annot instanceof Exclude) {
  81. $excludeAll = true;
  82. } elseif ($annot instanceof AccessType) {
  83. $classAccessType = $annot->type;
  84. } elseif ($annot instanceof ReadOnly) {
  85. $readOnlyClass = true;
  86. } elseif ($annot instanceof AccessorOrder) {
  87. $classMetadata->setAccessorOrder($annot->order, $annot->custom);
  88. } elseif ($annot instanceof Discriminator) {
  89. if ($annot->disabled) {
  90. $classMetadata->discriminatorDisabled = true;
  91. } else {
  92. $classMetadata->setDiscriminator($annot->field, $annot->map);
  93. }
  94. }
  95. }
  96. foreach ($class->getMethods() as $method) {
  97. if ($method->class !== $name) {
  98. continue;
  99. }
  100. $methodAnnotations = $this->reader->getMethodAnnotations($method);
  101. foreach ($methodAnnotations as $annot) {
  102. if ($annot instanceof PreSerialize) {
  103. $classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->name));
  104. continue 2;
  105. } elseif ($annot instanceof PostDeserialize) {
  106. $classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->name));
  107. continue 2;
  108. } elseif ($annot instanceof PostSerialize) {
  109. $classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->name));
  110. continue 2;
  111. } elseif ($annot instanceof VirtualProperty) {
  112. $virtualPropertyMetadata = new VirtualPropertyMetadata($name, $method->name);
  113. $propertiesMetadata[] = $virtualPropertyMetadata;
  114. $propertiesAnnotations[] = $methodAnnotations;
  115. continue 2;
  116. } elseif ($annot instanceof HandlerCallback) {
  117. $classMetadata->addHandlerCallback(GraphNavigator::parseDirection($annot->direction), $annot->format, $method->name);
  118. continue 2;
  119. }
  120. }
  121. }
  122. if ( ! $excludeAll) {
  123. foreach ($class->getProperties() as $property) {
  124. if ($property->class !== $name) {
  125. continue;
  126. }
  127. $propertiesMetadata[] = new PropertyMetadata($name, $property->getName());
  128. $propertiesAnnotations[] = $this->reader->getPropertyAnnotations($property);
  129. }
  130. foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) {
  131. $isExclude = false;
  132. $isExpose = $propertyMetadata instanceof VirtualPropertyMetadata;
  133. $propertyMetadata->readOnly = $propertyMetadata->readOnly || $readOnlyClass;
  134. $accessType = $classAccessType;
  135. $accessor = array(null, null);
  136. $propertyAnnotations = $propertiesAnnotations[$propertyKey];
  137. foreach ($propertyAnnotations as $annot) {
  138. if ($annot instanceof Since) {
  139. $propertyMetadata->sinceVersion = $annot->version;
  140. } elseif ($annot instanceof Until) {
  141. $propertyMetadata->untilVersion = $annot->version;
  142. } elseif ($annot instanceof SerializedName) {
  143. $propertyMetadata->serializedName = $annot->name;
  144. } elseif ($annot instanceof Expose) {
  145. $isExpose = true;
  146. } elseif ($annot instanceof Exclude) {
  147. $isExclude = true;
  148. } elseif ($annot instanceof Type) {
  149. $propertyMetadata->setType($annot->name);
  150. } elseif ($annot instanceof XmlElement) {
  151. $propertyMetadata->xmlAttribute = false;
  152. $propertyMetadata->xmlElementCData = $annot->cdata;
  153. $propertyMetadata->xmlNamespace = $annot->namespace;
  154. } elseif ($annot instanceof XmlList) {
  155. $propertyMetadata->xmlCollection = true;
  156. $propertyMetadata->xmlCollectionInline = $annot->inline;
  157. $propertyMetadata->xmlEntryName = $annot->entry;
  158. } elseif ($annot instanceof XmlMap) {
  159. $propertyMetadata->xmlCollection = true;
  160. $propertyMetadata->xmlCollectionInline = $annot->inline;
  161. $propertyMetadata->xmlEntryName = $annot->entry;
  162. $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute;
  163. } elseif ($annot instanceof XmlKeyValuePairs) {
  164. $propertyMetadata->xmlKeyValuePairs = true;
  165. } elseif ($annot instanceof XmlAttribute) {
  166. $propertyMetadata->xmlAttribute = true;
  167. $propertyMetadata->xmlNamespace = $annot->namespace;
  168. } elseif ($annot instanceof XmlValue) {
  169. $propertyMetadata->xmlValue = true;
  170. $propertyMetadata->xmlElementCData = $annot->cdata;
  171. } elseif ($annot instanceof XmlElement) {
  172. $propertyMetadata->xmlElementCData = $annot->cdata;
  173. } elseif ($annot instanceof AccessType) {
  174. $accessType = $annot->type;
  175. } elseif ($annot instanceof ReadOnly) {
  176. $propertyMetadata->readOnly = $annot->readOnly;
  177. } elseif ($annot instanceof Accessor) {
  178. $accessor = array($annot->getter, $annot->setter);
  179. } elseif ($annot instanceof Groups) {
  180. $propertyMetadata->groups = $annot->groups;
  181. foreach ((array) $propertyMetadata->groups as $groupName) {
  182. if (false !== strpos($groupName, ',')) {
  183. throw new InvalidArgumentException(sprintf(
  184. 'Invalid group name "%s" on "%s", did you mean to create multiple groups?',
  185. implode(', ', $propertyMetadata->groups),
  186. $propertyMetadata->class.'->'.$propertyMetadata->name
  187. ));
  188. }
  189. }
  190. } elseif ($annot instanceof Inline) {
  191. $propertyMetadata->inline = true;
  192. } elseif ($annot instanceof XmlAttributeMap) {
  193. $propertyMetadata->xmlAttributeMap = true;
  194. } elseif ($annot instanceof MaxDepth) {
  195. $propertyMetadata->maxDepth = $annot->depth;
  196. }
  197. }
  198. $propertyMetadata->setAccessor($accessType, $accessor[0], $accessor[1]);
  199. if ((ExclusionPolicy::NONE === $exclusionPolicy && ! $isExclude)
  200. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  201. $classMetadata->addPropertyMetadata($propertyMetadata);
  202. }
  203. }
  204. }
  205. return $classMetadata;
  206. }
  207. }