Passed
Push — master ( a74976...b3d616 )
by Paul
03:42
created

Builder::getCustomFieldClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
	public function __call( $method, $args )
55
	{
56
		$instance = new static;
57
		$instance->setTagFromMethod( $method );
58
		call_user_func_array( [$instance, 'normalize'], $args += ['',''] );
59
		$tags = array_merge( static::TAGS_FORM, static::TAGS_SINGLE, static::TAGS_STRUCTURE, static::TAGS_TEXT );
60
		$generatedTag = in_array( $instance->tag, $tags )
61
			? $instance->buildTag()
62
			: $instance->buildCustomField();
63
		if( !$this->render ) {
64
			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
	public function getClosingTag()
91
	{
92
		return '</'.$this->tag.'>';
93
	}
94
95
	/**
96
	 * @return string
97
	 */
98
	public function getOpeningTag()
99
	{
100
		$attributes = glsr( Attributes::class )->{$this->tag}( $this->args )->toString();
101
		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
	protected function buildDefaultTag( $text = '' )
120
	{
121
		if( empty( $text )) {
122
			$text = $this->args['text'];
123
		}
124
		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
			if( isset( $this->args['multiple'] )) {
146
				$this->args['name'].= '[]';
147
			}
148
			return $this->buildFormLabel().$this->getOpeningTag();
149
		}
150
		return empty( $this->args['options'] )
151
			? $this->buildFormInputChoice()
152
			: $this->buildFormInputMultiChoice();
153
	}
154
155
	/**
156
	 * @return string|void
157
	 */
158
	protected function buildFormInputChoice()
159
	{
160
		$labelText = !empty( $this->args['text'] )
161
			? $this->args['text']
162
			: $this->args['label'];
163
		return $this->label( $this->getOpeningTag().' '.$labelText, [
164
			'class' => 'glsr-'.$this->args['type'].'-label',
165
			'for' => $this->args['id'],
166
		]);
167
	}
168
169
	/**
170
	 * @return string|void
171
	 */
172
	protected function buildFormInputMultiChoice()
173
	{
174
		if( $this->args['type'] == 'checkbox' ) {
175
			$this->args['name'].= '[]';
176
		}
177
		$options = array_reduce( array_keys( $this->args['options'] ), function( $carry, $key ) {
178
			return $carry.$this->li( $this->{$this->args['type']}([
179
				'checked' => in_array( $key, (array)$this->args['value'] ),
180
				'name' => $this->args['name'],
181
				'text' => $this->args['options'][$key],
182
				'value' => $key,
183
			]));
184
		});
185
		return $this->ul( $options, [
186
			'class' => $this->args['class'],
187
			'id' => $this->args['id'],
188
		]);
189
	}
190
191
	/**
192
	 * @return void|string
193
	 */
194
	protected function buildFormLabel()
195
	{
196
		if( empty( $this->args['label'] ) || $this->args['type'] == 'hidden' )return;
197
		return $this->label([
198
			'for' => $this->args['id'],
199
			'text' => $this->args['label'],
200
		]);
201
	}
202
203
	/**
204
	 * @return string|void
205
	 */
206
	protected function buildFormSelect()
207
	{
208
		return $this->buildFormLabel().$this->buildDefaultTag( $this->buildFormSelectOptions() );
209
	}
210
211
	/**
212
	 * @return string|void
213
	 */
214
	protected function buildFormSelectOptions()
215
	{
216
		return array_reduce( array_keys( $this->args['options'] ), function( $carry, $key ) {
217
			return $carry.$this->option([
218
				'selected' => $this->args['value'] == $key,
219
				'text' => $this->args['options'][$key],
220
				'value' => $key,
221
			]);
222
		});
223
	}
224
225
	/**
226
	 * @return string|void
227
	 */
228
	protected function buildFormTextarea()
229
	{
230
		return $this->buildFormLabel().$this->buildDefaultTag( $this->args['value'] );
231
	}
232
233
	/**
234
	 * @return string|void
235
	 */
236
	protected function buildTag()
237
	{
238
		$this->mergeArgsWithRequiredDefaults();
239
		if( in_array( $this->tag, static::TAGS_SINGLE )) {
240
			return $this->getOpeningTag();
241
		}
242
		if( !in_array( $this->tag, static::TAGS_FORM )) {
243
			return $this->buildDefaultTag();
244
		}
245
		return call_user_func( [$this, 'buildForm'.ucfirst( $this->tag )] ).$this->buildFieldDescription();
246
	}
247
248
	/**
249
	 * @return string
250
	 */
251
	protected function getCustomFieldClassName()
252
	{
253
		return glsr( Helper::class )->buildClassName( $this->tag, __NAMESPACE__.'\Fields' );
254
	}
255
256
	/**
257
	 * @return void
258
	 */
259
	protected function mergeArgsWithRequiredDefaults()
260
	{
261
		$className = $this->getCustomFieldClassName();
262
		if( class_exists( $className )) {
263
			$this->args = array_merge(
264
				wp_parse_args( $this->args, $className::defaults() ),
265
				$className::required()
266
			);
267
		}
268
		$this->args = glsr( BuilderDefaults::class )->merge( $this->args );
269
	}
270
271
	/**
272
	 * @param string|array ...$params
273
	 * @return void
274
	 */
275
	protected function normalize( ...$params )
276
	{
277
		if( is_string( $params[0] ) || is_numeric( $params[0] )) {
278
			$this->setNameOrTextAttributeForTag( $params[0] );
279
		}
280
		if( is_array( $params[0] )) {
281
			$this->args += $params[0];
282
		}
283
		else if( is_array( $params[1] )) {
284
			$this->args += $params[1];
285
		}
286
	}
287
288
	/**
289
	 * @param string $value
290
	 * @return void
291
	 */
292
	protected function setNameOrTextAttributeForTag( $value )
293
	{
294
		$attribute = in_array( $this->tag, static::TAGS_FORM )
295
			? 'name'
296
			: 'text';
297
		$this->args[$attribute] = $value;
298
	}
299
300
	/**
301
	 * @param string $method
302
	 * @return void
303
	 */
304
	protected function setTagFromMethod( $method )
305
	{
306
		$this->tag = strtolower( $method );
307
		if( in_array( $this->tag, static::INPUT_TYPES )) {
308
			$this->args['type'] = $this->tag;
309
			$this->tag = 'input';
310
		}
311
	}
312
}
313