Xml.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Gedmo\References\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml;
  4. use Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is a xml mapping driver for References
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from xml specifically for References
  9. * extension.
  10. *
  11. * @author Aram Alipoor <aram.alipoor@gmail.com>
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class Xml extends BaseXml
  15. {
  16. /**
  17. * @var array
  18. */
  19. private $validTypes = array(
  20. 'document',
  21. 'entity'
  22. );
  23. /**
  24. * @var array
  25. */
  26. private $validReferences = array(
  27. 'referenceOne',
  28. 'referenceMany',
  29. 'referenceManyEmbed'
  30. );
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function readExtendedMetadata($meta, array &$config)
  35. {
  36. /**
  37. * @var \SimpleXmlElement $xml
  38. */
  39. $xml = $this->_getMapping($meta->name);
  40. $xmlDoctrine = $xml;
  41. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  42. if ($xmlDoctrine->getName() === 'entity' || $xmlDoctrine->getName() === 'document' || $xmlDoctrine->getName() === 'mapped-superclass') {
  43. if (isset($xml->reference)) {
  44. /**
  45. * @var \SimpleXMLElement $element
  46. */
  47. foreach ($xml->reference as $element) {
  48. if (!$this->_isAttributeSet($element, 'type')) {
  49. throw new InvalidMappingException("Reference type (document or entity) is not set in class - {$meta->name}");
  50. }
  51. $type = $this->_getAttribute($element, 'type');
  52. if (!in_array($type, $this->validTypes)) {
  53. throw new InvalidMappingException(
  54. $type .
  55. ' is not a valid reference type, valid types are: ' .
  56. implode(', ', $this->validTypes)
  57. );
  58. }
  59. $reference = $this->_getAttribute($element, 'reference');
  60. if (!in_array($reference, $this->validReferences)) {
  61. throw new InvalidMappingException(
  62. $reference .
  63. ' is not a valid reference, valid references are: ' .
  64. implode(', ', $this->validReferences)
  65. );
  66. }
  67. if (!$this->_isAttributeSet($element, 'field')) {
  68. throw new InvalidMappingException("Reference field is not set in class - {$meta->name}");
  69. }
  70. $field = $this->_getAttribute($element, 'field');
  71. if (!$this->_isAttributeSet($element, 'class')) {
  72. throw new InvalidMappingException("Reference field is not set in class - {$meta->name}");
  73. }
  74. $class = $this->_getAttribute($element, 'class');
  75. if (!$this->_isAttributeSet($element, 'identifier')) {
  76. throw new InvalidMappingException("Reference identifier is not set in class - {$meta->name}");
  77. }
  78. $identifier = $this->_getAttribute($element, 'identifier');
  79. $config[$reference][$field] = array(
  80. 'field' => $field,
  81. 'type' => $type,
  82. 'class' => $class,
  83. 'identifier' => $identifier
  84. );
  85. if (!$this->_isAttributeSet($element, 'mappedBy')) {
  86. $config[$reference][$field]['mappedBy'] = $this->_getAttribute($element, 'mappedBy');
  87. }
  88. if (!$this->_isAttributeSet($element, 'inversedBy')) {
  89. $config[$reference][$field]['inversedBy'] = $this->_getAttribute($element, 'inversedBy');
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }