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

Settings::normalizeValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 9
cp 0
crap 6
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use GeminiLabs\SiteReviews\Database\DefaultsManager;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Modules\Html\Field;
9
use GeminiLabs\SiteReviews\Modules\Html\Template;
10
use GeminiLabs\SiteReviews\Modules\Translator;
11
12
class Settings
13
{
14
	/**
15
	 * @param string $id
16
	 * @return string
17
	 */
18
	public function buildFields( $id )
19
	{
20
		$method = glsr( Helper::class )->buildMethodName( $id, 'getTemplateContextFor' );
21
		$context = !method_exists( $this, $method )
22
			? $this->getTemplateContext( $id )
23
			: $this->$method( $id );
24
		return glsr( Template::class )->build( 'pages/settings/'.$id, [
25
			'context' => $context,
26
		]);
27
	}
28
29
	/**
30
	 * @return string
31
	 */
32
	protected function getFieldDefault( array $field )
33
	{
34
		return isset( $field['default'] )
35
			? $field['default']
36
			: '';
37
	}
38
39
	/**
40
	 * @return array
41
	 */
42
	protected function getSettingFields( $path )
43
	{
44
		$settings = glsr( DefaultsManager::class )->settings();
45
		return array_filter( $settings, function( $key ) use( $path ) {
46
			return glsr( Helper::class )->startsWith( $path, $key );
47
		}, ARRAY_FILTER_USE_KEY );
48
	}
49
50
	/**
51
	 * @param string $id
52
	 * @return array
53
	 */
54
	protected function getTemplateContext( $id )
55
	{
56
		$fields = $this->getSettingFields( $this->normalizeSettingPath( $id ));
57
		$rows = '';
58
		foreach( $fields as $name => $field ) {
59
			$rows.= new Field( $this->normalize( $field, $name ));
60
		}
61
		return [
62
			'rows' => $rows,
63
		];
64
	}
65
66
	/**
67
	 * @return array
68
	 */
69
	protected function getTemplateContextForTranslations()
70
	{
71
		$translations = glsr( Translator::class )->renderAll();
72
		$class = empty( $translations )
73
			? 'glsr-hidden'
74
			: '';
75
		return [
76
			'class' => $class,
77
			'database_key' => OptionManager::databaseKey(),
78
			'translations' => $translations,
79
		];
80
	}
81
82
	/**
83
	 * @param string $name
84
	 * @return array
85
	 */
86
	protected function normalize( array $field, $name )
87
	{
88
		$field = wp_parse_args( $field, [
89
			'is_setting' => true,
90
			'name' => $name,
91
		]);
92
		$field = $this->normalizeLabel( $field );
93
		$field = $this->normalizeValue( $field );
94
		return $field;
95
	}
96
97
	/**
98
	 * @return array
99
	 */
100
	protected function normalizeLabel( array $field )
101
	{
102
		if( isset( $field['label'] )) {
103
			$field['legend'] = $field['label'];
104
			unset( $field['label'] );
105
		}
106
		else {
107
			$field['is_valid'] = false;
108
			glsr_log()->warning( 'Field is missing label' )->info( $field );
109
		}
110
		return $field;
111
	}
112
113
	/**
114
	 * @return array
115
	 */
116
	protected function normalizeValue( array $field )
117
	{
118
		if( !isset( $field['value'] )) {
119
			$field['value'] = glsr( OptionManager::class )->get(
120
				$field['name'],
121
				$this->getFieldDefault( $field )
122
			);
123
		}
124
		return $field;
125
	}
126
127
	/**
128
	 * @return string
129
	 */
130
	protected function normalizeSettingPath( $path )
131
	{
132
		return glsr( Helper::class )->prefixString( rtrim( $path, '.' ), 'settings.' );
133
	}
134
}
135