Annotation.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. );
  36. /**
  37. * Annotation reader instance
  38. *
  39. * @var object
  40. */
  41. private $reader;
  42. /**
  43. * original driver if it is available
  44. */
  45. protected $_originalDriver = null;
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function setAnnotationReader($reader)
  50. {
  51. $this->reader = $reader;
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function validateFullMetadata(ClassMetadata $meta, array $config) {}
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  61. $class = $meta->getReflectionClass();
  62. // property annotations
  63. foreach ($class->getProperties() as $property) {
  64. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  65. $meta->isInheritedField($property->name) ||
  66. isset($meta->associationMappings[$property->name]['inherited'])
  67. ) {
  68. continue;
  69. }
  70. if ($timestampable = $this->reader->getPropertyAnnotation($property, self::TIMESTAMPABLE)) {
  71. $field = $property->getName();
  72. if (!$meta->hasField($field)) {
  73. throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}");
  74. }
  75. if (!$this->isValidField($meta, $field)) {
  76. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  77. }
  78. if (!in_array($timestampable->on, array('update', 'create', 'change'))) {
  79. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  80. }
  81. if ($timestampable->on == 'change') {
  82. if (!isset($timestampable->field) || !isset($timestampable->value)) {
  83. throw new InvalidMappingException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}");
  84. }
  85. $field = array(
  86. 'field' => $field,
  87. 'trackedField' => $timestampable->field,
  88. 'value' => $timestampable->value
  89. );
  90. }
  91. // properties are unique and mapper checks that, no risk here
  92. $config[$timestampable->on][] = $field;
  93. }
  94. }
  95. }
  96. /**
  97. * Checks if $field type is valid
  98. *
  99. * @param ClassMetadata $meta
  100. * @param string $field
  101. * @return boolean
  102. */
  103. protected function isValidField(ClassMetadata $meta, $field)
  104. {
  105. $mapping = $meta->getFieldMapping($field);
  106. return $mapping && in_array($mapping['type'], $this->validTypes);
  107. }
  108. /**
  109. * Passes in the mapping read by original driver
  110. *
  111. * @param $driver
  112. * @return void
  113. */
  114. public function setOriginalDriver($driver)
  115. {
  116. $this->_originalDriver = $driver;
  117. }
  118. }