Annotation.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Gedmo\Timestampable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AnnotationDriverInterface,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Gedmo\Exception\InvalidMappingException;
  6. /**
  7. * This is an annotation mapping driver for Timestampable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from Annotations specificaly for Timestampable
  10. * extension.
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Translatable.Mapping.Driver
  14. * @subpackage Annotation
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class Annotation implements AnnotationDriverInterface
  19. {
  20. /**
  21. * Annotation field is timestampable
  22. */
  23. const TIMESTAMPABLE = 'Gedmo\\Mapping\\Annotation\\Timestampable';
  24. /**
  25. * List of types which are valid for timestamp
  26. *
  27. * @var array
  28. */
  29. private $validTypes = array(
  30. 'date',
  31. 'time',
  32. 'datetime',
  33. 'timestamp',
  34. 'zenddate',
  35. 'vardatetime'
  36. );
  37. /**
  38. * Annotation reader instance
  39. *
  40. * @var object
  41. */
  42. private $reader;
  43. /**
  44. * original driver if it is available
  45. */
  46. protected $_originalDriver = null;
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function setAnnotationReader($reader)
  51. {
  52. $this->reader = $reader;
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function readExtendedMetadata($meta, array &$config) {
  58. $class = $meta->getReflectionClass();
  59. if (!$class) {
  60. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  61. // this happens when running annotation driver in combination with
  62. // static reflection services. This is not the nicest fix
  63. $class = new \ReflectionClass($meta->name);
  64. }
  65. // property annotations
  66. foreach ($class->getProperties() as $property) {
  67. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  68. $meta->isInheritedField($property->name) ||
  69. isset($meta->associationMappings[$property->name]['inherited'])
  70. ) {
  71. continue;
  72. }
  73. if ($timestampable = $this->reader->getPropertyAnnotation($property, self::TIMESTAMPABLE)) {
  74. $field = $property->getName();
  75. if (!$meta->hasField($field)) {
  76. throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}");
  77. }
  78. if (!$this->isValidField($meta, $field)) {
  79. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  80. }
  81. if (!in_array($timestampable->on, array('update', 'create', 'change'))) {
  82. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  83. }
  84. if ($timestampable->on == 'change') {
  85. if (!isset($timestampable->field) || !isset($timestampable->value)) {
  86. throw new InvalidMappingException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}");
  87. }
  88. $field = array(
  89. 'field' => $field,
  90. 'trackedField' => $timestampable->field,
  91. 'value' => $timestampable->value
  92. );
  93. }
  94. // properties are unique and mapper checks that, no risk here
  95. $config[$timestampable->on][] = $field;
  96. }
  97. }
  98. }
  99. /**
  100. * Checks if $field type is valid
  101. *
  102. * @param object $meta
  103. * @param string $field
  104. * @return boolean
  105. */
  106. protected function isValidField($meta, $field)
  107. {
  108. $mapping = $meta->getFieldMapping($field);
  109. return $mapping && in_array($mapping['type'], $this->validTypes);
  110. }
  111. /**
  112. * Passes in the mapping read by original driver
  113. *
  114. * @param $driver
  115. * @return void
  116. */
  117. public function setOriginalDriver($driver)
  118. {
  119. $this->_originalDriver = $driver;
  120. }
  121. }