Passed
Push — develop ( 3d135a...646156 )
by Paul
02:56
created

Component::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
use GeminiLabs\Pollux\Application;
6
7
abstract class Component
8
{
9
	/**
10
	 * @var Application
11
	 */
12
	protected $app;
13
14
	public function __construct( Application $app )
15
	{
16
		$this->app = $app;
17
	}
18
19
	/**
20
	 * @return void
21
	 */
22
	abstract public function init();
23
24
	/**
25
	 * @return void|array
26
	 */
27
	abstract public function register();
28
29
	/**
30
	 * @return void
31
	 */
32
	abstract protected function normalize();
33
34
	/**
35
	 * @param string $view
36
	 * @return void
37
	 */
38
	public function render( $view, array $data = [] )
39
	{
40
		$file = apply_filters( 'pollux/views/file',
41
			$this->app->path( sprintf( 'views/%s.php', str_replace( '.php', '', $view ))),
42
			$view,
43
			$data
44
		);
45
		if( file_exists( $file )) {
46
			extract( $data );
47
			return include $file;
48
		}
49
	}
50
51
	/**
52
	 * @param string $id
53
	 * @return array
54
	 */
55
	protected function normalizeThis( array $data, array $defaults, $id )
56
	{
57
		$data = wp_parse_args( $data, $defaults );
58
		foreach( $defaults as $key => $value ) {
59
			$method = $this->app->make( 'Helper' )->buildMethodName( $key, 'normalize' );
60
			if( method_exists( $this, $method )) {
61
				$data[$key] = $this->$method( $data[$key], $data, $id );
62
			}
63
		}
64
		return $data;
65
	}
66
}
67