Annotation.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Timestampable\Mapping\MappingException;
  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. * (non-PHPdoc)
  37. * @see Gedmo\Mapping.Driver::validateFullMetadata()
  38. */
  39. public function validateFullMetadata(ClassMetadataInfo $meta, array $config)
  40. {
  41. }
  42. /**
  43. * (non-PHPdoc)
  44. * @see Gedmo\Mapping.Driver::readExtendedMetadata()
  45. */
  46. public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) {
  47. require_once __DIR__ . '/../Annotations.php';
  48. $reader = new AnnotationReader();
  49. $reader->setAnnotationNamespaceAlias('Gedmo\Timestampable\Mapping\\', 'gedmo');
  50. $class = $meta->getReflectionClass();
  51. // property annotations
  52. foreach ($class->getProperties() as $property) {
  53. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  54. $meta->isInheritedField($property->name) ||
  55. $meta->isInheritedAssociation($property->name)
  56. ) {
  57. continue;
  58. }
  59. if ($timestampable = $reader->getPropertyAnnotation($property, self::ANNOTATION_TIMESTAMPABLE)) {
  60. $field = $property->getName();
  61. if (!$meta->hasField($field)) {
  62. throw MappingException::fieldMustBeMapped($field, $meta->name);
  63. }
  64. if (!$this->_isValidField($meta, $field)) {
  65. throw MappingException::notValidFieldType($field, $meta->name);
  66. }
  67. if (!in_array($timestampable->on, array('update', 'create', 'change'))) {
  68. throw MappingException::triggerTypeInvalid($field, $meta->name);
  69. }
  70. if ($timestampable->on == 'change') {
  71. if (!isset($timestampable->field) || !isset($timestampable->value)) {
  72. throw MappingException::parametersMissing($field, $meta->name);
  73. }
  74. $field = array(
  75. 'field' => $field,
  76. 'trackedField' => $timestampable->field,
  77. 'value' => $timestampable->value
  78. );
  79. }
  80. // properties are unique and mapper checks that, no risk here
  81. $config[$timestampable->on][] = $field;
  82. }
  83. }
  84. }
  85. /**
  86. * Checks if $field type is valid
  87. *
  88. * @param ClassMetadataInfo $meta
  89. * @param string $field
  90. * @return boolean
  91. */
  92. protected function _isValidField(ClassMetadataInfo $meta, $field)
  93. {
  94. return in_array($meta->getTypeOfField($field), $this->_validTypes);
  95. }
  96. }