Xml.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. * @package Gedmo.Common.Mapping
  14. * @subpackage FileDriver
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. abstract class Xml extends File
  19. {
  20. const GEDMO_NAMESPACE_URI = 'http://gediminasm.org/schemas/orm/doctrine-extensions-mapping';
  21. const DOCTRINE_NAMESPACE_URI = 'http://doctrine-project.org/schemas/orm/doctrine-mapping';
  22. /**
  23. * File extension
  24. * @var string
  25. */
  26. protected $_extension = '.dcm.xml';
  27. /**
  28. * Get attribute value.
  29. * As we are supporting namespaces the only way to get to the attributes under a node is to use attributes function on it
  30. *
  31. * @param SimpleXMLElement $node
  32. * @param string $attributeName
  33. * @return string
  34. */
  35. protected function _getAttribute(SimpleXmlElement $node, $attributeName)
  36. {
  37. $attributes = $node->attributes();
  38. return (string)$attributes[$attributeName];
  39. }
  40. /**
  41. * Get boolean attribute value.
  42. * As we are supporting namespaces the only way to get to the attributes under a node is to use attributes function on it
  43. *
  44. * @param SimpleXMLElement $node
  45. * @param string $attributeName
  46. * @return boolean
  47. */
  48. protected function _getBooleanAttribute(SimpleXmlElement $node, $attributeName)
  49. {
  50. return 'true' === strtolower($this->_getAttribute($node, $attributeName));
  51. }
  52. /**
  53. * does attribute exist under a specific node
  54. * As we are supporting namespaces the only way to get to the attributes under a node is to use attributes function on it
  55. *
  56. * @param SimpleXMLElement $node
  57. * @param string $attributeName
  58. * @return string
  59. */
  60. protected function _isAttributeSet(SimpleXmlElement $node, $attributeName)
  61. {
  62. $attributes = $node->attributes();
  63. return isset($attributes[$attributeName]);
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. protected function _loadMappingFile($file)
  69. {
  70. $result = array();
  71. $xmlElement = simplexml_load_file($file);
  72. $xmlElement = $xmlElement->children(self::DOCTRINE_NAMESPACE_URI);
  73. if (isset($xmlElement->entity)) {
  74. foreach ($xmlElement->entity as $entityElement) {
  75. $entityName = $this->_getAttribute($entityElement, 'name');
  76. $result[$entityName] = $entityElement;
  77. }
  78. } else if (isset($xmlElement->{'mapped-superclass'})) {
  79. foreach ($xmlElement->{'mapped-superclass'} as $mappedSuperClass) {
  80. $className = $this->_getAttribute($mappedSuperClass, 'name');
  81. $result[$className] = $mappedSuperClass;
  82. }
  83. }
  84. return $result;
  85. }
  86. }