Annotation.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Gedmo\Blameable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AbstractAnnotationDriver,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Gedmo\Exception\InvalidMappingException;
  6. /**
  7. * This is an annotation mapping driver for Blameable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from Annotations specificaly for Blameable
  10. * extension.
  11. *
  12. * @author David Buchmann <mail@davidbu.ch>
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class Annotation extends AbstractAnnotationDriver
  16. {
  17. /**
  18. * Annotation field is blameable
  19. */
  20. const BLAMEABLE = 'Gedmo\\Mapping\\Annotation\\Blameable';
  21. /**
  22. * List of types which are valid for blame
  23. *
  24. * @var array
  25. */
  26. protected $validTypes = array(
  27. 'one',
  28. 'string',
  29. 'int',
  30. );
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function readExtendedMetadata($meta, array &$config)
  35. {
  36. $class = $this->getMetaReflectionClass($meta);
  37. // property annotations
  38. foreach ($class->getProperties() as $property) {
  39. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  40. $meta->isInheritedField($property->name) ||
  41. isset($meta->associationMappings[$property->name]['inherited'])
  42. ) {
  43. continue;
  44. }
  45. if ($blameable = $this->reader->getPropertyAnnotation($property, self::BLAMEABLE)) {
  46. $field = $property->getName();
  47. if (!$meta->hasField($field) && !$meta->hasAssociation($field)) {
  48. throw new InvalidMappingException("Unable to find blameable [{$field}] as mapped property in entity - {$meta->name}");
  49. }
  50. if ($meta->hasField($field)) {
  51. if ( !$this->isValidField($meta, $field)) {
  52. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' or a one-to-many relation in class - {$meta->name}");
  53. }
  54. } else {
  55. // association
  56. if (! $meta->isSingleValuedAssociation($field)) {
  57. throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->name}");
  58. }
  59. }
  60. if (!in_array($blameable->on, array('update', 'create', 'change'))) {
  61. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  62. }
  63. if ($blameable->on == 'change') {
  64. if (!isset($blameable->field)) {
  65. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  66. }
  67. if (is_array($blameable->field) && isset($blameable->value)) {
  68. throw new InvalidMappingException("Blameable extension does not support multiple value changeset detection yet.");
  69. }
  70. $field = array(
  71. 'field' => $field,
  72. 'trackedField' => $blameable->field,
  73. 'value' => $blameable->value,
  74. );
  75. }
  76. // properties are unique and mapper checks that, no risk here
  77. $config[$blameable->on][] = $field;
  78. }
  79. }
  80. }
  81. }