/app/code/core/Mage/XmlConnect/Model/Simplexml/Element.php

https://github.com/rgranadino/magento-mirror · PHP · 123 lines · 55 code · 7 blank · 61 comment · 6 complexity · 3d259d56502753a9c96a4b60c98b7ab4 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_XmlConnect
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. *
  28. * XmlConnect fixed Varien SimpleXML Element class
  29. *
  30. * @author Magento Core Team <core@magentocommerce.com>
  31. */
  32. class Mage_XmlConnect_Model_Simplexml_Element extends Varien_Simplexml_Element
  33. {
  34. /**
  35. * Appends $source to current node
  36. *
  37. * @param Mage_XmlConnect_Model_Simplexml_Element $source
  38. * @return Mage_XmlConnect_Model_Simplexml_Element
  39. */
  40. public function appendChild($source)
  41. {
  42. if (sizeof($source->children())) {
  43. /**
  44. * @link http://bugs.php.net/bug.php?id=41867 , fixed in 5.2.4
  45. */
  46. if (version_compare(phpversion(), '5.2.4', '<')===true) {
  47. $name = $source->children()->getName();
  48. } else {
  49. $name = $source->getName();
  50. }
  51. $child = $this->addChild($name);
  52. } else {
  53. $child = $this->addChild($source->getName(), $this->xmlentities($source));
  54. }
  55. $child->setParent($this);
  56. $attributes = $source->attributes();
  57. foreach ($attributes as $key=>$value) {
  58. $child->addAttribute($key, $this->xmlAttribute($value));
  59. }
  60. foreach ($source->children() as $sourceChild) {
  61. $child->appendChild($sourceChild);
  62. }
  63. return $this;
  64. }
  65. /**
  66. * Converts meaningful xml character (") to xml attribute specification
  67. *
  68. * @param string $value
  69. * @return string|this
  70. */
  71. public function xmlAttribute($value = null)
  72. {
  73. if (is_null($value)) {
  74. $value = $this;
  75. }
  76. $value = (string)$value;
  77. $value = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $value);
  78. return $value;
  79. }
  80. /**
  81. * Add field to fieldset
  82. *
  83. * @param string $elementName
  84. * @param string $elementType
  85. * @param array $config
  86. * @return Mage_XmlConnect_Model_Simplexml_Element
  87. */
  88. public function addField($elementName, $elementType, array $config)
  89. {
  90. $newField = $this->addChild('field');
  91. $newField->addAttribute('name', $this->xmlAttribute($elementName));
  92. $newField->addAttribute('type', $this->xmlAttribute($elementType));
  93. foreach ($config as $key => $val) {
  94. $newField->addAttribute($key, $this->xmlAttribute($val));
  95. }
  96. return $newField;
  97. }
  98. /**
  99. * Add custom field to SimpleXML element
  100. *
  101. * @param string $childName
  102. * @param string $value
  103. * @param array $config
  104. * @return Mage_XmlConnect_Model_Simplexml_Element
  105. */
  106. public function addCustomChild($childName, $value = null, $config = null)
  107. {
  108. $customFiled = $this->addChild($childName, $this->xmlentities($value));
  109. if (is_array($config)) {
  110. foreach ($config as $key => $val) {
  111. $customFiled->addAttribute($key, $this->xmlAttribute($val));
  112. }
  113. }
  114. return $customFiled;
  115. }
  116. }