1 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| <?php
/**
* PHP AXIOM
*
* @license LGPL
* @author Benjamin DELESPIERRE <benjamin.delespierre@gmail.com>
* @category libAxiom
* @package helper
* $Date: 2011-05-18 17:00:36 +0200 (mer., 18 mai 2011) $
* $Id: BaseHelper.class.php 22988 2011-05-18 15:00:36Z delespierre $
*/
/**
* Base Helper Abstract Class
*
* @abstract
* @author Delespierre
* @version $Rev: 22988 $
* @subpackage BaseHelper
*/
abstract class BaseHelper implements Helper {
/**
* Node's name
* @var string
*/
protected $_node_name;
/**
* Node's value
* @var mixed
*/
protected $_node_value;
/**
* Node's attributes
* @var array
*/
protected $_attributes;
/**
* Ndeo's children
* @var array
*/
protected $_children;
/**
* Default constructor
* @param string $node_name
* @param array $attributes = array()
* @param mixed $node_value = null
*/
public function __construct ($node_name, $attributes = array(), $node_value = null) {
$this->_node_name = $node_name;
$this->_node_value = $node_value;
$this->_attributes = $attributes;
}
/**
* (non-PHPdoc)
* @see Helper::setAttributes()
*/
public function setAttributes ($attributes) {
foreach ($attributes as $key => $value) {
$this->_attributes[$key] = $value;
}
return $this;
}
/**
* (non-PHPdoc)
* @see Helper::setValue()
*/
public function setValue ($value) {
$this->_node_value = $value;
}
/**
* (non-PHPdoc)
* @see Helper::getValue()
*/
public function getValue () {
return $this->_node_value;
}
/**
* __call overloading
* Enable the use of BaseHelper::setX() and BaseHelper::getX()
* where X is an attribute of the current node
* @param string $method
* @param array $args
* @return BaseHelper
*/
public function __call ($method, $args) {
if (strpos($method, 'set') === 0) {
$this->_attributes[lcfirst(substr($method, 3))] = $args[0];
}
elseif (strpos($method, 'get') === 0) {
return isset($this->_attributes[lcfirst(substr($method, 3))]) ? $this->_attributes[lcfirst(substr($method, 3))] : null;
}
return $this;
}
/**
* (non-PHPdoc)
* @see Helper::appendChild()
*/
public function appendChild ($node) {
return $this->_children[] = $node;
}
/**
* (non-PHPdoc)
* @see Helper::__toString()
*/
public function __toString () {
$attr = array();
foreach ($this->_attributes as $name => $value) {
$attr[] = "$name=\"$value\"";
}
$node = "<{$this->_node_name} " . implode(' ', $attr);
if (!count($this->_children) && !$this->_node_value)
return $node . " />";
else
$node .= ">";
if ($this->_node_value)
$node .= $this->_node_value;
if (count($this->_children))
$node .= implode($this->_children);
return $node . "</{$this->_node_name}>";
}
} |
Partager