Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TElement.php

Documentation is available at TElement.php

  1. <?php
  2. namespace Adianti\Widget\Base;
  3.  
  4. /**
  5.  * Base class for all HTML Elements
  6.  *
  7.  * @version    7.4
  8.  * @package    widget
  9.  * @subpackage base
  10.  * @author     Pablo Dall'Oglio
  11.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  12.  * @license    http://www.adianti.com.br/framework-license
  13.  */
  14. class TElement
  15. {
  16.     private $tagname;     // tag name
  17.     private $properties;  // tag properties
  18.     private $wrapped;
  19.     private $useLineBreaks;
  20.     private $useSingleQuotes;
  21.     private $afterElement;
  22.     protected $children;
  23.     private static $voidelements;
  24.     private $hidden;
  25.     
  26.     /**
  27.      * Class Constructor
  28.      * @param $tagname  tag name
  29.      */
  30.     public function __construct($tagname)
  31.     {
  32.         // define the element name
  33.         $this->tagname $tagname;
  34.         $this->useLineBreaks TRUE;
  35.         $this->useSingleQuotes FALSE;
  36.         $this->wrapped FALSE;
  37.         $this->hidden FALSE;
  38.         $this->properties [];
  39.         
  40.         if (empty(self::$voidelements))
  41.         {
  42.             self::$voidelements array('area''base''br''col''command''embed''hr',
  43.                                         'img''input''keygen''link''meta''param''source''track''wbr');
  44.         }
  45.     }
  46.     
  47.     /**
  48.      * Create an element
  49.      * @param $tagname Element name
  50.      * @param $value Element value
  51.      * @param $attributes Element attributes
  52.      */
  53.     public static function tag($tagname$value$attributes NULL)
  54.     {
  55.         $object new TElement($tagname);
  56.         
  57.         if (is_array($value))
  58.         {
  59.             foreach ($value as $element)
  60.             {
  61.                 $object->add($element);
  62.             }
  63.         }
  64.         else
  65.         {
  66.             $object->add($value);
  67.         }
  68.         
  69.         if ($attributes)
  70.         {
  71.             foreach ($attributes as $att_name => $att_value)
  72.             {
  73.                 $object->$att_name $att_value;
  74.             }
  75.         }
  76.         
  77.         return $object;
  78.     }
  79.     
  80.     /**
  81.      * hide object
  82.      */
  83.     public function hide()
  84.     {
  85.         $this->hidden true;
  86.     }
  87.     
  88.     /**
  89.      * Insert element after
  90.      */
  91.     public function after($element)
  92.     {
  93.         $this->afterElement $element;
  94.     }
  95.     
  96.     /**
  97.      * Return the after element
  98.      */
  99.     public function getAfterElement()
  100.     {
  101.         return $this->afterElement;
  102.     }
  103.     
  104.     /**
  105.      * Change the element name
  106.      * @param $tagname Element name
  107.      */
  108.     public function setName($tagname)
  109.     {
  110.         $this->tagname $tagname;
  111.     }
  112.     
  113.     /**
  114.      * Returns tag name
  115.      */
  116.     public function getName()
  117.     {
  118.         return $this->tagname;
  119.     }
  120.     
  121.     /**
  122.      * Define if the element is wrapped inside another one
  123.      * @param @bool Boolean TRUE if is wrapped
  124.      */
  125.     protected function setIsWrapped($bool)
  126.     {
  127.         $this->wrapped $bool;
  128.     }
  129.     
  130.     /**
  131.      * Return if the element is wrapped inside another one
  132.      */
  133.     public function getIsWrapped()
  134.     {
  135.         return $this->wrapped;
  136.     }
  137.     
  138.     /**
  139.      * Set tag property
  140.      * @param $name     Property Name
  141.      * @param $value    Property Value
  142.      */
  143.     public function setProperty($name$value)
  144.     {
  145.         // objects and arrays are not set as properties
  146.         if (is_scalar($value))
  147.         {
  148.             // store the property's value
  149.             $this->properties[$name$value;
  150.         }
  151.     }
  152.     
  153.     /**
  154.      * Set element properties
  155.      */
  156.     public function setProperties($properties)
  157.     {
  158.         foreach ($properties as $property => $value)
  159.         {
  160.             if (is_null($value))
  161.             {
  162.                 unset($this->properties[$property]);
  163.             }
  164.             else
  165.             {
  166.                 $this->properties[$property$value;
  167.             }
  168.         }
  169.     }
  170.     
  171.     /**
  172.      * Return a property
  173.      * @param $name property name
  174.      */
  175.     public function getProperty($name)
  176.     {
  177.         return isset($this->properties[$name]$this->properties[$namenull;
  178.     }
  179.     
  180.     /**
  181.      * Return element properties
  182.      */
  183.     public function getProperties()
  184.     {
  185.         return $this->properties;
  186.     }
  187.     
  188.     /**
  189.      * Intercepts whenever someones assign a new property's value
  190.      * @param $name     Property Name
  191.      * @param $value    Property Value
  192.      */
  193.     public function __set($name$value)
  194.     {
  195.         // objects and arrays are not set as properties
  196.         if (is_scalar($value))
  197.         {
  198.             // store the property's value
  199.             $this->properties[$name$value;
  200.         }
  201.     }
  202.     
  203.     /**
  204.      * Intercepts whenever someones unset a property's value
  205.      * @param $name     Property Name
  206.      */
  207.     public function __unset($name)
  208.     {
  209.         unset($this->properties[$name]);
  210.     }
  211.     
  212.     /**
  213.      * Returns a property's value
  214.      * @param $name     Property Name
  215.      */
  216.     public function __get($name)
  217.     {
  218.         if (isset($this->properties[$name]))
  219.         {              
  220.             return $this->properties[$name];
  221.         }
  222.     }
  223.     
  224.     /**
  225.      * Returns is a property's is set
  226.      * @param $name     Property Name
  227.      */
  228.     public function __isset($name)
  229.     {
  230.         return isset($this->properties[$name]);
  231.     }
  232.     
  233.     /**
  234.      * Clone the object
  235.      */
  236.     public function __clone()
  237.     {
  238.         // verify if the tag has child elements
  239.         if ($this->children)
  240.         {
  241.             // iterate all child elements
  242.             foreach ($this->children as $key => $child)
  243.             {
  244.                 if (is_object($child))
  245.                 {
  246.                     $this->children[$keyclone $child;
  247.                 }
  248.                 else
  249.                 {
  250.                     $this->children[$key$child;
  251.                 }
  252.             }
  253.         }
  254.     }
  255.     
  256.     /**
  257.      * Add an child element
  258.      * @param $child Any object that implements the show() method
  259.      */
  260.     public function add($child)
  261.     {
  262.         $this->children[$child;
  263.         if ($child instanceof TElement)
  264.         {
  265.             $child->setIsWrappedTRUE );
  266.         }
  267.     }
  268.     
  269.     /**
  270.      * Insert an child element
  271.      * @param $position Element position
  272.      * @param $child Any object that implements the show() method
  273.      */
  274.     public function insert($position$child)
  275.     {
  276.         array_splice$this->children$position0array($child) );
  277.         if ($child instanceof TElement)
  278.         {
  279.             $child->setIsWrappedTRUE );
  280.         }
  281.     }
  282.     
  283.     /**
  284.      * Set the use of linebreaks
  285.      * @param $linebreaks boolean
  286.      */
  287.     public function setUseLineBreaks($linebreaks)
  288.     {
  289.         $this->useLineBreaks $linebreaks;
  290.     }
  291.     
  292.     /**
  293.      * Set the use of single quotes
  294.      * @param $singlequotes boolean
  295.      */
  296.     public function setUseSingleQuotes($singlequotes)
  297.     {
  298.         $this->useSingleQuotes $singlequotes;
  299.     }
  300.     
  301.     /**
  302.      * Del an child element
  303.      * @param $child Any object that implements the show() method
  304.      */
  305.     public function del($object)
  306.     {
  307.         foreach ($this->children as $key => $child)
  308.         {
  309.             if ($child === $object// same instance
  310.             {
  311.                 unset($this->children[$key]);
  312.             }
  313.         }
  314.     }
  315.  
  316.     /**
  317.      * get children
  318.      */
  319.     public function getChildren()
  320.     {
  321.         return $this->children;
  322.     }
  323.     
  324.     /**
  325.      * Find child element
  326.      * @param $element tag name
  327.      * @param $properties match properties
  328.      */
  329.     public function find($element$properties null)
  330.     {
  331.         if ($this->children)
  332.         {
  333.             foreach ($this->children as $child)
  334.             {
  335.                 if ($child instanceof TElement)
  336.                 {
  337.                     if ($child->getName(== $element)
  338.                     {
  339.                         $match true;
  340.                         if ($properties)
  341.                         {
  342.                             foreach ($properties as $key => $value)
  343.                             {
  344.                                 if ($child->getProperty($key!== $value)
  345.                                 {
  346.                                     $match false;
  347.                                 }
  348.                             }
  349.                         }
  350.                         
  351.                         if ($match)
  352.                         {
  353.                             return array_merge([$child]$child->find($element$properties));
  354.                         }
  355.                     }
  356.                     return $child->find($element$properties);
  357.                 }
  358.             }
  359.         }
  360.         return [];
  361.     }
  362.     
  363.     /**
  364.      * Get an child element
  365.      * @param $position Element position
  366.      */
  367.     public function get($position)
  368.     {
  369.         return $this->children[$position];
  370.     }
  371.     
  372.     /**
  373.      * Opens the tag
  374.      */
  375.     public function openTag()
  376.     {
  377.         // exibe a tag de abertura
  378.         echo "<{$this->tagname}";
  379.         if ($this->properties)
  380.         {
  381.             // percorre as propriedades
  382.             foreach ($this->properties as $name=>$value)
  383.             {
  384.                 if ($this->useSingleQuotes)
  385.                 {
  386.                     $value = str_replace("'", '&#039;', $value);
  387.                     echo " {$name}='{$value}'";
  388.                 }
  389.                 else
  390.                 {
  391.                     $value = str_replace('"', '&quot;', $value);
  392.                     echo " {$name}=\"{$value}\"";
  393.                 }
  394.             }
  395.         }
  396.         
  397.         if (in_array($this->tagnameself::$voidelements))
  398.         {
  399.             echo '/>';
  400.         }
  401.         else
  402.         {
  403.             echo '>';
  404.         }
  405.     }
  406.     
  407.     /**
  408.      * BC only
  409.      */
  410.     public function open()
  411.     {
  412.         $this->openTag();
  413.     }
  414.     
  415.     /**
  416.      * Shows the tag
  417.      */
  418.     public function show()
  419.     {
  420.         if ($this->hidden)
  421.         {
  422.             return;
  423.         }
  424.         
  425.         // open the tag
  426.         $this->openTag();
  427.         
  428.         // verify if the tag has child elements
  429.         if ($this->children)
  430.         {
  431.             if (count($this->children)>1)
  432.             {
  433.                 if ($this->useLineBreaks)
  434.                 {
  435.                     echo "\n";
  436.                 }
  437.             }
  438.             // iterate all child elements
  439.             foreach ($this->children as $child)
  440.             {
  441.                 if ($child instanceof self)
  442.                 {
  443.                     $child->setUseLineBreaks($this->useLineBreaks);
  444.                 }
  445.                 
  446.                 // verify if the child is an object
  447.                 if (is_object($child))
  448.                 {
  449.                     $child->show();
  450.                 }
  451.                 // otherwise, the child is a scalar
  452.                 else if ((is_string($child)) or (is_numeric($child)))
  453.                 {
  454.                     echo $child;
  455.                 }
  456.             }
  457.         }
  458.         
  459.         if (!in_array($this->tagnameself::$voidelements))
  460.         {
  461.             // closes the tag
  462.             $this->closeTag();
  463.         }
  464.         
  465.         if (!empty($this->afterElement))
  466.         {
  467.             $this->afterElement->show();
  468.         }
  469.     }
  470.     
  471.     /**
  472.      * Closes the tag
  473.      */
  474.     public function closeTag()
  475.     {
  476.         echo "</{$this->tagname}>";
  477.         if ($this->useLineBreaks)
  478.         {
  479.             echo "\n";
  480.         }
  481.     }
  482.     
  483.     /**
  484.      * BC only
  485.      */
  486.     public function close()
  487.     {
  488.         $this->closeTag();
  489.     }
  490.     
  491.     /**
  492.      * Converts the object into a string
  493.      */
  494.     public function __toString()
  495.     {
  496.         return $this->getContents();
  497.     }
  498.     
  499.     /**
  500.      * Returns the element content as a string
  501.      */
  502.     public function getContents()
  503.     {
  504.         ob_start();
  505.         $this->show();
  506.         $content ob_get_contents();
  507.         ob_end_clean();
  508.         return $content;
  509.     }
  510.     
  511.     /**
  512.      * Clear element children
  513.      */
  514.     public function clearChildren()
  515.     {
  516.         $this->children = array();
  517.     }