Cancelled
Push — master ( 28a5cf...9e6262 )
by Paul
06:07
created

Field::normalizeFieldValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 12
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_valid' => true,
26
			'path' => '',
27
		]);
28
		$this->normalize();
29
	}
30
31
	/**
32
	 * @return string
33
	 */
34
	public function build()
35
	{
36
		if( !$this->field['is_valid'] )return;
37
		if( $this->field['is_multi'] ) {
38
			return $this->buildMultiField();
39
		}
40
		$this->field['data-depends'] = $this->getFieldDepends();
41
		return glsr( Template::class )->build( 'settings/form-table-row', [
42
			'context' => [
43
				'class' => $this->getFieldClass(),
44
				'field' => glsr( Builder::class )->{$this->field['type']}( $this->field ),
45
				'label' => glsr( Builder::class )->label( $this->field['legend'], ['for' => $this->field['id']] ),
46
			],
47
		]);
48
	}
49
50
	/**
51
	 * @return void
52
	 */
53
	public function render()
54
	{
55
		echo $this->build();
56
	}
57
58
	/**
59
	 * @return string
60
	 */
61
	protected function buildMultiField()
62
	{
63
		return glsr( Template::class )->build( 'settings/form-table-row-multiple', [
64
			'context' => [
65
				'class' => $this->getFieldClass(),
66
				'depends' => $this->getFieldDepends(),
67
				'field' => glsr( Builder::class )->{$this->field['type']}( $this->field ),
68
				'label' => glsr( Builder::class )->label( $this->field['legend'], ['for' => $this->field['id']] ),
69
				'legend' => $this->field['legend'],
70
			],
71
		]);
72
	}
73
74
	/**
75
	 * @return string
76
	 */
77
	protected function getFieldClass()
78
	{
79
		return $this->field['is_hidden']
80
			? 'hidden'
81
			: '';
82
	}
83
84
	/**
85
	 * @return string
86
	 */
87
	protected function getFieldDepends()
88
	{
89
		return !empty( $this->field['depends'] )
90
			? $this->field['depends']
91
			: '';
92
	}
93
94
	/**
95
	 * @param string $path
96
	 * @param string $expectedValue
97
	 * @return bool
98
	 */
99
	protected function isFieldHidden( $path, $expectedValue )
100
	{
101
		$optionValue = glsr( OptionManager::class )->get( $path );
102
		if( is_array( $optionValue )) {
103
			return !in_array( $expectedValue, $optionValue );
104
		}
105
		return $optionValue != $expectedValue;
106
	}
107
108
	/**
109
	 * @return bool
110
	 */
111
	protected function isFieldValid()
112
	{
113
		$isValid = true;
114
		$missingValues = [];
115
		$requiredValues = [
116
			'label', 'name', 'type',
117
		];
118
		foreach( $requiredValues as $value ) {
119
			if( isset( $this->field[$value] ))continue;
120
			$missingValues[] = $value;
121
			$isValid = $this->field['is_valid'] = false;
122
		}
123
		if( !empty( $missingValues )) {
124
			glsr_log()
125
				->warning( 'Field is missing: '.implode( ', ', $missingValues ))
126
				->info( $this->field );
127
		}
128
		return $isValid;
129
	}
130
131
	/**
132
	 * @return void
133
	 */
134
	protected function normalize()
135
	{
136
		if( !$this->isFieldValid() )return;
137
		$field = $this->field;
138
		foreach( $field as $key => $value ) {
139
			$methodName = glsr( Helper::class )->buildMethodName( $key, 'normalize' );
140
			if( !method_exists( $this, $methodName ))continue;
141
			$this->$methodName();
142
		}
143
		$this->normalizeFieldId();
144
		$this->normalizeFieldType();
145
		$this->normalizeFieldValue();
146
	}
147
148
	/**
149
	 * @return void
150
	 */
151
	protected function normalizeDepends()
152
	{
153
		if( empty( $this->field['depends'] ) || !is_array( $this->field['depends'] ))return;
154
		$path = key( $this->field['depends'] );
155
		$value = $this->field['depends'][$path];
156
		$this->field['depends'] = json_encode([
157
			'name' => glsr( Helper::class )->convertPathToName( $path, Application::ID ),
158
			'value' => $value,
159
		], JSON_HEX_APOS|JSON_HEX_QUOT );
160
		$this->field['is_hidden'] = $this->isFieldHidden( $path, $value );
161
	}
162
163
	/**
164
	 * @return void
165
	 */
166
	protected function normalizeFieldId()
167
	{
168
		if( isset( $this->field['id'] ))return;
169
		$this->field['id'] = glsr( Helper::class )->convertNameToId( $this->field['name'] );
170
	}
171
172
	/**
173
	 * @return void
174
	 */
175
	protected function normalizeFieldType()
176
	{
177
		$className = glsr( Helper::class )->buildClassName( $this->field['type'], 'Modules\Html\Fields' );
178
		if( class_exists( $className )) {
179
			$this->field = array_merge( $this->field, glsr( $className )->defaults() );
0 ignored issues
show
Bug introduced by
The method defaults() does not exist on GeminiLabs\SiteReviews\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
			$this->field = array_merge( $this->field, glsr( $className )->/** @scrutinizer ignore-call */ defaults() );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
180
		}
181
		if( in_array( $this->field['type'], static::MULTI_FIELD_TYPES )) {
182
			$this->field['is_multi'] = true;
183
		}
184
	}
185
186
	/**
187
	 * @return void
188
	 */
189
	protected function normalizeFieldValue()
190
	{
191
		if( isset( $this->field['value'] ))return;
192
		$defaultValue = isset( $this->field['default'] )
193
			? $this->field['default']
194
			: '';
195
		$this->field['value'] = glsr( OptionManager::class )->get( $this->field['path'], $defaultValue );
196
	}
197
198
	/**
199
	 * @return void
200
	 */
201
	protected function normalizeLabel()
202
	{
203
		$this->field['legend'] = $this->field['label'];
204
		unset( $this->field['label'] );
205
	}
206
207
	/**
208
	 * @return void
209
	 */
210
	protected function normalizeName()
211
	{
212
		$this->field['path'] = $this->field['name'];
213
		$this->field['name'] = glsr( Helper::class )->convertPathToName(
214
			$this->field['name'],
215
			Application::ID
216
		);
217
	}
218
}
219