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

Form::normalizeSettingsPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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\Helper;
7
use GeminiLabs\SiteReviews\Modules\Html\Field;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
10
class Form
11
{
12
	/**
13
	 * @param string $id
14
	 * @return void
15
	 */
16
	public function renderFields( $id )
17
	{
18
		$fields = $this->getSettingFields( $this->normalizeSettingPath( $id ));
19
		$rows = '';
20
		foreach( $fields as $name => $field ) {
21
			$field = wp_parse_args( $field, ['name' => $name] );
22
			$rows.= (new Field( $field ))->build();
23
		}
24
		glsr( Template::class )->render( 'pages/settings/'.$id, [
25
			'context' => [
26
				'rows' => $rows,
27
			],
28
		]);
29
	}
30
31
	/**
32
	 * @return array
33
	 */
34
	protected function getSettingFields( $path )
35
	{
36
		$settings = glsr( DefaultsManager::class )->settings();
37
		return array_filter( $settings, function( $key ) use( $path ) {
38
			return glsr( Helper::class )->startsWith( $path, $key );
39
		}, ARRAY_FILTER_USE_KEY );
40
	}
41
42
	/**
43
	 * @return string
44
	 */
45
	protected function normalizeSettingPath( $path )
46
	{
47
		return glsr( Helper::class )->prefixString( rtrim( $path, '.' ), 'settings.' );
48
	}
49
}
50