Annotation.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Gedmo\Timestampable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
  4. use Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is an annotation mapping driver for Timestampable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from Annotations specifically for Timestampable
  9. * extension.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class Annotation extends AbstractAnnotationDriver
  15. {
  16. /**
  17. * Annotation field is timestampable
  18. */
  19. const TIMESTAMPABLE = 'Gedmo\\Mapping\\Annotation\\Timestampable';
  20. /**
  21. * List of types which are valid for timestamp
  22. *
  23. * @var array
  24. */
  25. protected $validTypes = array(
  26. 'date',
  27. 'date_immutable',
  28. 'time',
  29. 'time_immutable',
  30. 'datetime',
  31. 'datetime_immutable',
  32. 'datetimetz',
  33. 'datetimetz_immutable',
  34. 'timestamp',
  35. 'zenddate',
  36. 'vardatetime',
  37. 'integer',
  38. );
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function readExtendedMetadata($meta, array &$config)
  43. {
  44. $class = $this->getMetaReflectionClass($meta);
  45. // property annotations
  46. foreach ($class->getProperties() as $property) {
  47. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  48. $meta->isInheritedField($property->name) ||
  49. isset($meta->associationMappings[$property->name]['inherited'])
  50. ) {
  51. continue;
  52. }
  53. if ($timestampable = $this->reader->getPropertyAnnotation($property, self::TIMESTAMPABLE)) {
  54. $field = $property->getName();
  55. if (!$meta->hasField($field)) {
  56. throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}");
  57. }
  58. if (!$this->isValidField($meta, $field)) {
  59. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  60. }
  61. if (!in_array($timestampable->on, array('update', 'create', 'change'))) {
  62. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  63. }
  64. if ($timestampable->on == 'change') {
  65. if (!isset($timestampable->field)) {
  66. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  67. }
  68. if (is_array($timestampable->field) && isset($timestampable->value)) {
  69. throw new InvalidMappingException("Timestampable extension does not support multiple value changeset detection yet.");
  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. }