Advertisement
j0k3r

HtmlHelper

Sep 8th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2.  
  3. final class HtmlBuilder {
  4.  
  5.     private $_elements = array();
  6.  
  7.     public function addElement(HtmlElement $element) {
  8.         if ($element == null)
  9.             return;
  10.         $this->_elements[] = $element;
  11.     }
  12.  
  13.     public function render() {
  14.         foreach ($this->_elements as $element) {
  15.             echo $element->render();
  16.         }
  17.     }
  18.  
  19. }
  20.  
  21. abstract class HtmlElement {
  22.  
  23.     protected $commonAttributes = array("id", "class", "title");
  24.     protected $customAttributes = array();
  25.     protected $attributes;
  26.     protected $output;
  27.  
  28.     protected function __construct() {
  29.         $this->attributes = new AttributeCollection();
  30.         $this->output = "";
  31.     }
  32.  
  33.     public function setAttribute(Attribute $attribute) {
  34.         if ($this->isValidAttribute($attribute->name)) {
  35.             $this->attributes->addAttribute($attribute);
  36.         }
  37.     }
  38.  
  39.     protected function isValidAttribute($name) {
  40.         if (in_array($name, $this->commonAttributes) || in_array($name, $this->customAttributes))
  41.             return true;
  42.         return false;
  43.     }
  44.  
  45.     public abstract function render();
  46. }
  47.  
  48. final class Attribute {
  49.  
  50.     public $name;
  51.     public $value;
  52.  
  53.     public function __construct($name, $value) {
  54.         $this->name = $name;
  55.         $this->value = $value;
  56.     }
  57.  
  58. }
  59.  
  60. final class AttributeCollection {
  61.  
  62.     private $_attributes = array();
  63.  
  64.     public function addAttribute(Attribute $attribute) {
  65.         if ($attribute == null)
  66.             return;
  67.         $this->_attributes[] = $attribute;
  68.     }
  69.  
  70.     public function attributeExists($name) {
  71.         foreach ($this->_attributes as $attribute) {
  72.             if ($attribute->name == $name)
  73.                 return true;
  74.         }
  75.         return false;
  76.     }
  77.  
  78.     public function getValue($name) {
  79.         if ($this->attributeExists($name)) {
  80.             foreach ($this->_attributes as $attribute) {
  81.                 if ($attribute->name == $name) {
  82.                     return $attribute->value;
  83.                 }
  84.             }
  85.         }
  86.     }
  87.  
  88.     public function getAttributes() {
  89.         return $this->_attributes;
  90.     }
  91.  
  92. }
  93.  
  94. final class Image extends HtmlElement {
  95.  
  96.     public function __construct() {
  97.         parent::__construct();
  98.         $this->customAttributes = array("src", "alt", "height", "width");
  99.     }
  100.  
  101.     public function render() {
  102.         $this->output .= "<img";
  103.         foreach ($this->attributes->getAttributes() as $attribute) {
  104.             $this->output .= ' ' . $attribute->name . '="' . $attribute->value . '"';
  105.         }
  106.         $this->output .= ' />';
  107.         return $this->output;
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement