Cancelled
Push — master ( 9c2a96...cfa019 )
by Paul
04:39
created

Field::buildSettingField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 7
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
		return glsr( Template::class )->build( 'partials/form/table-row', [
86
			'context' => [
87
				'class' => $this->getFieldClass(),
88
				'field' => glsr( Builder::class )->{$this->field['type']}( $this->field ),
89
				'label' => glsr( Builder::class )->label( $this->field['legend'], ['for' => $this->field['id']] ),
90
			],
91
		]);
92
	}
93
94
	/**
95
	 * @return string
96
	 */
97
	protected function buildSettingMultiField()
98
	{
99
		$dependsOn = $this->getFieldDependsOn();
100
		unset( $this->field['data-depends'] );
101
		return glsr( Template::class )->build( 'partials/form/table-row-multiple', [
102
			'context' => [
103
				'class' => $this->getFieldClass(),
104
				'depends_on' => $dependsOn,
105
				'field' => glsr( Builder::class )->{$this->field['type']}( $this->field ),
106
				'label' => glsr( Builder::class )->label( $this->field['legend'], ['for' => $this->field['id']] ),
107
				'legend' => $this->field['legend'],
108
			],
109
		]);
110
	}
111
112
	/**
113
	 * @return string
114
	 */
115
	protected function getFieldClass()
116
	{
117
		$classes = [];
118
		if( $this->field['is_hidden'] ) {
119
			$classes[] = 'hidden';
120
		}
121
		if( !empty( $this->field['required'] )) {
122
			$classes[] = 'glsr-required';
123
		}
124
		return implode( ' ', $classes );
125
	}
126
127
	/**
128
	 * @return string
129
	 */
130
	protected function getFieldDependsOn()
131
	{
132
		return !empty( $this->field['data-depends'] )
133
			? $this->field['data-depends']
134
			: '';
135
	}
136
137
	/**
138
	 * @return string
139
	 */
140
	protected function getFieldPrefix()
141
	{
142
		return $this->field['is_setting']
143
			? OptionManager::databaseKey()
144
			: Application::ID;
145
	}
146
147
	/**
148
	 * @return bool
149
	 */
150
	protected function isFieldValid()
151
	{
152
		$missingValues = [];
153
		$requiredValues = [
154
			'name', 'type',
155
		];
156
		foreach( $requiredValues as $value ) {
157
			if( isset( $this->field[$value] ))continue;
158
			$missingValues[] = $value;
159
			$this->field['is_valid'] = false;
160
		}
161
		if( !empty( $missingValues )) {
162
			glsr_log()
163
				->warning( 'Field is missing: '.implode( ', ', $missingValues ))
164
				->info( $this->field );
165
		}
166
		return $this->field['is_valid'];
167
	}
168
169
	/**
170
	 * @return void
171
	 */
172
	protected function normalize()
173
	{
174
		if( !$this->isFieldValid() )return;
175
		$this->field['path'] = $this->field['name'];
176
		$className = glsr( Helper::class )->buildClassName( $this->field['type'], __NAMESPACE__.'\Fields' );
177
		if( class_exists( $className )) {
178
			$this->field = array_merge(
179
				wp_parse_args( $this->field, $className::defaults() ),
180
				$className::required()
181
			);
182
		}
183
		$this->normalizeId();
184
		$this->normalizeName();
185
		$this->determineIfMulti();
186
	}
187
188
	/**
189
	 * @return void
190
	 */
191
	protected function normalizeId()
192
	{
193
		if( isset( $this->field['id'] ) || $this->field['is_raw'] )return;
194
		$this->field['id'] = glsr( Helper::class )->convertPathToId(
195
			$this->field['path'],
196
			$this->getFieldPrefix()
197
		);
198
	}
199
200
	/**
201
	 * @return void
202
	 */
203
	protected function normalizeName()
204
	{
205
206
		$this->field['name'] = glsr( Helper::class )->convertPathToName(
207
			$this->field['path'],
208
			$this->getFieldPrefix()
209
		);
210
	}
211
212
	/**
213
	 * @return void
214
	 */
215
	protected function determineIfMulti()
216
	{
217
		if( !in_array( $this->field['type'], static::MULTI_FIELD_TYPES ))return;
218
		$this->field['is_multi'] = true;
219
	}
220
}
221