00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Varien_Simplexml_Element extends SimpleXMLElement
00036 {
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 protected $_parent = null;
00048
00049
00050
00051
00052
00053
00054 public function setParent($element)
00055 {
00056 #$this->_parent = $element;
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066 public function getParent()
00067 {
00068 if (!empty($this->_parent)) {
00069 $parent = $this->_parent;
00070 } else {
00071 $arr = $this->xpath('..');
00072 $parent = $arr[0];
00073 }
00074 return $parent;
00075 }
00076
00077
00078
00079
00080
00081
00082 public function hasChildren()
00083 {
00084 if (!$this->children()) {
00085 return false;
00086 }
00087
00088
00089 foreach ($this->children() as $k=>$child) {
00090 return true;
00091 }
00092 return false;
00093 }
00094
00095
00096
00097
00098
00099
00100 public function getAttribute($name){
00101 $attrs = $this->attributes();
00102 return isset($attrs[$name]) ? (string)$attrs[$name] : null;
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 public function descend($path)
00148 {
00149 #$node = $this->xpath($path);
00150 #return $node[0];
00151 if (is_array($path)) {
00152 $pathArr = $path;
00153 } else {
00154 $pathArr = explode('/', $path);
00155 }
00156 $desc = $this;
00157 foreach ($pathArr as $nodeName) {
00158 if (strpos($nodeName, '@')!==false) {
00159 $a = explode('@', $nodeName);
00160 $b = explode('=', $a[1]);
00161 $nodeName = $a[0];
00162 $attributeName = $b[0];
00163 $attributeValue = $b[1];
00164 $found = false;
00165 foreach ($this->$nodeName as $desc) {
00166 if ((string)$nodeChild[$attributeName]===$attributeValue) {
00167 $found = true;
00168 break;
00169 }
00170 }
00171 if (!$found) {
00172 $desc = false;
00173 }
00174 } else {
00175 $desc = $desc->$nodeName;
00176 }
00177 if (!$desc) {
00178 return false;
00179 }
00180 }
00181 return $desc;
00182 }
00183
00184
00185
00186
00187
00188
00189 public function asArray()
00190 {
00191 $r = array();
00192
00193 $attributes = $this->attributes();
00194 foreach($attributes as $k=>$v) {
00195 if ($v) $r['@'][$k] = (string) $v;
00196 }
00197
00198 if (!($children = $this->children())) {
00199 $r = (string) $this;
00200 return $r;
00201 }
00202
00203 foreach($children as $childName=>$child) {
00204 $r[$childName] = array();
00205 foreach ($child as $index=>$element) {
00206 $r[$childName][$index] = $element->asArray();
00207 }
00208 }
00209
00210 return $r;
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220 public function asNiceXml($filename='', $level=0)
00221 {
00222 if (is_numeric($level)) {
00223 $pad = str_pad('', $level*3, ' ', STR_PAD_LEFT);
00224 $nl = "\n";
00225 } else {
00226 $pad = '';
00227 $nl = '';
00228 }
00229
00230 $out = $pad.'<'.$this->getName();
00231
00232 if ($attributes = $this->attributes()) {
00233 foreach ($attributes as $key=>$value) {
00234 $out .= ' '.$key.'="'.str_replace('"', '\"', (string)$value).'"';
00235 }
00236 }
00237
00238 if ($this->hasChildren()) {
00239 $out .= '>'.$nl;
00240 foreach ($this->children() as $child) {
00241 $out .= $child->asNiceXml('', is_numeric($level) ? $level+1 : true);
00242 }
00243 $out .= $pad.'</'.$this->getName().'>'.$nl;
00244 } else {
00245 $value = (string)$this;
00246 if (strlen($value)) {
00247 $out .= '>'.$this->xmlentities($value).'</'.$this->getName().'>'.$nl;
00248 } else {
00249 $out .= '/>'.$nl;
00250 }
00251 }
00252
00253 if ((0===$level || false===$level) && !empty($filename)) {
00254 file_put_contents($filename, $out);
00255 }
00256
00257 return $out;
00258 }
00259
00260
00261
00262
00263
00264
00265
00266 public function innerXml($level=0)
00267 {
00268 $out = '';
00269 foreach ($this->children() as $child) {
00270 $out .= $child->asNiceXml($level);
00271 }
00272 return $out;
00273 }
00274
00275
00276
00277
00278
00279
00280
00281 public function xmlentities($value='')
00282 {
00283 if (empty($value)) {
00284 $value = $this;
00285 }
00286 $value = (string)$value;
00287
00288 $value = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $value);
00289
00290 return $value;
00291 }
00292
00293
00294
00295
00296
00297
00298
00299 public function appendChild($source)
00300 {
00301 if ($source->children()) {
00302
00303
00304
00305 if (version_compare(phpversion(), '5.2.4', '<')===true) {
00306 $name = $source->children()->getName();
00307 }
00308 else {
00309 $name = $source->getName();
00310 }
00311 $child = $this->addChild($name);
00312 } else {
00313 $child = $this->addChild($source->getName(), $this->xmlentities($source));
00314 }
00315 $child->setParent($this);
00316
00317 $attributes = $source->attributes();
00318 foreach ($attributes as $key=>$value) {
00319 $child->addAttribute($key, $this->xmlentities($value));
00320 }
00321
00322 foreach ($source->children() as $sourceChild) {
00323 $child->appendChild($sourceChild);
00324 }
00325 return $this;
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 public function extend($source, $overwrite=false)
00339 {
00340 if (!$source instanceof Varien_Simplexml_Element) {
00341 return $this;
00342 }
00343
00344 foreach ($source->children() as $child) {
00345 $this->extendChild($child, $overwrite);
00346 }
00347
00348 return $this;
00349 }
00350
00351
00352
00353
00354
00355
00356
00357
00358 public function extendChild($source, $overwrite=false)
00359 {
00360
00361 $targetChild = null;
00362
00363
00364 $sourceName = $source->getName();
00365
00366
00367 $sourceChildren = $source->children();
00368
00369 if (!$source->hasChildren()) {
00370
00371 if (isset($this->$sourceName)) {
00372
00373 if ($this->$sourceName->children()) {
00374 return $this;
00375 }
00376 if ($overwrite) {
00377 unset($this->$sourceName);
00378 } else {
00379 return $this;
00380 }
00381 }
00382
00383 $targetChild = $this->addChild($sourceName, $source->xmlentities());
00384 $targetChild->setParent($this);
00385 foreach ($source->attributes() as $key=>$value) {
00386 $targetChild->addAttribute($key, $this->xmlentities($value));
00387 }
00388 return $this;
00389 }
00390
00391 if (isset($this->$sourceName)) {
00392 $targetChild = $this->$sourceName;
00393 }
00394
00395 if (is_null($targetChild)) {
00396
00397 $targetChild = $this->addChild($sourceName);
00398 $targetChild->setParent($this);
00399 foreach ($source->attributes() as $key=>$value) {
00400 $targetChild->addAttribute($key, $this->xmlentities($value));
00401 }
00402 }
00403
00404
00405 foreach ($sourceChildren as $childKey=>$childNode) {
00406 $targetChild->extendChild($childNode, $overwrite);
00407 }
00408
00409 return $this;
00410 }
00411
00412 public function setNode($path, $value, $overwrite=true)
00413 {
00414 $arr1 = explode('/', $path);
00415 $arr = array();
00416 foreach ($arr1 as $v) {
00417 if (!empty($v)) $arr[] = $v;
00418 }
00419 $last = sizeof($arr)-1;
00420 $node = $this;
00421 foreach ($arr as $i=>$nodeName) {
00422 if ($last===$i) {
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433 if (!isset($node->$nodeName) || $overwrite) {
00434
00435
00436 if (isset($node->$nodeName) && (version_compare(phpversion(), '5.2.6', '<')===true)) {
00437 $node->$nodeName = $node->xmlentities($value);
00438 } else {
00439 $node->$nodeName = $value;
00440 }
00441 }
00442 } else {
00443 if (!isset($node->$nodeName)) {
00444 $node = $node->addChild($nodeName);
00445 } else {
00446 $node = $node->$nodeName;
00447 }
00448 }
00449
00450 }
00451 return $this;
00452 }
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589 }