Passed
Push — master ( 525778...0f3274 )
by Paul
04:54
created

Builder::buildFormTextarea()   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
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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 = glsr( Helper::class )->buildClassName( $this->tag, __NAMESPACE__.'\Fields' );
121
		if( !class_exists( $className )) {
122
			glsr_log()->error( 'Field missing: '.$className );
123
			return;
124
		}
125
		return (new $className( $this ))->build();
126
	}
127
128
	/**
129
	 * @return string|void
130
	 */
131 7
	protected function buildDefaultTag( $text = '' )
132
	{
133 7
		if( empty( $text )) {
134 7
			$text = $this->args['text'];
135
		}
136 7
		return $this->getOpeningTag().$text.$this->getClosingTag();
137
	}
138
139
	/**
140
	 * @return string|void
141
	 */
142
	protected function buildFieldDescription()
143
	{
144
		if( empty( $this->args['description'] ))return;
145
		if( !empty( $this->globals['is_widget'] )) {
146
			return $this->small( $this->args['description'] );
147
		}
148
		return $this->p( $this->args['description'], ['class' => 'description'] );
149
	}
150
151
	/**
152
	 * @return string|void
153
	 */
154
	protected function buildFormInput()
155
	{
156
		if( !in_array( $this->args['type'], ['checkbox', 'radio'] )) {
157
			return $this->buildFormLabel().$this->getOpeningTag();
158
		}
159
		return empty( $this->args['options'] )
160
			? $this->buildFormInputChoice()
161
			: $this->buildFormInputMultiChoice();
162
	}
163
164
	/**
165
	 * @return string|void
166
	 */
167
	protected function buildFormInputChoice()
168
	{
169
		return $this->label( $this->getOpeningTag().' '.$this->args['text'] );
170
	}
171
172
	/**
173
	 * @return string|void
174
	 */
175
	protected function buildFormInputMultiChoice()
176
	{
177
		if( $this->args['type'] == 'checkbox' ) {
178
			$this->args['name'].= '[]';
179
		}
180
		$options = array_reduce( array_keys( $this->args['options'] ), function( $carry, $key ) {
181
			return $carry.$this->li( $this->{$this->args['type']}([
182
				'checked' => in_array( $key, (array)$this->args['value'] ),
183
				'name' => $this->args['name'],
184
				'text' => $this->args['options'][$key],
185
				'value' => $key,
186
			]));
187
		});
188
		return $this->ul( $options, [
189
			'class' => $this->args['class'],
190
			'id' => $this->args['id'],
191
		]);
192
	}
193
194
	/**
195
	 * @return void|string
196
	 */
197
	protected function buildFormLabel()
198
	{
199
		if( empty( $this->args['label'] ) || $this->args['type'] == 'hidden' )return;
200
		return $this->label([
201
			'for' => $this->args['id'],
202
			'text' => $this->args['label'],
203
		]);
204
	}
205
206
	/**
207
	 * @return string|void
208
	 */
209
	protected function buildFormSelect()
210
	{
211
		return $this->buildFormLabel().$this->buildDefaultTag( $this->buildFormSelectOptions() );
212
	}
213
214
	/**
215
	 * @return string|void
216
	 */
217
	protected function buildFormSelectOptions()
218
	{
219
		return array_reduce( array_keys( $this->args['options'] ), function( $carry, $key ) {
220
			return $carry.$this->option([
221
				'selected' => $this->args['value'] == $key,
222
				'text' => $this->args['options'][$key],
223
				'value' => $key,
224
			]);
225
		});
226
	}
227
228
	/**
229
	 * @return string|void
230
	 */
231
	protected function buildFormTextarea()
232
	{
233
		return $this->buildFormLabel().$this->buildDefaultTag( $this->args['value'] );
234
	}
235
236
	/**
237
	 * @return string|void
238
	 */
239 7
	protected function buildTag()
240
	{
241 7
		if( in_array( $this->tag, static::TAGS_SINGLE )) {
242
			return $this->getOpeningTag();
243
		}
244 7
		if( !in_array( $this->tag, static::TAGS_FORM )) {
245 7
			return $this->buildDefaultTag();
246
		}
247
		return call_user_func( [$this, 'buildForm'.ucfirst( $this->tag )] ).$this->buildFieldDescription();
248
	}
249
250
	/**
251
	 * @param string|array ...$params
252
	 * @return void
253
	 */
254 7
	protected function normalize( ...$params )
255
	{
256 7
		if( is_string( $params[0] ) || is_numeric( $params[0] )) {
257 7
			$this->setNameOrTextAttributeForTag( $params[0] );
258
		}
259 7
		if( is_array( $params[0] )) {
260
			$this->args += $params[0];
261
		}
262 7
		else if( is_array( $params[1] )) {
263 7
			$this->args += $params[1];
264
		}
265 7
		$this->args = glsr( BuilderDefaults::class )->merge( $this->args );
266 7
	}
267
268
	/**
269
	 * @param string $value
270
	 * @return void
271
	 */
272 7
	protected function setNameOrTextAttributeForTag( $value )
273
	{
274 7
		$attribute = in_array( $this->tag, static::TAGS_FORM )
275
			? 'name'
276 7
			: 'text';
277 7
		$this->args[$attribute] = $value;
278 7
	}
279
280
	/**
281
	 * @param string $method
282
	 * @return void
283
	 */
284 7
	protected function setTagFromMethod( $method )
285
	{
286 7
		$this->tag = strtolower( $method );
287 7
		if( in_array( $this->tag, static::INPUT_TYPES )) {
288
			$this->args['type'] = $this->tag;
289
			$this->tag = 'input';
290
		}
291 7
	}
292
}
293