Advertisement
nux95

class Input

Dec 14th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.57 KB | None | 0 0
  1. <?php
  2.  
  3. function _($x) {
  4.     return htmlspecialchars($x);
  5. }
  6.  
  7. class Input {
  8.  
  9.     // The type of the input-field is stored in the attribute-slot.
  10.     private $typeName;
  11.  
  12.     // This is an array of elements that will be rendered when the input-field
  13.     // type requires sub-elements, such as the `select` input-field. The values
  14.     // inserted here depend on the input-field type. They will be ignored when
  15.     // the input-field type does not require sub-elements.
  16.     private $subElements;
  17.  
  18.     // Attributes of the input-field are saved in this array.
  19.     private $attributes;
  20.  
  21.     // This array contains all css-classes for the input-field and will be
  22.     // treated special from the $attributes slot. The class-value generated
  23.     // here will overwrite attributes set in the $attributes array.
  24.     private $classes;
  25.  
  26.     public function __construct($typeName, $id=null, $class=null) {
  27.         $this->typeName = $typeName;
  28.         $this->subElements = array();
  29.         $this->attributes = array();
  30.         $this->classes = array();
  31.  
  32.         if ($id !== null)
  33.             $this->setAttribute('id', $id);
  34.         if ($class !== null)
  35.             $this->setClasses(explode(' ', $class));
  36.     }
  37.  
  38.     public function setClasses($classes) {
  39.         if (!is_array($classes))
  40.             throw new InvalidArgumentException('expected array.');
  41.  
  42.         $this->classes = $classes;
  43.     }
  44.  
  45.     public function addClass(/* ... */) {
  46.         $args = func_get_args();
  47.         foreach ($args as $className) {
  48.             $contained = array_search($className, $this->classes);
  49.             if ($contained === false) {
  50.                 array_push($this->classes, $className);
  51.             }
  52.         }
  53.     }
  54.  
  55.     public function removeClass($className) {
  56.         $index = array_search($className, $this->classes);
  57.         if ($index !== false) {
  58.             unset($this->classes[$index]);
  59.         }
  60.     }
  61.  
  62.     public function setAttribute($name, $value) {
  63.         if ($name == 'class' or $name == 'type') {
  64.             throw new LogicException('class and type attributes are treated differently.');
  65.         }
  66.         $this->attributes[$name] = $value;
  67.     }
  68.  
  69.     // removeAttribute()
  70.     // hasAttribute()
  71.  
  72.     public function render() {
  73.         $string = '<input type="' . _($this->typeName) . '" ';
  74.  
  75.         // Render the class-names.
  76.         if ($this->classes) {
  77.             $classNames = implode(' ', $this->classes);
  78.             $string .= 'class="' . $classNames . '" ';
  79.         }
  80.  
  81.         // Render the rest of the attributes.
  82.         foreach ($this->attributes as $key => $value) {
  83.             $string .= $key . '="' . _($value) . '" ';
  84.         }
  85.         $string .= '>';
  86.  
  87.         // Special treatment for `select`, etc..
  88.  
  89.         $string .= '</input>';
  90.  
  91.         return $string;
  92.     }
  93.  
  94. }
  95.  
  96. $i = new Input('text', 'myInput', 'foo bar');
  97. $i->addClass('foo', 'baar', 'spam', 'eggs');
  98. print $i->render();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement