1<?php 2namespace dokuwiki\Form; 3 4 5/** 6 * Class Element 7 * 8 * The basic building block of a form 9 * 10 * @package dokuwiki\Form 11 */ 12abstract class Element { 13 14 /** 15 * @var array the attributes of this element 16 */ 17 protected $attributes = array(); 18 19 /** 20 * @var string The type of this element 21 */ 22 protected $type; 23 24 /** 25 * @param string $type The type of this element 26 * @param array $attributes 27 */ 28 public function __construct($type, $attributes = array()) { 29 $this->type = $type; 30 $this->attributes = $attributes; 31 } 32 33 /** 34 * Gets or sets an attribute 35 * 36 * When no $value is given, the current content of the attribute is returned. 37 * An empty string is returned for unset attributes. 38 * 39 * When a $value is given, the content is set to that value and the Element 40 * itself is returned for easy chaining 41 * 42 * @param string $name Name of the attribute to access 43 * @param null|string $value New value to set 44 * @return string|$this 45 */ 46 public function attr($name, $value = null) { 47 // set 48 if($value !== null) { 49 $this->attributes[$name] = $value; 50 return $this; 51 } 52 53 // get 54 if(isset($this->attributes[$name])) { 55 return $this->attributes[$name]; 56 } else { 57 return ''; 58 } 59 } 60 61 /** 62 * Removes the given attribute if it exists 63 * 64 * @param $name 65 * @return $this 66 */ 67 public function rmattr($name) { 68 if(isset($this->attributes[$name])) { 69 unset($this->attributes[$name]); 70 } 71 return $this; 72 } 73 74 /** 75 * Gets or adds a all given attributes at once 76 * 77 * @param array|null $attributes 78 * @return array|$this 79 */ 80 public function attrs($attributes = null) { 81 // set 82 if($attributes) { 83 foreach((array) $attributes as $key => $val) { 84 $this->attr($key, $val); 85 } 86 return $this; 87 } 88 // get 89 return $this->attributes; 90 } 91 92 /** 93 * Adds a class to the class attribute 94 * 95 * This is the preferred method of setting the element's class 96 * 97 * @param string $class the new class to add 98 * @return $this 99 */ 100 public function addClass($class) { 101 $classes = explode(' ', $this->attr('class')); 102 $classes[] = $class; 103 $classes = array_unique($classes); 104 $this->attr('class', join(' ', $classes)); 105 return $this; 106 } 107 108 /** 109 * Get or set the element's ID 110 * 111 * This is the preferred way of setting the element's ID 112 * 113 * @param null|string $id 114 * @return string|$this 115 */ 116 public function id($id = null) { 117 if(strpos($id, '__') === false) { 118 throw new \InvalidArgumentException('IDs in DokuWiki have to contain two subsequent underscores'); 119 } 120 121 return $this->attr('id', $id); 122 } 123 124 /** 125 * Get or set the element's value 126 * 127 * This is the preferred way of setting the element's value 128 * 129 * @param null|string $value 130 * @return string|$this 131 */ 132 public function val($value = null) { 133 return $this->attr('value', $value); 134 } 135 136 /** 137 * The HTML representation of this element 138 * 139 * @return string 140 */ 141 abstract public function toHTML(); 142} 143