Annotation.php 4.2 KB

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