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