Annotation.php 4.0 KB

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