Annotation.php 2.8 KB

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