xref: /dokuwiki/inc/Form/Element.php (revision 6d0ceaf93ca31dfb83fd4325ef2eecd9cef733c0)
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        $classes = array_filter($classes);
105        $this->attr('class', join(' ', $classes));
106        return $this;
107    }
108
109    /**
110     * Get or set the element's ID
111     *
112     * This is the preferred way of setting the element's ID
113     *
114     * @param null|string $id
115     * @return string|$this
116     */
117    public function id($id = null) {
118        if(strpos($id, '__') === false) {
119            throw new \InvalidArgumentException('IDs in DokuWiki have to contain two subsequent underscores');
120        }
121
122        return $this->attr('id', $id);
123    }
124
125    /**
126     * Get or set the element's value
127     *
128     * This is the preferred way of setting the element's value
129     *
130     * @param null|string $value
131     * @return string|$this
132     */
133    public function val($value = null) {
134        return $this->attr('value', $value);
135    }
136
137    /**
138     * The HTML representation of this element
139     *
140     * @return string
141     */
142    abstract public function toHTML();
143}
144