Annotation.php 3.5 KB

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