Yaml.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Gedmo\ReferenceIntegrity\Mapping\Driver;
  3. use Gedmo\Exception\InvalidMappingException;
  4. use Gedmo\Mapping\Driver\File;
  5. use Gedmo\Mapping\Driver;
  6. use Gedmo\ReferenceIntegrity\Mapping\Validator;
  7. /**
  8. * This is a yaml mapping driver for ReferenceIntegrity
  9. * extension. Used for extraction of extended
  10. * metadata from yaml 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 Yaml extends File implements Driver
  17. {
  18. /**
  19. * File extension
  20. * @var string
  21. */
  22. protected $_extension = '.dcm.yml';
  23. /**
  24. * {@inheritDoc}
  25. */
  26. public function readExtendedMetadata($meta, array &$config)
  27. {
  28. $mapping = $this->_getMapping($meta->name);
  29. $validator = new Validator();
  30. if (isset($mapping['fields'])) {
  31. foreach ($mapping['fields'] as $property => $fieldMapping) {
  32. if (isset($fieldMapping['gedmo']['referenceIntegrity'])) {
  33. if (!$meta->hasField($property)) {
  34. throw new InvalidMappingException(
  35. sprintf(
  36. "Unable to find reference integrity [%s] as mapped property in entity - %s",
  37. $property,
  38. $meta->name
  39. )
  40. );
  41. }
  42. if (empty($mapping['fields'][$property]['mappedBy'])) {
  43. throw new InvalidMappingException(
  44. sprintf(
  45. "'mappedBy' should be set on '%s' in '%s'",
  46. $property,
  47. $meta->name
  48. )
  49. );
  50. }
  51. if (!in_array($fieldMapping['gedmo']['referenceIntegrity'], $validator->getIntegrityActions())) {
  52. throw new InvalidMappingException(
  53. sprintf(
  54. "Field - [%s] does not have a valid integrity option, [%s] in class - %s",
  55. $property,
  56. implode($validator->getIntegrityActions(), ', '),
  57. $meta->name
  58. )
  59. );
  60. }
  61. $config['referenceIntegrities'][$property][$mapping['fields'][$property]['mappedBy']] =
  62. $fieldMapping['gedmo']['referenceIntegrity'];
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. protected function _loadMappingFile($file)
  71. {
  72. return \Symfony\Component\Yaml\Yaml::parse($file);
  73. }
  74. }