Annotation.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Gedmo\Timestampable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  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 Driver
  20. {
  21. /**
  22. * Annotation field is timestampable
  23. */
  24. const ANNOTATION_TIMESTAMPABLE = 'Gedmo\Timestampable\Mapping\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. * {@inheritDoc}
  38. */
  39. public function validateFullMetadata(ClassMetadata $meta, array $config) {}
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  44. require_once __DIR__ . '/../Annotations.php';
  45. $reader = new AnnotationReader();
  46. $reader->setAnnotationNamespaceAlias('Gedmo\Timestampable\Mapping\\', 'gedmo');
  47. $class = $meta->getReflectionClass();
  48. // property annotations
  49. foreach ($class->getProperties() as $property) {
  50. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  51. $meta->isInheritedField($property->name) ||
  52. isset($meta->associationMappings[$property->name]['inherited'])
  53. ) {
  54. continue;
  55. }
  56. if ($timestampable = $reader->getPropertyAnnotation($property, self::ANNOTATION_TIMESTAMPABLE)) {
  57. $field = $property->getName();
  58. if (!$meta->hasField($field)) {
  59. throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}");
  60. }
  61. if (!$this->isValidField($meta, $field)) {
  62. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  63. }
  64. if (!in_array($timestampable->on, array('update', 'create', 'change'))) {
  65. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  66. }
  67. if ($timestampable->on == 'change') {
  68. if (!isset($timestampable->field) || !isset($timestampable->value)) {
  69. throw new InvalidMappingException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}");
  70. }
  71. $field = array(
  72. 'field' => $field,
  73. 'trackedField' => $timestampable->field,
  74. 'value' => $timestampable->value
  75. );
  76. }
  77. // properties are unique and mapper checks that, no risk here
  78. $config[$timestampable->on][] = $field;
  79. }
  80. }
  81. }
  82. /**
  83. * Checks if $field type is valid
  84. *
  85. * @param ClassMetadata $meta
  86. * @param string $field
  87. * @return boolean
  88. */
  89. protected function isValidField(ClassMetadata $meta, $field)
  90. {
  91. $mapping = $meta->getFieldMapping($field);
  92. return $mapping && in_array($mapping['type'], $this->validTypes);
  93. }
  94. }