Annotation.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * @package Gedmo.Blameable.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 extends AbstractAnnotationDriver
  19. {
  20. /**
  21. * Annotation field is blameable
  22. */
  23. const BLAMEABLE = 'Gedmo\\Mapping\\Annotation\\Blameable';
  24. /**
  25. * List of types which are valid for blame
  26. *
  27. * @var array
  28. */
  29. protected $validTypes = array(
  30. 'string',
  31. 'int',
  32. );
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function readExtendedMetadata($meta, array &$config)
  37. {
  38. $class = $this->getMetaReflectionClass($meta);
  39. // property annotations
  40. foreach ($class->getProperties() as $property) {
  41. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  42. $meta->isInheritedField($property->name) ||
  43. isset($meta->associationMappings[$property->name]['inherited'])
  44. ) {
  45. continue;
  46. }
  47. if ($blameable = $this->reader->getPropertyAnnotation($property, self::BLAMEABLE)) {
  48. $field = $property->getName();
  49. if (!$meta->hasField($field) && !$meta->hasAssociation($field)) {
  50. throw new InvalidMappingException("Unable to find blameable [{$field}] as mapped property in entity - {$meta->name}");
  51. }
  52. if ($meta->hasField($field)) {
  53. if ( !$this->isValidField($meta, $field)) {
  54. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' or a one-to-many relation in class - {$meta->name}");
  55. }
  56. } else {
  57. // association
  58. if (! $meta->isSingleValuedAssociation($field)) {
  59. throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->name}");
  60. }
  61. }
  62. if (!in_array($blameable->on, array('update', 'create', 'change'))) {
  63. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  64. }
  65. if ($blameable->on == 'change') {
  66. if (!isset($blameable->field)) {
  67. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  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. }