Xml.php 2.9 KB

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