AnnotationDriver.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. * Copyright 2011 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\SerializerBundle\Metadata\Driver;
  18. use JMS\SerializerBundle\Annotation\AccessorOrder;
  19. use JMS\SerializerBundle\Annotation\Groups;
  20. use JMS\SerializerBundle\Annotation\Accessor;
  21. use JMS\SerializerBundle\Annotation\AccessType;
  22. use JMS\SerializerBundle\Annotation\XmlMap;
  23. use JMS\SerializerBundle\Annotation\XmlRoot;
  24. use JMS\SerializerBundle\Annotation\XmlAttribute;
  25. use JMS\SerializerBundle\Annotation\XmlList;
  26. use JMS\SerializerBundle\Annotation\XmlValue;
  27. use JMS\SerializerBundle\Annotation\PostSerialize;
  28. use JMS\SerializerBundle\Annotation\PostDeserialize;
  29. use JMS\SerializerBundle\Annotation\PreSerialize;
  30. use Metadata\MethodMetadata;
  31. use Doctrine\Common\Annotations\Reader;
  32. use JMS\SerializerBundle\Annotation\Type;
  33. use JMS\SerializerBundle\Annotation\Exclude;
  34. use JMS\SerializerBundle\Annotation\Group;
  35. use JMS\SerializerBundle\Annotation\Expose;
  36. use JMS\SerializerBundle\Annotation\SerializedName;
  37. use JMS\SerializerBundle\Annotation\Until;
  38. use JMS\SerializerBundle\Annotation\Since;
  39. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  40. use JMS\SerializerBundle\Annotation\Inline;
  41. use JMS\SerializerBundle\Annotation\ReadOnly;
  42. use JMS\SerializerBundle\Metadata\ClassMetadata;
  43. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  44. use Metadata\Driver\DriverInterface;
  45. class AnnotationDriver implements DriverInterface
  46. {
  47. private $reader;
  48. public function __construct(Reader $reader)
  49. {
  50. $this->reader = $reader;
  51. }
  52. public function loadMetadataForClass(\ReflectionClass $class)
  53. {
  54. $classMetadata = new ClassMetadata($name = $class->getName());
  55. $classMetadata->fileResources[] = $class->getFilename();
  56. $exclusionPolicy = 'NONE';
  57. $excludeAll = false;
  58. $classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY;
  59. foreach ($this->reader->getClassAnnotations($class) as $annot) {
  60. if ($annot instanceof ExclusionPolicy) {
  61. $exclusionPolicy = $annot->policy;
  62. } else if ($annot instanceof XmlRoot) {
  63. $classMetadata->xmlRootName = $annot->name;
  64. } else if ($annot instanceof Exclude) {
  65. $excludeAll = true;
  66. } else if ($annot instanceof AccessType) {
  67. $classAccessType = $annot->type;
  68. } else if ($annot instanceof AccessorOrder) {
  69. $classMetadata->setAccessorOrder($annot->order, $annot->custom);
  70. }
  71. }
  72. if (!$excludeAll) {
  73. foreach ($class->getProperties() as $property) {
  74. if ($property->getDeclaringClass()->getName() !== $name) {
  75. continue;
  76. }
  77. $propertyMetadata = new PropertyMetadata($name, $property->getName());
  78. $isExclude = $isExpose = false;
  79. $AccessType = $classAccessType;
  80. $accessor = array(null, null);
  81. foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
  82. if ($annot instanceof Since) {
  83. $propertyMetadata->sinceVersion = $annot->version;
  84. } else if ($annot instanceof Until) {
  85. $propertyMetadata->untilVersion = $annot->version;
  86. } else if ($annot instanceof SerializedName) {
  87. $propertyMetadata->serializedName = $annot->name;
  88. } else if ($annot instanceof Expose) {
  89. $isExpose = true;
  90. } else if ($annot instanceof Group) {
  91. $propertyMetadata->group = $annot->group;
  92. } else if ($annot instanceof Exclude) {
  93. $isExclude = true;
  94. } else if ($annot instanceof Type) {
  95. $propertyMetadata->type = $annot->name;
  96. } else if ($annot instanceof XmlList) {
  97. $propertyMetadata->xmlCollection = true;
  98. $propertyMetadata->xmlCollectionInline = $annot->inline;
  99. $propertyMetadata->xmlEntryName = $annot->entry;
  100. } else if ($annot instanceof XmlMap) {
  101. $propertyMetadata->xmlCollection = true;
  102. $propertyMetadata->xmlCollectionInline = $annot->inline;
  103. $propertyMetadata->xmlEntryName = $annot->entry;
  104. $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute;
  105. } else if ($annot instanceof XmlAttribute) {
  106. $propertyMetadata->xmlAttribute = true;
  107. } else if ($annot instanceof XmlValue) {
  108. $propertyMetadata->xmlValue = true;
  109. } else if ($annot instanceof AccessType) {
  110. $AccessType = $annot->type;
  111. } else if ($annot instanceof Accessor) {
  112. $accessor = array($annot->getter, $annot->setter);
  113. } else if ($annot instanceof Groups) {
  114. $propertyMetadata->setGroups($annot->names);
  115. } else if ($annot instanceof Inline) {
  116. $propertyMetadata->inline = true;
  117. } else if ($annot instanceof ReadOnly) {
  118. $propertyMetadata->readOnly = true;
  119. }
  120. }
  121. $propertyMetadata->setAccessor($AccessType, $accessor[0], $accessor[1]);
  122. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  123. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  124. $classMetadata->addPropertyMetadata($propertyMetadata);
  125. }
  126. }
  127. }
  128. foreach ($class->getMethods() as $method) {
  129. if ($method->getDeclaringClass()->getName() !== $name) {
  130. continue;
  131. }
  132. foreach ($this->reader->getMethodAnnotations($method) as $annot) {
  133. if ($annot instanceof PreSerialize) {
  134. $classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->getName()));
  135. continue 2;
  136. } else if ($annot instanceof PostDeserialize) {
  137. $classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->getName()));
  138. continue 2;
  139. } else if ($annot instanceof PostSerialize) {
  140. $classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->getName()));
  141. continue 2;
  142. }
  143. }
  144. }
  145. return $classMetadata;
  146. }
  147. }