Passed
Push — master ( 60c13a...992102 )
by Paul
04:54
created

Builder::getCustomFieldClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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