Passed
Push — master ( 992102...9c2a96 )
by Paul
04:54
created

Field::normalizeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Modules\Html\Builder;
9
use GeminiLabs\SiteReviews\Modules\Html\Template;
10
11
class Field
12
{
13
	const MULTI_FIELD_TYPES = ['radio', 'checkbox'];
14
15
	/**
16
	 * @var array
17
	 */
18
	public $field;
19
20
	public function __construct( array $field = [] )
21
	{
22
		$this->field = wp_parse_args( $field, [
23
			'is_hidden' => false,
24
			'is_multi' => false,
25
			'is_raw' => false,
26
			'is_setting' => false,
27
			'is_valid' => true,
28
			'path' => '',
29
		]);
30
		$this->normalize();
31
	}
32
33
	/**
34
	 * @return string
35
	 */
36
	public function __toString()
37
	{
38
		return (string)$this->build();
39
	}
40
41
	/**
42
	 * @return string
43
	 */
44
	public function build()
45
	{
46
		if( !$this->field['is_valid'] )return;
47
		if( $this->field['is_raw'] ) {
48
			return glsr( Builder::class )->hidden( $this->field );
49
		}
50
		if( !$this->field['is_setting'] ) {
51
			return $this->buildField();
52
		}
53
		if( !$this->field['is_multi'] ) {
54
			return $this->buildSettingField();
55
		}
56
		return $this->buildSettingMultiField();
57
	}
58
59
	/**
60
	 * @return void
61
	 */
62
	public function render()
63
	{
64
		echo $this->build();
65
	}
66
67
	/**
68
	 * @return string
69
	 */
70
	protected function buildField()
71
	{
72
		return glsr( Template::class )->build( 'partials/form/field', [
73
			'context' => [
74
				'class' => $this->getFieldClass(),
75
				'field' => glsr( Builder::class )->{$this->field['type']}( $this->field ),
76
			],
77
		]);
78
	}
79
80
	/**
81
	 * @return string
82
	 */
83
	protected function buildSettingField()
84
	{
85
		$this->field['data-depends'] = $this->getFieldDependsOn();
86
		return glsr( Template::class )->build( 'partials/form/table-row', [
87
			'context' => [
88
				'class' => $this->getFieldClass(),
89
				'field' => glsr( Builder::class )->{$this->field['type']}( $this->field ),
90
				'label' => glsr( Builder::class )->label( $this->field['legend'], ['for' => $this->field['id']] ),
91
			],
92
		]);
93
	}
94
95
	/**
96
	 * @return string
97
	 */
98
	protected function buildSettingMultiField()
99
	{
100
		return glsr( Template::class )->build( 'partials/form/table-row-multiple', [
101
			'context' => [
102
				'class' => $this->getFieldClass(),
103
				'depends_on' => $this->getFieldDependsOn(),
104
				'field' => glsr( Builder::class )->{$this->field['type']}( $this->field ),
105
				'label' => glsr( Builder::class )->label( $this->field['legend'], ['for' => $this->field['id']] ),
106
				'legend' => $this->field['legend'],
107
			],
108
		]);
109
	}
110
111
	/**
112
	 * @return string
113
	 */
114
	protected function getFieldClass()
115
	{
116
		$classes = [];
117
		if( $this->field['is_hidden'] ) {
118
			$classes[] = 'hidden';
119
		}
120
		if( !empty( $this->field['required'] )) {
121
			$classes[] = 'glsr-required';
122
		}
123
		return implode( ' ', $classes );
124
	}
125
126
	/**
127
	 * @return string
128
	 */
129
	protected function getFieldDependsOn()
130
	{
131
		return !empty( $this->field['depends_on'] )
132
			? $this->field['depends_on']
133
			: '';
134
	}
135
136
	/**
137
	 * @return string
138
	 */
139
	protected function getFieldPrefix()
140
	{
141
		return $this->field['is_setting']
142
			? OptionManager::databaseKey()
143
			: Application::ID;
144
	}
145
146
	/**
147
	 * @param string $path
148
	 * @param string|array $expectedValue
149
	 * @return bool
150
	 */
151
	protected function isFieldHidden( $path, $expectedValue )
152
	{
153
		$optionValue = glsr( OptionManager::class )->get(
154
			$path,
155
			glsr( Helper::class )->getPathValue( $path, glsr()->defaults )
156
		);
157
		if( is_array( $expectedValue )) {
158
			return !in_array( $optionValue, $expectedValue );
159
		}
160
		return $optionValue != $expectedValue;
161
	}
162
163
	/**
164
	 * @return bool
165
	 */
166
	protected function isFieldValid()
167
	{
168
		$missingValues = [];
169
		$requiredValues = [
170
			'name', 'type',
171
		];
172
		foreach( $requiredValues as $value ) {
173
			if( isset( $this->field[$value] ))continue;
174
			$missingValues[] = $value;
175
			$this->field['is_valid'] = false;
176
		}
177
		if( !empty( $missingValues )) {
178
			glsr_log()
179
				->warning( 'Field is missing: '.implode( ', ', $missingValues ))
180
				->info( $this->field );
181
		}
182
		return $this->field['is_valid'];
183
	}
184
185
	/**
186
	 * @return void
187
	 */
188
	protected function normalize()
189
	{
190
		if( !$this->isFieldValid() )return;
191
		$className = glsr( Helper::class )->buildClassName( $this->field['type'], __NAMESPACE__.'\Fields' );
192
		if( class_exists( $className )) {
193
			$this->field = array_merge(
194
				wp_parse_args( $this->field, $className::defaults() ),
195
				$className::required()
196
			);
197
		}
198
		$this->normalizeDependsOn();
199
		$this->normalizeId();
200
		$this->normalizeName();
201
		$this->normalizeType();
202
	}
203
204
	/**
205
	 * @return void
206
	 */
207
	protected function normalizeDependsOn()
208
	{
209
		if( empty( $this->field['depends_on'] ) || !is_array( $this->field['depends_on'] ))return;
210
		$path = key( $this->field['depends_on'] );
211
		$value = $this->field['depends_on'][$path];
212
		$this->field['depends_on'] = json_encode([
213
			'name' => glsr( Helper::class )->convertPathToName( $path, $this->getFieldPrefix() ),
214
			'value' => $value,
215
		], JSON_HEX_APOS|JSON_HEX_QUOT );
216
		$this->field['is_hidden'] = $this->isFieldHidden( $path, $value );
217
	}
218
219
	/**
220
	 * @return void
221
	 */
222
	protected function normalizeId()
223
	{
224
		if( isset( $this->field['id'] ) || $this->field['is_raw'] )return;
225
		$this->field['id'] = glsr( Helper::class )->convertNameToId( $this->field['name'] );
226
	}
227
228
	/**
229
	 * @return void
230
	 */
231
	protected function normalizeName()
232
	{
233
		$this->field['path'] = $this->field['name'];
234
		$this->field['name'] = glsr( Helper::class )->convertPathToName(
235
			$this->field['path'],
236
			$this->getFieldPrefix()
237
		);
238
	}
239
240
	/**
241
	 * @return void
242
	 */
243
	protected function normalizeType()
244
	{
245
		if( in_array( $this->field['type'], static::MULTI_FIELD_TYPES )) {
246
			$this->field['is_multi'] = true;
247
		}
248
	}
249
}
250