AnnotationDriver.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace JMS\SerializerBundle\Metadata\Driver;
  3. use Annotations\ReaderInterface;
  4. use JMS\SerializerBundle\Annotation\Type;
  5. use JMS\SerializerBundle\Annotation\Exclude;
  6. use JMS\SerializerBundle\Annotation\Expose;
  7. use JMS\SerializerBundle\Annotation\SerializedName;
  8. use JMS\SerializerBundle\Annotation\Until;
  9. use JMS\SerializerBundle\Annotation\Since;
  10. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  11. use JMS\SerializerBundle\Metadata\ClassMetadata;
  12. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  13. class AnnotationDriver implements DriverInterface
  14. {
  15. private $reader;
  16. public function __construct(ReaderInterface $reader)
  17. {
  18. $this->reader = $reader;
  19. }
  20. public function loadMetadataForClass(\ReflectionClass $class)
  21. {
  22. $classMetadata = new ClassMetadata($name = $class->getName());
  23. foreach ($this->reader->getClassAnnotations($class) as $annot) {
  24. if ($annot instanceof ExclusionPolicy) {
  25. $classMetadata->setExclusionPolicy($annot->getStrategy());
  26. }
  27. }
  28. foreach ($class->getProperties() as $property) {
  29. if ($property->getDeclaringClass()->getName() !== $name) {
  30. continue;
  31. }
  32. $propertyMetadata = new PropertyMetadata($name, $property->getName());
  33. foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
  34. if ($annot instanceof Since) {
  35. $propertyMetadata->setSinceVersion($annot->getVersion());
  36. } else if ($annot instanceof Until) {
  37. $propertyMetadata->setUntilVersion($annot->getVersion());
  38. } else if ($annot instanceof SerializedName) {
  39. $propertyMetadata->setSerializedName($annot->getName());
  40. } else if ($annot instanceof Expose) {
  41. $propertyMetadata->setExposed(true);
  42. } else if ($annot instanceof Exclude) {
  43. $propertyMetadata->setExcluded(true);
  44. } else if ($annot instanceof Type) {
  45. $propertyMetadata->setType($annot->getName());
  46. }
  47. }
  48. $classMetadata->addPropertyMetadata($propertyMetadata);
  49. }
  50. return $classMetadata;
  51. }
  52. }