Annotation.php 3.7 KB

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