*
* @var array
*/
protected $attributes;
/**
* true :
* false :
*
* @var bool
*/
protected $doubleTag = true;
/**
* tag name
* <$tag>$tag>
* @var string
*/
protected $tag = "html";
/**
* value between tags
*
* $value
*
* @var string
*/
protected $value;
/**
* raw value to add
*
* @var string
*/
protected $toAdd;
public function __construct()
{
$this->attributes = array();
}
/* getters */
/**
* get array of attribute
* if $name != null && valid attribute name return value for given attribute name
* return false if not found
*
* @param null|string $name
* @param bool $default
* @return array|bool
*/
public function getAttributes($name = null, $default = false)
{
if(is_null($name))
return $this->attributes;
if(isset($this->attributes[$name]))
return $this->attributes[$name];
return $default;
}
/**
* return meta built
*
* @param bool $newline
* @param string $ending default ">"
* @return string
*/
public function build($newline = true, $ending = ">"){
$attr = " ";
$toAdd = $this->toAdd;
if(!is_null($toAdd))
$toAdd = " ".$toAdd;
else
$toAdd = "";
foreach($this->attributes as $name => $value){
if(!empty($value) && $value != 'TRUE')
$attr .= $name.'="'.$value.'" ';
else
$attr .= " ".$name;
}
if($newline)
$nl = PHP_EOL;
else
$nl = "";
if(!$this->doubleTag)
return '<'.$this->tag.$attr.$toAdd.$ending.$nl;
else {
if(empty($this->value))
$this->value = "";
return '<'.$this->tag.$attr.$toAdd.'>'.$this->value.''.$this->tag.'>'.$nl;
}
}
/**
* @return bool
*/
public function isDoubleTag(){
return $this->doubleTag;
}
/* setters */
/**
* @param $name array|string
* @param $value string
* @return $this
*/
public function setAttributes($name, $value = "")
{
if(is_array($name))
$this->attributes = $name;
if(!empty($name))
$this->attributes[(string)$name] = $value;
return $this;
}
/**
* Remove attribute with key == $label and return true,
* return false if not found
*
* @param $label string
* @return bool
*/
public function removeAttribute($label){
foreach($this->attributes as $name => $value){
if($name == $label){
unset($this->attributes[$name]);
return true;
}
}
return false;
}
/**
* @param $bool
* @return $this
*/
public function setDoubleTag($bool){
$this->doubleTag = (boolean)$bool;
return $this;
}
/**
* @return string
*/
public function getToAdd()
{
return $this->toAdd;
}
/**
* @param string $toAdd
* @return $this
*/
public function setToAdd($toAdd)
{
$this->toAdd = $toAdd;
return $this;
}
}