Passed
Push — master ( af44f0...ee0da0 )
by Paul
04:21
created

Builder   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Test Coverage

Coverage 42.71%

Importance

Changes 0
Metric Value
wmc 37
dl 0
loc 266
ccs 41
cts 96
cp 0.4271
rs 8.6
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFormInput() 0 8 3
A buildFieldDescription() 0 4 2
B normalize() 0 12 5
A buildCustomField() 0 8 2
A buildTag() 0 6 2
A buildFormSelect() 0 3 1
A buildFormLabel() 0 6 3
A buildFormTextarea() 0 3 1
A buildFormInputMultiChoice() 0 11 1
A buildDefaultTag() 0 6 2
A buildFormSelectOptions() 0 7 1
A setNameOrTextAttributeForTag() 0 6 2
A buildFormInputChoice() 0 3 1
A setTagFromMethod() 0 6 2
A __call() 0 13 3
A getClosingTag() 0 3 1
A __construct() 0 3 1
A getOpeningTag() 0 4 1
A __set() 0 12 3
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
		'button', '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_STRUCTURE = [
22
		'div', 'form', 'nav', 'ol', 'section', 'ul',
23
	];
24
25
	const TAGS_TEXT = [
26
		'a', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'label', 'li', 'option', 'p', 'pre', 'small',
27
		'span',
28
	];
29
30
	/**
31
	 * @var array
32
	 */
33
	public $args = [];
34
35
	/**
36
	 * @var array
37
	 */
38
	public $globals = [];
39
40
	/**
41
	 * @var bool
42
	 */
43
	public $render = false;
44
45
	/**
46
	 * @var string
47
	 */
48
	public $tag;
49
50 7
	public function __construct( array $globals = [] )
51
	{
52 7
		$this->globals = $globals;
53 7
	}
54
55
	/**
56
	 * @param string $method
57
	 * @param array $args
58
	 * @return string|void
59
	 */
60 7
	public function __call( $method, $args )
61
	{
62 7
		$instance = new static();
63 7
		$instance->setTagFromMethod( $method );
64 7
		call_user_func_array( [$instance, 'normalize'], $args += ['',''] );
65 7
		$tags = array_merge( static::TAGS_FORM, static::TAGS_STRUCTURE, static::TAGS_TEXT );
66 7
		$generatedTag = in_array( $instance->tag, $tags )
67 7
			? $instance->buildTag()
68 7
			: $instance->buildCustomField();
69 7
		if( !$this->render ) {
70 7
			return $generatedTag;
71
		}
72
		echo $generatedTag;
73
	}
74
75
	/**
76
	 * @param string $property
77
	 * @param mixed $value
78
	 * @return void
79
	 */
80
	public function __set( $property, $value )
81
	{
82
		$properties = [
83
			'args' => 'is_array',
84
			'globals' => 'is_array',
85
			'render' => 'is_bool',
86
			'tag' => 'is_string',
87
		];
88
		if( !isset( $properties[$property] )
89
			|| empty( array_filter( [$value], $properties[$property] ))
90
		)return;
91
		$this->$property = $value;
92
	}
93
94
	/**
95
	 * @return string
96
	 */
97 7
	public function getClosingTag()
98
	{
99 7
		return '</'.$this->tag.'>';
100
	}
101
102
	/**
103
	 * @return string
104
	 */
105 7
	public function getOpeningTag()
106
	{
107 7
		$attributes = glsr( Attributes::class )->{$this->tag}( $this->args )->toString();
108 7
		return '<'.trim( $this->tag.' '.$attributes ).'>';
109
	}
110
111
	/**
112
	 * @return string|void
113
	 */
114
	protected function buildCustomField()
115
	{
116
		$className = glsr( Helper::class )->buildClassName( $this->tag, __NAMESPACE__.'\Fields' );
117
		if( !class_exists( $className )) {
118
			glsr_log()->error( 'Field missing: '.$className );
119
			return;
120
		}
121
		return (new $className( $this ))->build();
122
	}
123
124
	/**
125
	 * @return string|void
126
	 */
127 7
	protected function buildDefaultTag( $text = '' )
128
	{
129 7
		if( empty( $text )) {
130 7
			$text = $this->args['text'];
131
		}
132 7
		return $this->getOpeningTag().$text.$this->getClosingTag();
133
	}
134
135
	/**
136
	 * @return string|void
137
	 */
138
	protected function buildFieldDescription()
139
	{
140
		if( !empty( $this->args['description'] )) {
141
			return $this->small( $this->args['description'] );
142
		}
143
	}
144
145
	/**
146
	 * @return string|void
147
	 */
148
	protected function buildFormInput()
149
	{
150
		if( !in_array( $this->args['type'], ['checkbox', 'radio'] )) {
151
			return $this->buildFormLabel().$this->getOpeningTag();
152
		}
153
		return empty( $this->args['options'] )
154
			? $this->buildFormInputChoice()
155
			: $this->buildFormInputMultiChoice();
156
	}
157
158
	/**
159
	 * @return string|void
160
	 */
161
	protected function buildFormInputChoice()
162
	{
163
		return $this->label( $this->getOpeningTag().' '.$this->args['text'] );
164
	}
165
166
	/**
167
	 * @return string|void
168
	 */
169
	protected function buildFormInputMultiChoice()
170
	{
171
		$options = array_reduce( array_keys( $this->args['options'] ), function( $carry, $key ) {
172
			return $carry.$this->li( $this->{$this->args['type']}([
173
				'checked' => in_array( $key, (array)$this->args['value'] ),
174
				'name' => $this->args['name'].'[]',
175
				'text' => $this->args['options'][$key],
176
				'value' => $key,
177
			]));
178
		});
179
		return $this->ul( $options );
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();
222
	}
223
224
	/**
225
	 * @return string|void
226
	 */
227 7
	protected function buildTag()
228
	{
229 7
		if( !in_array( $this->tag, static::TAGS_FORM )) {
230 7
			return $this->buildDefaultTag();
231
		}
232
		return call_user_func( [$this, 'buildForm'.ucfirst( $this->tag )] ).$this->buildFieldDescription();
233
	}
234
235
	/**
236
	 * @param string|array ...$params
237
	 * @return void
238
	 */
239 7
	protected function normalize( ...$params )
240
	{
241 7
		if( is_string( $params[0] ) || is_numeric( $params[0] )) {
242 7
			$this->setNameOrTextAttributeForTag( $params[0] );
243
		}
244 7
		if( is_array( $params[0] )) {
245
			$this->args += $params[0];
246
		}
247 7
		else if( is_array( $params[1] )) {
248 7
			$this->args += $params[1];
249
		}
250 7
		$this->args = glsr( BuilderDefaults::class )->merge( $this->args );
251 7
	}
252
253
	/**
254
	 * @param string $value
255
	 * @return void
256
	 */
257 7
	protected function setNameOrTextAttributeForTag( $value )
258
	{
259 7
		$attribute = in_array( $this->tag, static::TAGS_FORM )
260
			? 'name'
261 7
			: 'text';
262 7
		$this->args[$attribute] = $value;
263 7
	}
264
265
	/**
266
	 * @param string $method
267
	 * @return void
268
	 */
269 7
	protected function setTagFromMethod( $method )
270
	{
271 7
		$this->tag = strtolower( $method );
272 7
		if( in_array( $this->tag, static::INPUT_TYPES )) {
273
			$this->args['type'] = $this->tag;
274
			$this->tag = 'input';
275
		}
276 7
	}
277
}
278