Annotation.php 3.7 KB

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