1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33:
34: class Mage_Api_Model_Wsdl_Config_Element extends Varien_Simplexml_Element
35: {
36: public function extend($source, $overwrite=false)
37: {
38: if (!$source instanceof Varien_Simplexml_Element) {
39: return $this;
40: }
41:
42: foreach ($this->getChildren($source) as $namespace => $children) {
43: foreach ($children as $child) {
44: $this->extendChild($child, $overwrite, $namespace);
45: }
46: }
47:
48: return $this;
49: }
50:
51: 52: 53: 54: 55: 56: 57:
58: public function extendChild($source, $overwrite=false, $elmNamespace = '')
59: {
60:
61: $targetChild = null;
62:
63:
64: $sourceName = $source->getName();
65:
66:
67: $sourceChildren = $this->getChildren($source);
68:
69: if ($elmNamespace == '') {
70: $elmNamespace = null;
71: }
72:
73: if (!$source->hasChildren()) {
74:
75: $elm = $this->getElementByName($source, $elmNamespace);
76: if (!is_null($elm)) {
77:
78:
79: if ($this->getChildren($elm)) {
80: return $this;
81: }
82: if ($overwrite) {
83:
84: unset($elm);
85: } else {
86: return $this;
87: }
88: }
89: $targetChild = $this->addChild($sourceName, $source->xmlentities(), $elmNamespace);
90: $targetChild->setParent($this);
91: foreach ($this->getAttributes($source) as $namespace => $attributes) {
92: foreach ($attributes as $key => $value) {
93: $_namespacesPrefix = Mage_Api_Model_Wsdl_Config::getNamespacesPrefix();
94: if ($namespace == '') {
95: $namespace = null;
96: } elseif (array_key_exists($namespace, $_namespacesPrefix)) {
97: $key = $_namespacesPrefix[$namespace] . ':' . $key;
98: }
99:
100: $targetChild->addAttribute($key, $this->xmlentities($value), $namespace);
101: }
102: }
103: return $this;
104: }
105:
106: $elm = $this->getElementByName($source, $elmNamespace);
107: if (!is_null($elm)) {
108: $targetChild = $elm;
109: }
110: if (is_null($targetChild)) {
111:
112: $targetChild = $this->addChild($sourceName, null, $elmNamespace);
113: $targetChild->setParent($this);
114: foreach ($this->getAttributes($source) as $namespace => $attributes) {
115: foreach ($attributes as $key => $value) {
116: $_namespacesPrefix = Mage_Api_Model_Wsdl_Config::getNamespacesPrefix();
117: if ($namespace == '') {
118: $namespace = null;
119: } elseif (array_key_exists($namespace, $_namespacesPrefix)) {
120: $key = $_namespacesPrefix[$namespace] . ':' . $key;
121: }
122: $targetChild->addAttribute($key, $this->xmlentities($value), $namespace);
123: }
124: }
125: }
126:
127: foreach ($sourceChildren as $elmNamespace => $children) {
128: foreach ($children as $childKey => $childNode) {
129: $targetChild->extendChild($childNode, $overwrite, $elmNamespace);
130: }
131: }
132:
133: return $this;
134: }
135:
136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148:
149: public function getAttributes($source, $namespace = null)
150: {
151: $attributes = array();
152: if (!is_null($namespace)) {
153: $attributes[$namespace] = $source->attributes($namespace);
154: return $attributes;
155: }
156: $namespaces = $source->getNamespaces(true);
157: $attributes[''] = $source->attributes('');
158: foreach ($namespaces as $key => $value) {
159: if ($key == '' || $key == 'soap') {
160: continue;
161: }
162: $attributes[$value] = $source->attributes($value);
163: }
164: return $attributes;
165: }
166:
167: 168: 169: 170: 171:
172: public function getChildren($source)
173: {
174: $children = array();
175: $namespaces = $source->getNamespaces(true);
176:
177: $isWsi = Mage::helper('api/data')->isComplianceWSI();
178:
179: foreach ($namespaces as $key => $value) {
180: if ($key == '' || (!$isWsi && $key == 'wsdl')) {
181: continue;
182: }
183: $children[$value] = $source->children($value);
184: }
185: $children[''] = $source->children('');
186: return $children;
187: }
188:
189: 190: 191: 192: 193:
194: public function hasChildren()
195: {
196: if (!$this->getChildren($this)) {
197: return false;
198: }
199:
200:
201: foreach ($this->getChildren($this) as $namespace => $children) {
202: foreach ($children as $k => $child) {
203: return true;
204: }
205: }
206: return false;
207: }
208:
209: 210: 211: 212: 213: 214: 215:
216: public function getElementByName($source, $elmNamespace = '')
217: {
218: $sourceName = $source->getName();
219: $extendElmAttributes = $this->getAttributes($source);
220: foreach ($this->children($elmNamespace) as $k => $child) {
221: if ($child->getName() == $sourceName) {
222: $elm = true;
223: foreach ($extendElmAttributes as $namespace => $attributes) {
224: 225: 226: 227:
228:
229:
230:
231:
232:
233:
234: foreach ($attributes as $key => $value) {
235: if (is_null($child->getAttribute($key, $namespace)) || $child->getAttribute($key, $namespace) != $value) {
236: $elm = false;
237: }
238: }
239: }
240: 241: 242: 243:
244: if (!count($extendElmAttributes) && count($this->getAttributes($child))) {
245: $elm = false;
246: }
247: if ($elm) {
248: return $child;
249: }
250: }
251: }
252: return null;
253: }
254:
255: 256: 257: 258: 259:
260: public function getAttribute($name, $namespace = ''){
261: $attrs = $this->attributes($namespace);
262: return isset($attrs[$name]) ? (string)$attrs[$name] : null;
263: }
264:
265: }
266: