esXmlNode
Codice PHP:
<?php

/**
 * esXmlNode class
 * This class is a rappresentation of a xml node
 *
 * @author Davide Borsatto
 * @version 0.8
 * @package xml 
 */        

class esXmlNode
{

  
/**
   * The name of the node
   * @access private
   * @var string
   */
  
private $name '';

  
/**
   * The attributes of the node, in a key => value form
   * @access private
   * @var array
   */
  
private $attributes = array();

  
/**
   * The nodes sons of this one
   * @access private
   * @var array
   */
  
private $sons = array();

  
/**
   * The value
   * @access private
   * @var string
   */
  
private $value '';

  
/**
   * If the node must print the cdata string while converted to xml
   * @access private
   * @var boolean
   */
  
private $cdata false;

  
/**
   * If the node must be printed in the short mode (<node />) instead of the default (<node></node>)
   * @access private
   * @var boolean
   */
  
private $shortIfEmpty true;

  
/**
   * The parent (if exists) of this node
   * @access private
   * @var object
   */
  
private $parent null;

  
/**
   * Contructor, it can set up {@link $name} and the {@link $parent}
   * @access public
   * @param string $name The name of the node
   * @param array $options An array with default options
   */
  
public function __construct($name ''$options = array())
  {
    
$this->setName($name);
    
$defaultOptions = array
    (
      
'parent'         => null,
      
'short_if_empty' => true,
      
'cdata'          => false,
      
'value'          => ''
    
);
    
$options array_merge($defaultOptions$options);
    
$this->setShortIfEmpty($options['short_if_empty']);
    
$this->setCdata($options['cdata']);
    
$this->setValue($options['value']);
    if (
$options['parent'] instanceof esXmlNode)
    {
      
$this->setParent($options['parent']);
    }
  }

  
/**
   * Gets a string rappresentation of the node
   * @access public
   * @return string the xml rappresentation of the node      
   */     
  
public function __toString()
  {
    return 
$this->toXml();
  }

  
/**
   * Sets the {@link $name} of the node
   * @param string $name The name of the node   
   */
  
public function setName($name)
  {
    
$this->name $name;
  }

  
/**
   * Gets the {@link $name} of the node
   * @access public
   * @return string The name of the node
   */           
  
public function getName()
  {
    return 
$this->name;
  }

  
/**
   * Sets the {@link $value} of the node
   * @access public
   * @param string $value The value of the node
   */           
  
public function setValue($value)
  {
    
$this->value $value;
  }

  
/**
   * Gets the {@link $value} of the node
   * @access public
   * @return string The value of the node
   */           
  
public function getValue()
  {
    return 
$this->value;
  }

  
/**
   * If the node must print the cdata string while converted to xml
   * @access public
   * @param boolean $cdata If the cdata string must be printed
   */           
  
public function setCdata($cdata)
  {
    
$this->cdata $cdata;
  }

  
/**
   * Sets if the short tag mode must be used in conversion to xml
   * @access public
   * @param boolean $shortIfEmpty If the short tag mode must be used in conversion to xml
   */           
  
public function setShortIfEmpty($shortIfEmpty)
  {
    
$this->shortIfEmpty $shortIfEmpty;
  }

  
/**
   * Returns true if the node has at least one son
   * @access public   
   * @return boolean If the node has sons or not
   */        
  
public function hasSons()
  {
    return 
count($this->sons) > 0;
  }

  
/**
   * Returns true if the node is a leaf (it does not have any sons)
   * @access public
   * @return boolean If the node is a leaf or not
   */
  
public function isLeaf()
  {
    return !
$this->hasSons();
  }

  
/**
   * Adds a son to the current node
   * @access public
   * @param XmlNode $node The son of the current node
   */           
  
public function addSon(esXmlNode $node)
  {
    
$node->setParent($this);
    
$this->sons[] = $node;
  }

  
/**
   * Adds an array of XmlNode nodes
   * @access public
   * @param array $nodes An XmlNode array
   */   
  
public function addSons(array $nodes)
  {
    foreach (
$nodes as $node)
    {
      if (
$node instanceof esXmlNode)
      {
        
$this->addSon($node);
      }
    }
  }

  
/**
   * Returns the sons of the current node
   * @access public
   * @return array The sons of the current node
   */           
  
public function getSons()
  {
    return 
$this->sons;
  }

  
/**
   * Sets an attribute of the current node
   * @access public
   * @param string $name The name of the attribute
   * @param string $value The value of the attribute
   */              
  
public function setAttribute($name$value)
  {
    
$this->attributes[$name] = $value;
  }

  
/**
   * Returns if the node has an attribute or not
   * @access public
   * @param mixed $name The name of the attribute
   * @return boolean If the node has the requested attribute
  */
  
public function hasAttribute($name)
  {
    return 
Arrays::has($this->attributes$name);
  }


  
// ...