Source for file TElement.php
Documentation is available at TElement.php
namespace
Adianti \
Widget \
Base ;
* Base class for all HTML Elements
* @author Pablo Dall'Oglio
* @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
* @license http://www.adianti.com.br/framework-license
private $tagname ; // tag name
private $properties ; // tag properties
private $useSingleQuotes ;
private static $voidelements ;
* @param $tagname tag name
// define the element name
$this -> tagname =
$tagname ;
$this -> useLineBreaks =
TRUE ;
$this -> useSingleQuotes =
FALSE ;
if ( empty ( self :: $voidelements ))
self :: $voidelements =
array ( 'area' , 'base' , 'br' , 'col' , 'command' , 'embed' , 'hr' ,
'img' , 'input' , 'keygen' , 'link' , 'meta' , 'param' , 'source' , 'track' , 'wbr' ) ;
* @param $tagname Element name
* @param $value Element value
* @param $attributes Element attributes
public static function tag ( $tagname , $value , $attributes =
NULL )
$object =
new TElement ( $tagname ) ;
foreach ( $value as $element )
foreach ( $attributes as $att_name =>
$att_value )
$object -> $att_name =
$att_value ;
public function after ( $element )
$this -> afterElement =
$element ;
* Return the after element
return $this -> afterElement ;
* Change the element name
* @param $tagname Element name
$this -> tagname =
$tagname ;
* Define if the element is wrapped inside another one
* @param @bool Boolean TRUE if is wrapped
* Return if the element is wrapped inside another one
* @param $name Property Name
* @param $value Property Value
// objects and arrays are not set as properties
// store the property's value
$this -> properties [ $name ] =
$value ;
foreach ( $properties as $property =>
$value )
unset
( $this -> properties [ $property ] ) ;
$this -> properties [ $property ] =
$value ;
* @param $name property name
return isset
( $this -> properties [ $name ] ) ?
$this -> properties [ $name ] :
null ;
* Return element properties
return $this -> properties ;
* Intercepts whenever someones assign a new property's value
* @param $name Property Name
* @param $value Property Value
public function __set ( $name , $value )
// objects and arrays are not set as properties
// store the property's value
$this -> properties [ $name ] =
$value ;
* Intercepts whenever someones unset a property's value
* @param $name Property Name
unset
( $this -> properties [ $name ] ) ;
* Returns a property's value
* @param $name Property Name
public function __get ( $name )
if ( isset
( $this -> properties [ $name ] ))
return $this -> properties [ $name ] ;
* Returns is a property's is set
* @param $name Property Name
return isset
( $this -> properties [ $name ] ) ;
// verify if the tag has child elements
// iterate all child elements
foreach ( $this -> children as $key =>
$child )
* @param $child Any object that implements the show() method
public function add ( $child )
$child -> setIsWrapped ( TRUE ) ;
* Insert an child element
* @param $position Element position
* @param $child Any object that implements the show() method
public function insert ( $position , $child )
$child -> setIsWrapped ( TRUE ) ;
* Set the use of linebreaks
* @param $linebreaks boolean
$this -> useLineBreaks =
$linebreaks ;
* Set the use of single quotes
* @param $singlequotes boolean
$this -> useSingleQuotes =
$singlequotes ;
* @param $child Any object that implements the show() method
public function del ( $object )
foreach ( $this -> children as $key =>
$child )
if ( $child ===
$object ) // same instance
* @param $element tag name
* @param $properties match properties
public function find ( $element , $properties =
null )
if ( $child -> getName ( ) ==
$element )
foreach ( $properties as $key =>
$value )
if ( $child -> getProperty ( $key ) !==
$value )
return array_merge ( [ $child ] , $child -> find ( $element , $properties )) ;
return $child -> find ( $element , $properties ) ;
* @param $position Element position
public function get ( $position )
// exibe a tag de abertura
echo
" <{$this -> tagname } " ;
// percorre as propriedades
foreach ( $this -> properties as $name =>
$value )
if ( $this -> useSingleQuotes )
$value = str_replace ( "'" , ''' , $value ) ;
echo " {
$name } ='{
$value } '
" ;
$value = str_replace ( '"' , '"' , $value ) ;
echo " {
$name } =\"{
$value } \"
" ;
if ( in_array ( $this -> tagname , self :: $voidelements ))
// verify if the tag has child elements
if ( $this -> useLineBreaks )
// iterate all child elements
if ( $child instanceof self )
$child -> setUseLineBreaks ( $this -> useLineBreaks ) ;
// verify if the child is an object
// otherwise, the child is a scalar
else if (( is_string ( $child )) or ( is_numeric ( $child )))
if ( ! in_array ( $this -> tagname , self :: $voidelements ))
if ( ! empty ( $this -> afterElement ))
$this -> afterElement -> show ( ) ;
public function closeTag ( )
echo " </ {
$this -> tagname } >
" ;
if ( $this -> useLineBreaks )
* Converts the object into a string
public function __toString ( )
* Returns the element content as a string
public function getContents ( )
$content =
ob_get_contents ( ) ;
public function clearChildren ( )