Passed
Push — master ( 8a7fa4...223132 )
by Paul
06:19
created

Form::getTemplateContextForTranslations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 10
cp 0
crap 6
rs 9.4285
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 Form
13
{
14
	/**
15
	 * @param string $id
16
	 * @return void
17
	 */
18
	public function renderFields( $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
		glsr( Template::class )->render( 'pages/settings/'.$id, [
25
			'context' => $context,
26
		]);
27
	}
28
29
	/**
30
	 * @return array
31
	 */
32
	protected function getSettingFields( $path )
33
	{
34
		$settings = glsr( DefaultsManager::class )->settings();
35
		return array_filter( $settings, function( $key ) use( $path ) {
36
			return glsr( Helper::class )->startsWith( $path, $key );
37
		}, ARRAY_FILTER_USE_KEY );
38
	}
39
40
	/**
41
	 * @param string $id
42
	 * @return array
43
	 */
44
	protected function getTemplateContext( $id )
45
	{
46
		$fields = $this->getSettingFields( $this->normalizeSettingPath( $id ));
47
		$rows = '';
48
		foreach( $fields as $name => $field ) {
49
			$field = wp_parse_args( $field, ['name' => $name] );
50
			$rows.= (new Field( $field ))->build();
51
		}
52
		return [
53
			'rows' => $rows,
54
		];
55
	}
56
57
	/**
58
	 * @return array
59
	 */
60
	protected function getTemplateContextForTranslations()
61
	{
62
		$translations = glsr( Translator::class )->renderAll();
63
		$class = empty( $translations )
64
			? 'glsr-hidden'
65
			: '';
66
		return [
67
			'class' => $class,
68
			'database_key' => OptionManager::databaseKey(),
69
			'translations' => $translations,
70
		];
71
	}
72
73
	/**
74
	 * @return string
75
	 */
76
	protected function normalizeSettingPath( $path )
77
	{
78
		return glsr( Helper::class )->prefixString( rtrim( $path, '.' ), 'settings.' );
79
	}
80
}
81