Annotation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Gedmo\References\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AnnotationDriverInterface;
  4. /**
  5. * This is an annotation mapping driver for References
  6. * behavioral extension.
  7. *
  8. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  9. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  10. * @author Jonathan H. Wage <jonwage@gmail.com>
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class Annotation implements AnnotationDriverInterface
  14. {
  15. /**
  16. * Annotation to mark field as reference to one
  17. */
  18. const REFERENCE_ONE = 'Gedmo\\Mapping\\Annotation\\ReferenceOne';
  19. /**
  20. * Annotation to mark field as reference to many
  21. */
  22. const REFERENCE_MANY = 'Gedmo\\Mapping\\Annotation\\ReferenceMany';
  23. /**
  24. * Annotation to mark field as reference to many
  25. */
  26. const REFERENCE_MANY_EMBED = 'Gedmo\\Mapping\\Annotation\\ReferenceManyEmbed';
  27. private $annotations = array(
  28. 'referenceOne' => self::REFERENCE_ONE,
  29. 'referenceMany' => self::REFERENCE_MANY,
  30. 'referenceManyEmbed' => self::REFERENCE_MANY_EMBED,
  31. );
  32. /**
  33. * Annotation reader instance
  34. *
  35. * @var object
  36. */
  37. private $reader;
  38. /**
  39. * original driver if it is available
  40. */
  41. protected $_originalDriver = null;
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function setAnnotationReader($reader)
  46. {
  47. $this->reader = $reader;
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function readExtendedMetadata($meta, array &$config)
  53. {
  54. $class = $meta->getReflectionClass();
  55. foreach ($this->annotations as $key => $annotation) {
  56. $config[$key] = array();
  57. foreach ($class->getProperties() as $property) {
  58. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  59. $meta->isInheritedField($property->name) ||
  60. isset($meta->associationMappings[$property->name]['inherited'])
  61. ) {
  62. continue;
  63. }
  64. if ($reference = $this->reader->getPropertyAnnotation($property, $annotation)) {
  65. $config[$key][$property->getName()] = array(
  66. 'field' => $property->getName(),
  67. 'type' => $reference->type,
  68. 'class' => $reference->class,
  69. 'identifier' => $reference->identifier,
  70. 'mappedBy' => $reference->mappedBy,
  71. 'inversedBy' => $reference->inversedBy,
  72. );
  73. }
  74. }
  75. }
  76. }
  77. /**
  78. * Passes in the mapping read by original driver
  79. *
  80. * @param $driver
  81. * @return void
  82. */
  83. public function setOriginalDriver($driver)
  84. {
  85. $this->_originalDriver = $driver;
  86. }
  87. }