Varien_Simplexml_Element Class Reference

Inheritance diagram for Varien_Simplexml_Element:

Mage_Api_Model_Wsdl_Config_Element Mage_Core_Model_Config_Element Mage_Core_Model_Layout_Element

List of all members.

Public Member Functions

 setParent ($element)
 getParent ()
 hasChildren ()
 getAttribute ($name)
 descend ($path)
 asArray ()
 asNiceXml ($filename='', $level=0)
 innerXml ($level=0)
 xmlentities ($value='')
 appendChild ($source)
 extend ($source, $overwrite=false)
 extendChild ($source, $overwrite=false)
 setNode ($path, $value, $overwrite=true)

Protected Attributes

 $_parent = null


Detailed Description

Definition at line 35 of file Element.php.


Member Function Documentation

appendChild ( source  ) 

Appends $source to current node

Parameters:
Varien_Simplexml_Element $source
Returns:
Varien_Simplexml_Element

See also:
http://bugs.php.net/bug.php?id=41867 , fixed in 5.2.4

Definition at line 299 of file Element.php.

00300     {
00301         if ($source->children()) {
00302             /**
00303              * @see http://bugs.php.net/bug.php?id=41867 , fixed in 5.2.4
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     }

asArray (  ) 

Returns the node and children as an array

Returns:
array

Definition at line 189 of file Element.php.

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     }

asNiceXml ( filename = '',
level = 0 
)

Makes nicely formatted XML from the node

Parameters:
string $filename
int|boolean $level if false
Returns:
string

Definition at line 220 of file Element.php.

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     }

descend ( path  ) 

Find a descendant of a node by path

Todo:
Do we need to make it xpath look-a-like?

param string $path Subset of xpath. Example: "child/grand[@attrName='attrValue']/subGrand"

Parameters:
string $path Example: "child/grand@attrName=attrValue/subGrand" (to make it faster without regex)
Returns:
Varien_Simplexml_Element

Definition at line 147 of file Element.php.

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     }

extend ( source,
overwrite = false 
)

Extends current node with xml from $source

If $overwrite is false will merge only missing nodes Otherwise will overwrite existing nodes

Parameters:
Varien_Simplexml_Element $source
boolean $overwrite
Returns:
Varien_Simplexml_Element

Reimplemented in Mage_Api_Model_Wsdl_Config_Element.

Definition at line 338 of file Element.php.

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     }

extendChild ( source,
overwrite = false 
)

Extends one node

Parameters:
Varien_Simplexml_Element $source
boolean $overwrite
Returns:
Varien_Simplexml_Element

Definition at line 358 of file Element.php.

00359     {
00360         // this will be our new target node
00361         $targetChild = null;
00362 
00363         // name of the source node
00364         $sourceName = $source->getName();
00365 
00366         // here we have children of our source node
00367         $sourceChildren = $source->children();
00368 
00369         if (!$source->hasChildren()) {
00370             // handle string node
00371             if (isset($this->$sourceName)) {
00372                 // if target already has children return without regard
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             // if child target is not found create new and descend
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         // finally add our source node children to resulting new target node
00405         foreach ($sourceChildren as $childKey=>$childNode) {
00406             $targetChild->extendChild($childNode, $overwrite);
00407         }
00408 
00409         return $this;
00410     }

getAttribute ( name  ) 

Returns attribute value by attribute name

Returns:
string

Definition at line 100 of file Element.php.

00100                                        {
00101         $attrs = $this->attributes();
00102         return isset($attrs[$name]) ? (string)$attrs[$name] : null;
00103     }

getParent (  ) 

Returns parent node for the element

Currently using xpath

Returns:
Varien_Simplexml_Element

Definition at line 66 of file Element.php.

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     }

hasChildren (  ) 

Enter description here...

Returns:
boolean

Reimplemented in Mage_Api_Model_Wsdl_Config_Element.

Definition at line 82 of file Element.php.

00083     {
00084         if (!$this->children()) {
00085             return false;
00086         }
00087 
00088         // simplexml bug: @attributes is in children() but invisible in foreach
00089         foreach ($this->children() as $k=>$child) {
00090             return true;
00091         }
00092         return false;
00093     }

innerXml ( level = 0  ) 

Enter description here...

Parameters:
int $level
Returns:
string

Definition at line 266 of file Element.php.

00267     {
00268         $out = '';
00269         foreach ($this->children() as $child) {
00270             $out .= $child->asNiceXml($level);
00271         }
00272         return $out;
00273     }

setNode ( path,
value,
overwrite = true 
)

Definition at line 412 of file Element.php.

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                 if (isset($xml->$nodeName)) {
00425                     if ($overwrite) {
00426                         unset($xml->$nodeName);
00427                     } else {
00428                         continue;
00429                     }
00430                 }
00431                 $xml->addChild($nodeName, $xml->xmlentities($value));
00432                 */
00433                 if (!isset($node->$nodeName) || $overwrite) {
00434                     // http://bugs.php.net/bug.php?id=36795
00435                     // comment on [8 Feb 8:09pm UTC]
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     }

setParent ( element  ) 

For future use

Parameters:
Varien_Simplexml_Element $element

Definition at line 54 of file Element.php.

00055     {
00056         #$this->_parent = $element;
00057     }

xmlentities ( value = ''  ) 

Converts meaningful xml characters to xml entities

Parameters:
string 
Returns:
string

Definition at line 281 of file Element.php.

00282     {
00283         if (empty($value)) {
00284             $value = $this;
00285         }
00286         $value = (string)$value;
00287 
00288         $value = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $value);
00289 
00290         return $value;
00291     }


Member Data Documentation

$_parent = null [protected]

Definition at line 47 of file Element.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:25:04 2009 for Magento by  doxygen 1.5.8