PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_comprofiler/library/cb/xml/cb.xml.simplexml.php

https://github.com/Shigaru/shigaru
PHP | 134 lines | 72 code | 9 blank | 53 comment | 26 complexity | e5613454cdd8dfe2989fbe35b9e71354 MD5 | raw file
  1. <?php
  2. /**
  3. * Abstraction class for PHP SimpleXMLElement for PHP 4 and 5, including < 5.1.3
  4. * @version $Id: cb.xml.simplexml.php 1183 2010-09-25 23:16:33Z beat $
  5. * @author Beat
  6. * @copyright (C) 2007 Beat and Lightning MultiCom SA, 1009 Pully, Switzerland
  7. * @license Lightning Proprietary. See licence. Allowed for free use within CB and for CB plugins.
  8. */
  9. // Check to ensure this file is within the rest of the framework
  10. if ( ! ( defined( '_VALID_CB' ) || defined( '_JEXEC' ) || defined( '_VALID_MOS' ) ) ) { die( 'Direct Access to this location is not allowed.' ); }
  11. //define('CBXML_TEST_CBXML','');
  12. //define('JXML_TEST_DOMIT', '');
  13. define('CB_PHP_XML', class_exists( 'SimpleXMLElement' ) && ( version_compare( phpversion(), '5.1.3', '>=' ) ) && ( ! @ini_get( 'zend.ze1_compatibility_mode' ) ) && ! defined('CBXML_TEST_CBXML') );
  14. if ( CB_PHP_XML ) {
  15. cbimport( 'cb.xml.xml' );
  16. } else {
  17. cbimport( 'cb.xml.domit' );
  18. }
  19. /**
  20. * SimpleXML Element extended for CB.
  21. *
  22. */
  23. class CBSimpleXMLElement extends FixedSimpleXML {
  24. /**
  25. * Get the first child element in matching all the attiributes $attributes
  26. *
  27. * @param string $name The name tag of the element searched
  28. * @param array $attributes array of attribute => value which must match also
  29. * @return CBSimpleXMLElement or false if no child matches
  30. */
  31. function &getChildByNameAttributes( $name, $attributes = null ) {
  32. if ( $attributes === null ) {
  33. $attributes = array();
  34. }
  35. foreach ( $this->children() as $child ) {
  36. if ( $child->name() == $name ) {
  37. $found = true;
  38. foreach ( $attributes as $atr => $val ) {
  39. if ( $child->attributes( $atr ) != $val ) {
  40. $found = false;
  41. break;
  42. }
  43. }
  44. if ( $found ) {
  45. return $child;
  46. }
  47. }
  48. }
  49. $false = false;
  50. return $false;
  51. }
  52. /**
  53. * Get the first child element in matching the attiribute
  54. *
  55. * @param string $name The name tag of the element searched
  56. * @param string $attribute Attribute name to check
  57. * @param string $value Attribute value which must also match
  58. * @return CBSimpleXMLElement or false if no child matches
  59. */
  60. function &getChildByNameAttr( $name, $attribute, $value = null ) {
  61. foreach ( $this->children() as $child ) {
  62. if ( $child->name() == $name ) {
  63. if ( $child->attributes( $attribute ) == $value ) {
  64. return $child;
  65. }
  66. }
  67. }
  68. $false = false;
  69. return $false;
  70. }
  71. /**
  72. * Get the first child or childs' child (recursing) element in matching the attiribute
  73. *
  74. * @param string $name The name tag of the element searched
  75. * @param string $attribute Attribute name to check
  76. * @param string $value Attribute value which must also match
  77. * @return CBSimpleXMLElement or false if no child matches
  78. */
  79. function &getAnyChildByNameAttr( $name, $attribute, $value = null ) {
  80. $children = $this->children(); // this is needed due to a bug in PHP 4.4.2 where you can have only 1 iterator per array reference, so doing second iteration on same array within first iteration kills this.
  81. foreach ( $children as $child ) {
  82. if ( $child->name() == $name ) {
  83. if ( $child->attributes( $attribute ) == $value ) {
  84. return $child;
  85. }
  86. }
  87. if ( count( $child->children() ) > 0 ) {
  88. $grandchild = $child->getAnyChildByNameAttr( $name, $attribute, $value ); // recurse
  89. if ( $grandchild ) {
  90. return $grandchild;
  91. }
  92. }
  93. }
  94. $false = false;
  95. return $false;
  96. }
  97. /**
  98. * Appends (copies) a child $source and all its descendants to $this node
  99. * @since 1.2.4
  100. *
  101. * @param CBSimpleXMLElement $source
  102. * @param callback $callBack to check/transform data or attributes of a node: $destinationData = function ( string|array $sourceData, CBSimpleXMLElement $sourceNode, CBSimpleXMLElement $destinationParentNode );
  103. */
  104. function & addChildWithDescendants( &$source, $callBack = null ) {
  105. if ( $callBack === null ) {
  106. $child = $this->addChildWithAttr( $source->name(), $source->data(), null, $source->attributes() );
  107. } else {
  108. $child = $this->addChildWithAttr( $source->name(), call_user_func_array( $callBack, array( $source->data(), $source, $this ) ), null, call_user_func_array( $callBack, array( $source->attributes(), $source, $this ) ) );
  109. }
  110. foreach ( $source->children() as $sourceChild ) {
  111. $child->addChildWithDescendants( $sourceChild, $callBack );
  112. }
  113. return $child;
  114. }
  115. /* THIS MOVED ONE LEVEL DOWN TO PHP-specific implementations !!!
  116. *
  117. * Get an element in the document by / separated path
  118. * or FALSE
  119. *
  120. * @param string $path The / separated path to the element
  121. * @return CBSimpleXMLElement or FALSE
  122. */
  123. // function & getElementByPath( $path ) {
  124. }
  125. ?>