Created
February 21, 2010 00:14
-
-
Save erenon/309999 to your computer and use it in GitHub Desktop.
HtmlElement
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 | |
/** | |
* Osztálygyűlytemény HTML generálásra | |
* | |
* Szigorúan PoC, produktív környezetben valószínűleg használhatatlan | |
* | |
* | |
* @category HtmlGenerator | |
* @package Generator | |
* @license New BSD License | |
* @version 0.1 | |
* @author erenon <erenon2 (Q) gmail (P) com> | |
* @dedicatedTo Mefi | |
* @todo dokumentáció befejezése | |
* @todo getterek és setterek hozzáadása | |
* @todo Elképesztő bloatwaret lehet belőle csinálni. Ötletek: további tagek, interfacek, iteratorok, egységtesztek (!) | |
*/ | |
/** | |
* Az alap osztály, amiből minden elem származik. | |
* DomElement lenne a tisztességes neve, csak ilyen már létezik :) | |
* | |
* @see php.net/DomElement | |
* @category HtmlGenerator | |
* @package Generator | |
*/ | |
class HtmlElement | |
{ | |
/** | |
* A html tag neve, pl.: div | |
*/ | |
protected $_tag; | |
/** | |
* Üres tag? | |
* pl.: <br/> | |
*/ | |
protected $_selfClose; | |
/** | |
* Attribútomok tömbje, pl.: id, class, name | |
*/ | |
protected $_attributes = array(); | |
/** | |
* Az elem által tartalmazott elemek. | |
*/ | |
protected $_childs = array(); | |
/** | |
* Új elem létrehozása | |
* | |
* @param string $tag Az létrehozandó tag, pl.: div | |
* @param bool $selfClose Üres tag? | |
*/ | |
public function __construct($tag, $selfClose = false) | |
{ | |
$this->_tag = $tag; | |
$this->_selfClose = $selfClose; | |
} | |
/** | |
* Új attribútum hozzáadása | |
* | |
* @param HtmlAttribute $attribute a hozzáadandó attribútum | |
* @return HtmlElement $this | |
*/ | |
public function addAttribute(HtmlAttribute $attribute) | |
{ | |
$this->_attributes[] = $attribute; | |
return $this; | |
} | |
/** | |
* Meglévő attribútum törlése | |
* | |
* @todo Implement removeAttribute | |
*/ | |
public function removeAttribute(HtmlAttribute $attribute) | |
{ | |
throw new Exception("Not implemented"); | |
} | |
/** | |
* Új leszármazott elem hozzáadása. | |
* | |
* @param HtmlElement $child A hozzáadandó leszármazott | |
* @return HtmlElement $child | |
*/ | |
public function addChild(HtmlElement $child) | |
{ | |
$this->_childs[] = $child; | |
return $this; | |
} | |
/** | |
* Meglévő leszármazott törlése | |
* | |
* @todo Implement removeChild | |
*/ | |
public function removeChild(HtmlElement $child) | |
{ | |
throw new Exception("Not implemented"); | |
} | |
/** | |
* Az elem megjelenítése attribútumaival és gyermekeivel. | |
* | |
* @return string Az elem html formája | |
*/ | |
public function render() | |
{ | |
$output = $this->_renderTag(); | |
if($this->_selfClose === false) { | |
$output .= $this->_renderChilds(); | |
$output .= $this->_renderTagClose(); | |
} | |
return $output; | |
} | |
/** | |
* Rövidítés | |
* | |
* @example echo $html; | |
* @return string Az elem html formája | |
*/ | |
public function __toString() | |
{ | |
return $this->render(); | |
} | |
/** | |
* Tag html formájának előállítása | |
* | |
* @return string A tag, pl.: <div id="container"> | |
*/ | |
protected function _renderTag() | |
{ | |
$output = '<' . $this->_tag; | |
foreach ($this->_attributes as $attribute) | |
{ | |
$output .= $attribute->render(); | |
} | |
if ($this->_selfClose) { | |
$output .= '/>'; | |
} else { | |
$output .= '>'; | |
} | |
$output .= "\n"; | |
return $output; | |
} | |
/** | |
* A gyermekek html formájának előállítása | |
*/ | |
protected function _renderChilds() | |
{ | |
$output = ""; | |
foreach ($this->_childs as $child) | |
{ | |
$output .= $child->render() . "\n"; | |
} | |
return $output; | |
} | |
/** | |
* Lezáró tag html formájának előállítása | |
* | |
* @return string Lezáró tag | |
*/ | |
protected function _renderTagClose() | |
{ | |
return '</' . $this->_tag . '>'; | |
} | |
} | |
/** | |
* A tagek attribútumaival foglalkozó osztály | |
* | |
* @category HtmlGenerator | |
* @package Generator | |
*/ | |
class HtmlAttribute | |
{ | |
protected $_name; | |
protected $_value; | |
/** | |
* Attribútum létrehozása | |
* | |
* @param string $name Az attribútum neve | |
* @param string $value Az attribútum értéke | |
*/ | |
public function __construct($name, $value) | |
{ | |
$this->_name = $name; | |
$this->setValue($value); | |
} | |
public function setValue($value) | |
{ | |
if (is_array($value)) | |
{ | |
$this->_value = join(' ', $value); | |
} else if (is_string($value)) { | |
$this->_value = $value; | |
} else { | |
throw new Exception('Invalid value given'); | |
} | |
return $this; | |
} | |
public function getValue() | |
{ | |
return $this->_value; | |
} | |
public function render() | |
{ | |
$output = " "; | |
$output .= $this->_name . '="' . $this->getValue() . '"'; | |
return $output; | |
} | |
} | |
/** | |
* A HTML tagek által közrezárt szöveget kezelő osztály | |
* | |
* Az öröklődés nem logikus, okokat lásd a HtmlElement fejlécében. | |
* | |
* @category HtmlGenerator | |
* @package Generator | |
*/ | |
class Text extends HtmlElement | |
{ | |
private $_value; | |
/** | |
* @param string $value A megjeleníteni kívánt szöveg | |
*/ | |
public function __construct($value) | |
{ | |
$this->_value = $value; | |
} | |
public function render() | |
{ | |
return $this->_value; | |
} | |
} | |
/** | |
* Legördülő listát kezelő osztály | |
* | |
* Lényegében a PoC lényege. Nagyon könnyen előállítható. | |
* | |
* @category HtmlGenerator | |
* @package Generator | |
* @todo add optgroup support | |
*/ | |
class SelectElement extends HtmlElement | |
{ | |
//private $_options; | |
/** | |
* @param array $options A megjeleníteni kívánt választási lehetőségek | |
*/ | |
public function __construct(array $options) | |
{ | |
$this->_tag = 'select'; | |
$this->_selfClose = false; | |
if (!empty($options)) { | |
foreach ($options as $option) { | |
$optionElement = new HtmlElement('option'); | |
$optionText = new Text($option); | |
$optionElement->addChild($optionText); | |
$this->addChild($optionElement); | |
} | |
} else { | |
throw new Exception('Empty array given'); | |
} | |
} | |
} | |
/** | |
* Demo | |
* | |
* Az osztályok használatának bemutatása | |
* | |
* | |
* @category HtmlGenerator | |
* @package Demo | |
* @license New BSD License | |
*/ | |
//Composition tesztelése | |
$html = new HtmlElement('html'); | |
$head = new HtmlElement('head'); | |
$style = new HtmlElement('style'); | |
$body = new HtmlElement('body'); | |
$div = new HtmlElement('div'); | |
$html->addChild($head); | |
$head->addChild($style); | |
$html->addChild($body); | |
$body->addChild($div); | |
//text tesztelése | |
$styleText = new Text('.bigfonted {font-size: 2em;} .bordered {border: 1px dotted #f00;}'); | |
$style->addChild($styleText); | |
//attribute tesztelése | |
$divStyle = new HtmlAttribute('class', array('bordered', 'bigfonted')); | |
$div->addAttribute($divStyle); | |
$divText = new Text('Nagybetűs szöveg'); | |
$div->addChild($divText); | |
//selfclose tesztelése | |
$br = new HtmlElement('br', true); | |
$div->addChild($br); | |
$div->addChild($br); | |
//select demo | |
$select = new SelectElement(array('Egy', 'Kettő', 'Három')); | |
$div->addChild($select); | |
echo $html; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment