Annotation.php 3.1 KB

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