Xml.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Gedmo\Mapping\Driver;
  3. use Gedmo\Mapping\Driver;
  4. use Gedmo\Exception\InvalidMappingException;
  5. use SimpleXMLElement;
  6. /**
  7. * The mapping XmlDriver abstract class, defines the
  8. * metadata extraction function common among all
  9. * all drivers used on these extensions by file based
  10. * drivers.
  11. *
  12. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. abstract class Xml extends File
  17. {
  18. const GEDMO_NAMESPACE_URI = 'http://gediminasm.org/schemas/orm/doctrine-extensions-mapping';
  19. const DOCTRINE_NAMESPACE_URI = 'http://doctrine-project.org/schemas/orm/doctrine-mapping';
  20. /**
  21. * File extension
  22. * @var string
  23. */
  24. protected $_extension = '.dcm.xml';
  25. /**
  26. * Get attribute value.
  27. * As we are supporting namespaces the only way to get to the attributes under a node is to use attributes function on it
  28. *
  29. * @param SimpleXMLElement $node
  30. * @param string $attributeName
  31. *
  32. * @return string
  33. */
  34. protected function _getAttribute(SimpleXmlElement $node, $attributeName)
  35. {
  36. $attributes = $node->attributes();
  37. return (string) $attributes[$attributeName];
  38. }
  39. /**
  40. * Get boolean attribute value.
  41. * As we are supporting namespaces the only way to get to the attributes under a node is to use attributes function on it
  42. *
  43. * @param SimpleXMLElement $node
  44. * @param string $attributeName
  45. *
  46. * @return boolean
  47. */
  48. protected function _getBooleanAttribute(SimpleXmlElement $node, $attributeName)
  49. {
  50. $rawValue = strtolower($this->_getAttribute($node, $attributeName));
  51. if ($rawValue === '1' || $rawValue === 'true') {
  52. return true;
  53. }
  54. if ($rawValue === '0' || $rawValue === 'false') {
  55. return false;
  56. }
  57. throw new InvalidMappingException(sprintf("Attribute %s must have a valid boolean value, '%s' found", $attributeName, $this->_getAttribute($node, $attributeName)));
  58. }
  59. /**
  60. * does attribute exist under a specific node
  61. * As we are supporting namespaces the only way to get to the attributes under a node is to use attributes function on it
  62. *
  63. * @param SimpleXMLElement $node
  64. * @param string $attributeName
  65. *
  66. * @return string
  67. */
  68. protected function _isAttributeSet(SimpleXmlElement $node, $attributeName)
  69. {
  70. $attributes = $node->attributes();
  71. return isset($attributes[$attributeName]);
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. protected function _loadMappingFile($file)
  77. {
  78. $result = array();
  79. $xmlElement = simplexml_load_file($file);
  80. $xmlElement = $xmlElement->children(self::DOCTRINE_NAMESPACE_URI);
  81. if (isset($xmlElement->entity)) {
  82. foreach ($xmlElement->entity as $entityElement) {
  83. $entityName = $this->_getAttribute($entityElement, 'name');
  84. $result[$entityName] = $entityElement;
  85. }
  86. } elseif (isset($xmlElement->{'mapped-superclass'})) {
  87. foreach ($xmlElement->{'mapped-superclass'} as $mappedSuperClass) {
  88. $className = $this->_getAttribute($mappedSuperClass, 'name');
  89. $result[$className] = $mappedSuperClass;
  90. }
  91. }
  92. return $result;
  93. }
  94. }