Passed
Push — master ( 59c1b9...1f2705 )
by Paul
04:56
created

Builder::buildFormLabel()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 0
crap 12
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use GeminiLabs\SiteReviews\Defaults\BuilderDefaults;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Modules\Html\Attributes;
8
9
class Builder
10
{
11
	const INPUT_TYPES = [
12
		'checkbox', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month',
13
		'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time',
14
		'url', 'week',
15
	];
16
17
	const TAGS_FORM = [
18
		'input', 'select', 'textarea',
19
	];
20
21
	const TAGS_SINGLE = [
22
		'img',
23
	];
24
25
	const TAGS_STRUCTURE = [
26
		'div', 'form', 'nav', 'ol', 'section', 'ul',
27
	];
28
29
	const TAGS_TEXT = [
30
		'a', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'label', 'li', 'option', 'p', 'pre', 'small',
31
		'span',
32
	];
33
34
	/**
35
	 * @var array
36
	 */
37
	public $args = [];
38
39
	/**
40
	 * @var bool
41
	 */
42
	public $render = false;
43
44
	/**
45
	 * @var string
46
	 */
47
	public $tag;
48
49
	/**
50
	 * @param string $method
51
	 * @param array $args
52
	 * @return string|void
53
	 */
54 7
	public function __call( $method, $args )
55
	{
56 7
		$instance = new static;
57 7
		$instance->setTagFromMethod( $method );
58 7
		call_user_func_array( [$instance, 'normalize'], $args += ['',''] );
59 7
		$tags = array_merge( static::TAGS_FORM, static::TAGS_SINGLE, static::TAGS_STRUCTURE, static::TAGS_TEXT );
60 7
		$generatedTag = in_array( $instance->tag, $tags )
61 7
			? $instance->buildTag()
62 7
			: $instance->buildCustomField();
63 7
		if( !$this->render ) {
64 7
			return $generatedTag;
65
		}
66
		echo $generatedTag;
67
	}
68
69
	/**
70
	 * @param string $property
71
	 * @param mixed $value
72
	 * @return void
73
	 */
74
	public function __set( $property, $value )
75
	{
76
		$properties = [
77
			'args' => 'is_array',
78
			'render' => 'is_bool',
79
			'tag' => 'is_string',
80
		];
81
		if( !isset( $properties[$property] )
82
			|| empty( array_filter( [$value], $properties[$property] ))
83
		)return;
84
		$this->$property = $value;
85
	}
86
87
	/**
88
	 * @return string
89
	 */
90 7
	public function getClosingTag()
91
	{
92 7
		return '</'.$this->tag.'>';
93
	}
94
95
	/**
96
	 * @return string
97
	 */
98 7
	public function getOpeningTag()
99
	{
100 7
		$attributes = glsr( Attributes::class )->{$this->tag}( $this->args )->toString();
101 7
		return '<'.trim( $this->tag.' '.$attributes ).'>';
102
	}
103
104
	/**
105
	 * @return string|void
106
	 */
107
	protected function buildCustomField()
108
	{
109
		$className = $this->getCustomFieldClassName();
110
		if( class_exists( $className )) {
111
			return (new $className( $this ))->build();
112
		}
113
		glsr_log()->error( 'Field missing: '.$className );
114
	}
115
116
	/**
117
	 * @return string|void
118
	 */
119 7
	protected function buildDefaultTag( $text = '' )
120
	{
121 7
		if( empty( $text )) {
122 7
			$text = $this->args['text'];
123
		}
124 7
		return $this->getOpeningTag().$text.$this->getClosingTag();
125
	}
126
127
	/**
128
	 * @return string|void
129
	 */
130
	protected function buildFieldDescription()
131
	{
132
		if( empty( $this->args['description'] ))return;
133
		if( $this->args['is_widget'] ) {
134
			return $this->small( $this->args['description'] );
135
		}
136
		return $this->p( $this->args['description'], ['class' => 'description'] );
137
	}
138
139
	/**
140
	 * @return string|void
141
	 */
142
	protected function buildFormInput()
143
	{
144
		if( !in_array( $this->args['type'], ['checkbox', 'radio'] )) {
145
			return $this->buildFormLabel().$this->getOpeningTag();
146
		}
147
		return empty( $this->args['options'] )
148
			? $this->buildFormInputChoice()
149
			: $this->buildFormInputMultiChoice();
150
	}
151
152
	/**
153
	 * @return string|void
154
	 */
155
	protected function buildFormInputChoice()
156
	{
157
		return $this->label( $this->getOpeningTag().' '.$this->args['text'] );
158
	}
159
160
	/**
161
	 * @return string|void
162
	 */
163
	protected function buildFormInputMultiChoice()
164
	{
165
		if( $this->args['type'] == 'checkbox' ) {
166
			$this->args['name'].= '[]';
167
		}
168
		$options = array_reduce( array_keys( $this->args['options'] ), function( $carry, $key ) {
169
			return $carry.$this->li( $this->{$this->args['type']}([
170
				'checked' => in_array( $key, (array)$this->args['value'] ),
171
				'name' => $this->args['name'],
172
				'text' => $this->args['options'][$key],
173
				'value' => $key,
174
			]));
175
		});
176
		return $this->ul( $options, [
177
			'class' => $this->args['class'],
178
			'id' => $this->args['id'],
179
		]);
180
	}
181
182
	/**
183
	 * @return void|string
184
	 */
185
	protected function buildFormLabel()
186
	{
187
		if( empty( $this->args['label'] ) || $this->args['type'] == 'hidden' )return;
188
		return $this->label([
189
			'for' => $this->args['id'],
190
			'text' => $this->args['label'],
191
		]);
192
	}
193
194
	/**
195
	 * @return string|void
196
	 */
197
	protected function buildFormSelect()
198
	{
199
		return $this->buildFormLabel().$this->buildDefaultTag( $this->buildFormSelectOptions() );
200
	}
201
202
	/**
203
	 * @return string|void
204
	 */
205
	protected function buildFormSelectOptions()
206
	{
207
		return array_reduce( array_keys( $this->args['options'] ), function( $carry, $key ) {
208
			return $carry.$this->option([
209
				'selected' => $this->args['value'] == $key,
210
				'text' => $this->args['options'][$key],
211
				'value' => $key,
212
			]);
213
		});
214
	}
215
216
	/**
217
	 * @return string|void
218
	 */
219
	protected function buildFormTextarea()
220
	{
221
		return $this->buildFormLabel().$this->buildDefaultTag( $this->args['value'] );
222
	}
223
224
	/**
225
	 * @return string|void
226
	 */
227 7
	protected function buildTag()
228
	{
229 7
		$this->mergeArgsWithRequiredDefaults();
230 7
		if( in_array( $this->tag, static::TAGS_SINGLE )) {
231
			return $this->getOpeningTag();
232
		}
233 7
		if( !in_array( $this->tag, static::TAGS_FORM )) {
234 7
			return $this->buildDefaultTag();
235
		}
236
		return call_user_func( [$this, 'buildForm'.ucfirst( $this->tag )] ).$this->buildFieldDescription();
237
	}
238
239
	/**
240
	 * @return string
241
	 */
242 7
	protected function getCustomFieldClassName()
243
	{
244 7
		return glsr( Helper::class )->buildClassName( $this->tag, __NAMESPACE__.'\Fields' );
245
	}
246
247
	/**
248
	 * @return void
249
	 */
250 7
	protected function mergeArgsWithRequiredDefaults()
251
	{
252 7
		$args = glsr( BuilderDefaults::class )->merge( $this->args );
253 7
		$className = $this->getCustomFieldClassName();
254 7
		if( class_exists( $className )) {
255
			$args = array_merge(
256
				wp_parse_args( $args, $className::defaults() ),
257
				$className::required()
258
			);
259
		}
260 7
		$this->args = $args;
261 7
	}
262
263
	/**
264
	 * @param string|array ...$params
265
	 * @return void
266
	 */
267 7
	protected function normalize( ...$params )
268
	{
269 7
		if( is_string( $params[0] ) || is_numeric( $params[0] )) {
270 7
			$this->setNameOrTextAttributeForTag( $params[0] );
271
		}
272 7
		if( is_array( $params[0] )) {
273
			$this->args += $params[0];
274
		}
275 7
		else if( is_array( $params[1] )) {
276 7
			$this->args += $params[1];
277
		}
278 7
	}
279
280
	/**
281
	 * @param string $value
282
	 * @return void
283
	 */
284 7
	protected function setNameOrTextAttributeForTag( $value )
285
	{
286 7
		$attribute = in_array( $this->tag, static::TAGS_FORM )
287
			? 'name'
288 7
			: 'text';
289 7
		$this->args[$attribute] = $value;
290 7
	}
291
292
	/**
293
	 * @param string $method
294
	 * @return void
295
	 */
296 7
	protected function setTagFromMethod( $method )
297
	{
298 7
		$this->tag = strtolower( $method );
299 7
		if( in_array( $this->tag, static::INPUT_TYPES )) {
300
			$this->args['type'] = $this->tag;
301
			$this->tag = 'input';
302
		}
303 7
	}
304
}
305