Annotation.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Gedmo\Timestampable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AbstractAnnotationDriver,
  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. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class Annotation extends AbstractAnnotationDriver
  16. {
  17. /**
  18. * Annotation field is timestampable
  19. */
  20. const TIMESTAMPABLE = 'Gedmo\\Mapping\\Annotation\\Timestampable';
  21. /**
  22. * List of types which are valid for timestamp
  23. *
  24. * @var array
  25. */
  26. protected $validTypes = array(
  27. 'date',
  28. 'time',
  29. 'datetime',
  30. 'datetimetz',
  31. 'timestamp',
  32. 'zenddate',
  33. 'vardatetime',
  34. 'integer'
  35. );
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function readExtendedMetadata($meta, array &$config) {
  40. $class = $this->getMetaReflectionClass($meta);
  41. // property annotations
  42. foreach ($class->getProperties() as $property) {
  43. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  44. $meta->isInheritedField($property->name) ||
  45. isset($meta->associationMappings[$property->name]['inherited'])
  46. ) {
  47. continue;
  48. }
  49. if ($timestampable = $this->reader->getPropertyAnnotation($property, self::TIMESTAMPABLE)) {
  50. $field = $property->getName();
  51. if (!$meta->hasField($field)) {
  52. throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}");
  53. }
  54. if (!$this->isValidField($meta, $field)) {
  55. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  56. }
  57. if (!in_array($timestampable->on, array('update', 'create', 'change'))) {
  58. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  59. }
  60. if ($timestampable->on == 'change') {
  61. if (!isset($timestampable->field)) {
  62. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  63. }
  64. if (is_array($timestampable->field) && isset($timestampable->value)) {
  65. throw new InvalidMappingException("Timestampable extension does not support multiple value changeset detection yet.");
  66. }
  67. $field = array(
  68. 'field' => $field,
  69. 'trackedField' => $timestampable->field,
  70. 'value' => $timestampable->value,
  71. );
  72. }
  73. // properties are unique and mapper checks that, no risk here
  74. $config[$timestampable->on][] = $field;
  75. }
  76. }
  77. }
  78. }