Annotation.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. * @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 extends AbstractAnnotationDriver
  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. protected $validTypes = array(
  30. 'date',
  31. 'time',
  32. 'datetime',
  33. 'timestamp',
  34. 'zenddate',
  35. 'vardatetime'
  36. );
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function readExtendedMetadata($meta, array &$config) {
  41. $class = $this->getMetaReflectionClass($meta);
  42. // property annotations
  43. foreach ($class->getProperties() as $property) {
  44. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  45. $meta->isInheritedField($property->name) ||
  46. isset($meta->associationMappings[$property->name]['inherited'])
  47. ) {
  48. continue;
  49. }
  50. if ($timestampable = $this->reader->getPropertyAnnotation($property, self::TIMESTAMPABLE)) {
  51. $field = $property->getName();
  52. if (!$meta->hasField($field)) {
  53. throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}");
  54. }
  55. if (!$this->isValidField($meta, $field)) {
  56. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  57. }
  58. if (!in_array($timestampable->on, array('update', 'create', 'change'))) {
  59. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  60. }
  61. if ($timestampable->on == 'change') {
  62. if (!isset($timestampable->field)) {
  63. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  64. }
  65. $field = array(
  66. 'field' => $field,
  67. 'trackedField' => $timestampable->field,
  68. 'value' => $timestampable->value,
  69. );
  70. }
  71. // properties are unique and mapper checks that, no risk here
  72. $config[$timestampable->on][] = $field;
  73. }
  74. }
  75. }
  76. }