AnnotationDriver.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. $classMetadata->fileResources[] = $class->getFilename();
  40. foreach ($this->reader->getClassAnnotations($class) as $annot) {
  41. if ($annot instanceof ExclusionPolicy) {
  42. $classMetadata->exclusionPolicy = $annot->policy;
  43. }
  44. }
  45. foreach ($class->getProperties() as $property) {
  46. if ($property->getDeclaringClass()->getName() !== $name) {
  47. continue;
  48. }
  49. $propertyMetadata = new PropertyMetadata($name, $property->getName());
  50. foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
  51. if ($annot instanceof Since) {
  52. $propertyMetadata->sinceVersion = $annot->version;
  53. } else if ($annot instanceof Until) {
  54. $propertyMetadata->untilVersion = $annot->version;
  55. } else if ($annot instanceof SerializedName) {
  56. $propertyMetadata->serializedName = $annot->name;
  57. } else if ($annot instanceof Expose) {
  58. $propertyMetadata->exposed = true;
  59. } else if ($annot instanceof Exclude) {
  60. $propertyMetadata->excluded = true;
  61. } else if ($annot instanceof Type) {
  62. $propertyMetadata->type = $annot->name;
  63. }
  64. }
  65. $classMetadata->addPropertyMetadata($propertyMetadata);
  66. }
  67. return $classMetadata;
  68. }
  69. }