Created
June 22, 2012 18:16
-
-
Save rbarros/2974329 to your computer and use it in GitHub Desktop.
Classe para criação de elementos (tag/atributo/value)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Element Application | |
* | |
* An open source application development framework for PHP 5.1.6 or newer | |
* | |
* @package HTML | |
* @category Classes | |
* @author Ramon Barros | |
* @copyright Copyright (c) 2012, Ramon Barros. | |
* @license http://www.ramon-barros.com/ | |
* @link http://public.ramon-barros.com/ | |
* @since Version 1.6.12 | |
* @filesource Element.php | |
*/ | |
/** | |
* Interface Element | |
* Interface para definição das funções para criação das tags | |
* @author Ramon Barros | |
* @copyright Copyright (c) 2012, Ramon Barros. | |
*/ | |
interface Element { | |
public function element($type,$attributeArray = array()); | |
public function setAttribute($attribute,$value = ''); | |
public function getAttribute($attribute); | |
public function removeAttribute($attribute); | |
public function clearAttributes(); | |
public function elementChild($element); | |
public function createElement(); | |
public function getElement(); | |
} | |
/** | |
* Interface para criação dos elementos | |
*/ | |
abstract class AbstractElement implements Element { | |
/** | |
* @var string | |
*/ | |
protected $element; | |
/** | |
* @var string | |
*/ | |
protected $type; | |
/** | |
* @var array | |
*/ | |
protected $unaryTagArray = array('input', 'img', 'hr', 'br', 'meta', 'link'); | |
/** | |
* @var array | |
*/ | |
protected $attributeArray; | |
/** | |
* @var string | |
*/ | |
protected $innerHtml; | |
/** | |
* Define o tipo de tag e seus atrubutos | |
* @param string $type Tipo de tag | |
* @param array $attributeArray atributos da tag | |
* @return object | |
*/ | |
public function element($type, $attributeArray= array()) { | |
$this->type = strtolower($type); | |
foreach($attributeArray as $attribute => $value) { | |
$this->setAttribute($attribute, $value); | |
} | |
return $this; | |
} | |
/** | |
* Define os atributos e seus valores | |
* @param string $attribute atributo | |
* @param array $value valor do atributo | |
* @return object | |
*/ | |
public function setAttribute($attribute,$value = '') { | |
if(!is_array($attribute)) { | |
$this->attributeArray[$attribute] = $value; | |
} | |
else { | |
$this->attributeArray = array_merge($this->attributeArray, $attribute); | |
} | |
return $this; | |
} | |
/** | |
* Recupera um atributo utiliando o seu nome | |
* @param string $attribute atributo | |
* @return array | |
*/ | |
public function getAttribute($attribute) { | |
return $this->attributeArray[$attribute]; | |
} | |
/** | |
* Remove um atributo utiliando o seu nome | |
* @param string $attribute atributo | |
* @return object | |
*/ | |
public function removeAttribute($attribute) { | |
if(isset($this->attributeArray[$attribute])) { | |
unset($this->attributeArray[$attribute]); | |
} | |
return $this; | |
} | |
/** | |
* Limpa o array de atributos | |
* @return object | |
*/ | |
public function clearAttributes() { | |
$this->attributeArray = array(); | |
return $this; | |
} | |
/** | |
* Adiciona um filho a tag pai | |
* @param object $element Objeto filho que devera ser adicionado ao pai | |
* @return object | |
*/ | |
public function elementChild($element) { | |
if(@get_class($object) == __class__) { | |
$this->innerHtml .= $object->build(); | |
} | |
return $this; | |
} | |
/** | |
* Adiciona texto entre as tags de abertura e fechamento <tag>$object</tag> | |
* @param text $object | |
* @return object | |
*/ | |
public function updateElement($object) { | |
$this->innerHtml = $object; | |
return $this; | |
} | |
/** | |
* Cria os elementos especificados | |
* @return object | |
*/ | |
public function createElement() { | |
$this->element = "<".$this->type; | |
if(count($this->attributeArray)) { | |
foreach($this->attributeArray as $key => $value) { | |
$this->element .= " ".$key."=\"".$value."\""; | |
} | |
} | |
if(!in_array($this->type, $this->unaryTagArray)) { | |
$this->element.= ">\n".$this->innerHtml."\n</".$this->type.">\n"; | |
}else { | |
$this->element.= " />\n"; | |
} | |
return $this; | |
} | |
/** | |
* Recupera os elementos criados anteriormente, para serem mostrados na página | |
* @return string $this->element | |
*/ | |
public function getElement(){ | |
return $this->element; | |
} | |
} | |
/** | |
* Implementação da classe tag para criação dos elementos. | |
*/ | |
class tag extends AbstractElement { | |
/** | |
* @var string | |
*/ | |
protected $element; | |
/** | |
* Constroi o objeto que representa os elementos | |
* @param string $type | |
* @param array $attributeArray | |
*/ | |
public function __construct($type, $attributeArray) { | |
$this->element($type, $attributeArray); | |
} | |
/** | |
* Recupera os elementos criados anteriormente, para serem mostrados na página | |
* @return string $element | |
*/ | |
public function get() { | |
$this->createElement(); | |
return $this->getElement(); | |
} | |
/** | |
* Adiciona texto entre as tags de abertura e fechamento <tag>$object</tag> | |
* @param text $object | |
* @return object | |
*/ | |
public function update($object) { | |
$this->updateElement($object); | |
return $this; | |
} | |
} | |
// END Elements Class | |
/* End of file Elements.php */ | |
/* Location: ./path/subpackage/Elements.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment