PageRenderTime 145ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Api/Model/Wsdl/Config/Element.php

https://bitbucket.org/jokusafet/magento2
PHP | 265 lines | 148 code | 23 blank | 94 comment | 33 complexity | 5be43aefacd467b7713d809ec578164b 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_Api
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Wsdl element model
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. */
  32. class Mage_Api_Model_Wsdl_Config_Element extends Varien_Simplexml_Element
  33. {
  34. public function extend($source, $overwrite=false)
  35. {
  36. if (!$source instanceof Varien_Simplexml_Element) {
  37. return $this;
  38. }
  39. foreach ($this->getChildren($source) as $namespace => $children) {
  40. foreach ($children as $child) {
  41. $this->extendChild($child, $overwrite, $namespace);
  42. }
  43. }
  44. return $this;
  45. }
  46. /**
  47. * Extends one node
  48. *
  49. * @param Varien_Simplexml_Element $source
  50. * @param boolean $overwrite
  51. * @return Varien_Simplexml_Element
  52. */
  53. public function extendChild($source, $overwrite=false, $elmNamespace = '')
  54. {
  55. // this will be our new target node
  56. $targetChild = null;
  57. // name of the source node
  58. $sourceName = $source->getName();
  59. // here we have children of our source node
  60. $sourceChildren = $this->getChildren($source);
  61. if ($elmNamespace == '') {
  62. $elmNamespace = null;
  63. }
  64. if (!$source->hasChildren()) {
  65. // handle string node
  66. $elm = $this->getElementByName($source, $elmNamespace);
  67. if (!is_null($elm)) {
  68. // if target already has children return without regard
  69. if ($this->getChildren($elm)) {
  70. return $this;
  71. }
  72. if ($overwrite) {
  73. // unset($this->$sourceName);
  74. unset($elm);
  75. } else {
  76. return $this;
  77. }
  78. }
  79. $targetChild = $this->addChild($sourceName, $source->xmlentities(), $elmNamespace);
  80. $targetChild->setParent($this);
  81. foreach ($this->getAttributes($source) as $namespace => $attributes) {
  82. foreach ($attributes as $key => $value) {
  83. $_namespacesPrefix = Mage_Api_Model_Wsdl_Config::getNamespacesPrefix();
  84. if ($namespace == '') {
  85. $namespace = null;
  86. } elseif (array_key_exists($namespace, $_namespacesPrefix)) {
  87. $key = $_namespacesPrefix[$namespace] . ':' . $key;
  88. }
  89. $targetChild->addAttribute($key, $this->xmlentities($value), $namespace);
  90. }
  91. }
  92. return $this;
  93. }
  94. $elm = $this->getElementByName($source, $elmNamespace);
  95. if (!is_null($elm)) {
  96. $targetChild = $elm;
  97. }
  98. if (is_null($targetChild)) {
  99. // if child target is not found create new and descend
  100. $targetChild = $this->addChild($sourceName, null, $elmNamespace);
  101. $targetChild->setParent($this);
  102. foreach ($this->getAttributes($source) as $namespace => $attributes) {
  103. foreach ($attributes as $key => $value) {
  104. $_namespacesPrefix = Mage_Api_Model_Wsdl_Config::getNamespacesPrefix();
  105. if ($namespace == '') {
  106. $namespace = null;
  107. } elseif (array_key_exists($namespace, $_namespacesPrefix)) {
  108. $key = $_namespacesPrefix[$namespace] . ':' . $key;
  109. }
  110. $targetChild->addAttribute($key, $this->xmlentities($value), $namespace);
  111. }
  112. }
  113. }
  114. foreach ($sourceChildren as $elmNamespace => $children) {
  115. foreach ($children as $childKey => $childNode) {
  116. $targetChild->extendChild($childNode, $overwrite, $elmNamespace);
  117. }
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Return attributes of all namespaces
  123. *
  124. * array(
  125. * namespace => array(
  126. * attribute_key => attribute_value,
  127. * ...
  128. * )
  129. * )
  130. *
  131. * @param Varien_Simplexml_Element $source
  132. * @return array
  133. */
  134. public function getAttributes($source, $namespace = null)
  135. {
  136. $attributes = array();
  137. if (!is_null($namespace)) {
  138. $attributes[$namespace] = $source->attributes($namespace);
  139. return $attributes;
  140. }
  141. $namespaces = $source->getNamespaces(true);
  142. $attributes[''] = $source->attributes('');
  143. foreach ($namespaces as $key => $value) {
  144. if ($key == '' || $key == 'soap') {
  145. continue;
  146. }
  147. $attributes[$value] = $source->attributes($value);
  148. }
  149. return $attributes;
  150. }
  151. /**
  152. * Return children of all namespaces
  153. *
  154. * @param Varien_Simplexml_Element $source
  155. */
  156. public function getChildren($source)
  157. {
  158. $children = array();
  159. $namespaces = $source->getNamespaces(true);
  160. $isWsi = Mage::helper('Mage_Api_Helper_Data')->isComplianceWSI();
  161. foreach ($namespaces as $key => $value) {
  162. if ($key == '' || (!$isWsi && $key == 'wsdl')) {
  163. continue;
  164. }
  165. $children[$value] = $source->children($value);
  166. }
  167. $children[''] = $source->children('');
  168. return $children;
  169. }
  170. /**
  171. * Return if has children
  172. *
  173. * @return boolean
  174. */
  175. public function hasChildren()
  176. {
  177. if (!$this->getChildren($this)) {
  178. return false;
  179. }
  180. // simplexml bug: @attributes is in children() but invisible in foreach
  181. foreach ($this->getChildren($this) as $namespace => $children) {
  182. foreach ($children as $k => $child) {
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. /**
  189. * Return element by tag name, and checking attributes with namespaces
  190. *
  191. * @param Varien_Simplexml_Element $source
  192. * @param string $namespace
  193. * @return null|Varien_Simplexml_Element
  194. */
  195. public function getElementByName($source, $elmNamespace = '')
  196. {
  197. $sourceName = $source->getName();
  198. $extendElmAttributes = $this->getAttributes($source);
  199. foreach ($this->children($elmNamespace) as $k => $child) {
  200. if ($child->getName() == $sourceName) {
  201. $elm = true;
  202. foreach ($extendElmAttributes as $namespace => $attributes) {
  203. /**
  204. * if count of attributes of extend element is 0 in $namespace,
  205. * and current element has attributes in $namespace - different elements
  206. */
  207. // if (!count($attributes) && count($this->getAttributes($child, $namespace))) {
  208. // foreach ($this->getAttributes($child, $namespace) as $attribute) {
  209. // $elm = false;
  210. // break;
  211. // }
  212. // }
  213. foreach ($attributes as $key => $value) {
  214. if (is_null($child->getAttribute($key, $namespace)) || $child->getAttribute($key, $namespace) != $value) {
  215. $elm = false;
  216. }
  217. }
  218. }
  219. /**
  220. * if count of namespaces attributes of element to extend is 0,
  221. * but current element has namespaces attributes - different elements
  222. */
  223. if (!count($extendElmAttributes) && count($this->getAttributes($child))) {
  224. $elm = false;
  225. }
  226. if ($elm) {
  227. return $child;
  228. }
  229. }
  230. }
  231. return null;
  232. }
  233. /**
  234. * Returns attribute value by attribute name
  235. *
  236. * @return string
  237. */
  238. public function getAttribute($name, $namespace = ''){
  239. $attrs = $this->attributes($namespace);
  240. return isset($attrs[$name]) ? (string)$attrs[$name] : null;
  241. }
  242. }