AnnotationDriver.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 Annotations\ReaderInterface;
  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. class AnnotationDriver implements DriverInterface
  29. {
  30. private $reader;
  31. public function __construct(ReaderInterface $reader)
  32. {
  33. $this->reader = $reader;
  34. }
  35. public function loadMetadataForClass(\ReflectionClass $class)
  36. {
  37. $classMetadata = new ClassMetadata($name = $class->getName());
  38. foreach ($this->reader->getClassAnnotations($class) as $annot) {
  39. if ($annot instanceof ExclusionPolicy) {
  40. $classMetadata->setExclusionPolicy($annot->getStrategy());
  41. }
  42. }
  43. foreach ($class->getProperties() as $property) {
  44. if ($property->getDeclaringClass()->getName() !== $name) {
  45. continue;
  46. }
  47. $propertyMetadata = new PropertyMetadata($name, $property->getName());
  48. foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
  49. if ($annot instanceof Since) {
  50. $propertyMetadata->setSinceVersion($annot->getVersion());
  51. } else if ($annot instanceof Until) {
  52. $propertyMetadata->setUntilVersion($annot->getVersion());
  53. } else if ($annot instanceof SerializedName) {
  54. $propertyMetadata->setSerializedName($annot->getName());
  55. } else if ($annot instanceof Expose) {
  56. $propertyMetadata->setExposed(true);
  57. } else if ($annot instanceof Exclude) {
  58. $propertyMetadata->setExcluded(true);
  59. } else if ($annot instanceof Type) {
  60. $propertyMetadata->setType($annot->getName());
  61. }
  62. }
  63. $classMetadata->addPropertyMetadata($propertyMetadata);
  64. }
  65. return $classMetadata;
  66. }
  67. }