AnnotationDriver.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 Doctrine\Common\Annotations\Reader;
  19. use JMS\SerializerBundle\Annotation\Type;
  20. use JMS\SerializerBundle\Annotation\Exclude;
  21. use JMS\SerializerBundle\Annotation\Expose;
  22. use JMS\SerializerBundle\Annotation\SerializedName;
  23. use JMS\SerializerBundle\Annotation\Until;
  24. use JMS\SerializerBundle\Annotation\Since;
  25. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  26. use JMS\SerializerBundle\Metadata\ClassMetadata;
  27. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  28. use Metadata\Driver\DriverInterface;
  29. class AnnotationDriver implements DriverInterface
  30. {
  31. private $reader;
  32. public function __construct(Reader $reader)
  33. {
  34. $this->reader = $reader;
  35. }
  36. public function loadMetadataForClass(\ReflectionClass $class)
  37. {
  38. $classMetadata = new ClassMetadata($name = $class->getName());
  39. foreach ($this->reader->getClassAnnotations($class) as $annot) {
  40. if ($annot instanceof ExclusionPolicy) {
  41. $classMetadata->exclusionPolicy = $annot->policy;
  42. }
  43. }
  44. foreach ($class->getProperties() as $property) {
  45. if ($property->getDeclaringClass()->getName() !== $name) {
  46. continue;
  47. }
  48. $propertyMetadata = new PropertyMetadata($name, $property->getName());
  49. foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
  50. if ($annot instanceof Since) {
  51. $propertyMetadata->sinceVersion = $annot->version;
  52. } else if ($annot instanceof Until) {
  53. $propertyMetadata->untilVersion = $annot->version;
  54. } else if ($annot instanceof SerializedName) {
  55. $propertyMetadata->serializedName = $annot->name;
  56. } else if ($annot instanceof Expose) {
  57. $propertyMetadata->exposed = true;
  58. } else if ($annot instanceof Exclude) {
  59. $propertyMetadata->excluded = true;
  60. } else if ($annot instanceof Type) {
  61. $propertyMetadata->type = $annot->name;
  62. }
  63. }
  64. $classMetadata->addPropertyMetadata($propertyMetadata);
  65. }
  66. return $classMetadata;
  67. }
  68. }