PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.1.4
Admin Columns v7.1.4
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Form / Element.php
codepress-admin-columns / classes / Form Last commit date
Element 2 days ago Element.php 2 days ago Nonce.php 2 days ago NonceFactory.php 2 days ago PreviewNonce.php 2 days ago
Element.php
213 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace AC\Form;
6
7 use AC\Helper;
8 use AC\Renderable;
9
10 abstract class Element implements Renderable
11 {
12 protected array $attributes = [];
13
14 /**
15 * Options for element like select
16 */
17 protected array $options = [];
18
19 /**
20 * The elements value
21 * @var mixed
22 */
23 protected $value = null;
24
25 protected string $label = '';
26
27 protected string $description = '';
28
29 /**
30 * Setup element with base name and id
31 */
32 public function __construct(string $name, array $options = [])
33 {
34 $this->set_name($name);
35 $this->set_id($name);
36 $this->set_options($options);
37 }
38
39 protected function render_description(): ?string
40 {
41 if (! $this->get_description()) {
42 return null;
43 }
44
45 $template = '<p class="help-msg">%s</p>';
46
47 return sprintf($template, $this->get_description());
48 }
49
50 abstract public function render(): string;
51
52 public function get_attribute(string $key): ?string
53 {
54 if (! isset($this->attributes[$key])) {
55 return null;
56 }
57
58 return trim((string)$this->attributes[$key]);
59 }
60
61 public function set_attribute(string $key, string $value): self
62 {
63 if ('value' === $key) {
64 $this->set_value($value);
65
66 return $this;
67 }
68
69 $this->attributes[$key] = $value;
70
71 return $this;
72 }
73
74 public function get_attributes(): array
75 {
76 return $this->attributes;
77 }
78
79 public function set_attributes(array $attributes): self
80 {
81 foreach ($attributes as $key => $value) {
82 $this->set_attribute((string)$key, (string)$value);
83 }
84
85 return $this;
86 }
87
88 /**
89 * Get attributes as string
90 */
91 protected function get_attributes_as_string(array $attributes): string
92 {
93 $output = [];
94
95 foreach ($attributes as $key => $value) {
96 $output[] = $this->get_attribute_as_string((string)$key, (string)$value);
97 }
98
99 return implode(' ', $output);
100 }
101
102 /**
103 * Render an attribute
104 */
105 protected function get_attribute_as_string(string $key, ?string $value = null): string
106 {
107 if (null === $value) {
108 $value = $this->get_attribute($key);
109 }
110
111 return Helper\Html::create()->get_attribute_as_string($key, $value);
112 }
113
114 public function get_name(): ?string
115 {
116 return $this->get_attribute('name');
117 }
118
119 public function set_name(string $name): self
120 {
121 return $this->set_attribute('name', $name);
122 }
123
124 public function get_id(): ?string
125 {
126 return $this->get_attribute('id');
127 }
128
129 public function set_id(string $id): self
130 {
131 return $this->set_attribute('id', $id);
132 }
133
134 /**
135 * @return mixed
136 */
137 public function get_value()
138 {
139 return $this->value;
140 }
141
142 /**
143 * @param mixed $value
144 *
145 * @return $this
146 */
147 public function set_value($value): self
148 {
149 $this->value = $value;
150
151 return $this;
152 }
153
154 public function set_class(string $class): self
155 {
156 $this->set_attribute('class', $class);
157
158 return $this;
159 }
160
161 public function add_class(string $class): self
162 {
163 $parts = explode(' ', (string)$this->get_attribute('class'));
164 $parts[] = $class;
165
166 $this->set_class(implode(' ', $parts));
167
168 return $this;
169 }
170
171 public function get_label(): string
172 {
173 return $this->label;
174 }
175
176 public function set_label(string $label): self
177 {
178 $this->label = $label;
179
180 return $this;
181 }
182
183 public function set_options(array $options): self
184 {
185 $this->options = $options;
186
187 return $this;
188 }
189
190 public function get_options(): array
191 {
192 return $this->options;
193 }
194
195 public function get_description(): string
196 {
197 return $this->description;
198 }
199
200 public function set_description(string $description): self
201 {
202 $this->description = $description;
203
204 return $this;
205 }
206
207 public function __toString(): string
208 {
209 return $this->render();
210 }
211
212 }
213