Annotation.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function readExtendedMetadata($meta, array &$config) {
  44. require_once __DIR__ . '/../Annotations.php';
  45. $reader = new AnnotationReader();
  46. $reader->setAnnotationNamespaceAlias('Gedmo\Timestampable\Mapping\\', 'gedmo');
  47. $class = $meta->getReflectionClass();
  48. // property annotations
  49. foreach ($class->getProperties() as $property) {
  50. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  51. $meta->isInheritedField($property->name) ||
  52. $meta->isInheritedAssociation($property->name)
  53. ) {
  54. continue;
  55. }
  56. if ($timestampable = $reader->getPropertyAnnotation($property, self::ANNOTATION_TIMESTAMPABLE)) {
  57. $field = $property->getName();
  58. if (!$meta->hasField($field)) {
  59. throw new InvalidArgumentException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}");
  60. }
  61. if (!$this->_isValidField($meta, $field)) {
  62. throw new InvalidArgumentException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  63. }
  64. if (!in_array($timestampable->on, array('update', 'create', 'change'))) {
  65. throw new InvalidArgumentException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  66. }
  67. if ($timestampable->on == 'change') {
  68. if (!isset($timestampable->field) || !isset($timestampable->value)) {
  69. throw new InvalidArgumentException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}");
  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. /**
  83. * Checks if $field type is valid
  84. *
  85. * @param ClassMetadata $meta
  86. * @param string $field
  87. * @return boolean
  88. */
  89. protected function _isValidField($meta, $field)
  90. {
  91. return in_array($meta->getTypeOfField($field), $this->_validTypes);
  92. }
  93. }