Annotation.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Gedmo\ReferenceIntegrity\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
  4. use Gedmo\Exception\InvalidMappingException;
  5. use Gedmo\ReferenceIntegrity\Mapping\Validator;
  6. /**
  7. * This is an annotation mapping driver for ReferenceIntegrity
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from Annotations specifically for ReferenceIntegrity
  10. * extension.
  11. *
  12. * @author Evert Harmeling <evert.harmeling@freshheads.com>
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class Annotation extends AbstractAnnotationDriver
  16. {
  17. /**
  18. * Annotation to identify the fields which manages the reference integrity
  19. */
  20. const REFERENCE_INTEGRITY = 'Gedmo\\Mapping\\Annotation\\ReferenceIntegrity';
  21. /**
  22. * ReferenceIntegrityAction extension annotation
  23. */
  24. const ACTION = 'Gedmo\\Mapping\\Annotation\\ReferenceIntegrityAction';
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function readExtendedMetadata($meta, array &$config)
  29. {
  30. $validator = new Validator();
  31. $reflClass = $this->getMetaReflectionClass($meta);
  32. foreach ($reflClass->getProperties() as $reflProperty) {
  33. if ($referenceIntegrity = $this->reader->getPropertyAnnotation($reflProperty, self::REFERENCE_INTEGRITY)) {
  34. $property = $reflProperty->getName();
  35. if (!$meta->hasField($property)) {
  36. throw new InvalidMappingException(
  37. sprintf(
  38. "Unable to find reference integrity [%s] as mapped property in entity - %s",
  39. $property,
  40. $meta->name
  41. )
  42. );
  43. }
  44. $fieldMapping = $meta->getFieldMapping($property);
  45. if (!isset($fieldMapping['mappedBy'])) {
  46. throw new InvalidMappingException(
  47. sprintf(
  48. "'mappedBy' should be set on '%s' in '%s'",
  49. $property,
  50. $meta->name
  51. )
  52. );
  53. }
  54. if (!in_array($referenceIntegrity->value, $validator->getIntegrityActions())) {
  55. throw new InvalidMappingException(
  56. sprintf(
  57. "Field - [%s] does not have a valid integrity option, [%s] in class - %s",
  58. $property,
  59. implode($validator->getIntegrityActions(), ', '),
  60. $meta->name
  61. )
  62. );
  63. }
  64. $config['referenceIntegrity'][$property] = $referenceIntegrity->value;
  65. }
  66. }
  67. }
  68. }