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

Form::build()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
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 4
nop 2
dl 0
loc 12
ccs 0
cts 9
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\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