Annotation.php 3.7 KB

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