Annotation.php 4.0 KB

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