-
Notifications
You must be signed in to change notification settings - Fork 14
/
Renderer.php
executable file
·116 lines (100 loc) · 2.54 KB
/
Renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/*
* This file is part of Spoon Library.
*
* (c) Davy Hellemans <davy@spoon-library.com>
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/
namespace Spoon\Template;
/**
* Class used as a base class for the rendered template.
*
* @author Davy Hellemans <davy@spoon-library.com>
*/
class Renderer extends Template
{
/**
* Method used to fetch the actual value based on the context and a list of keys.
*
* @todo refactor?
* @todo $context should be optional and act that way by returning null if so
*
* @param array $context
* @param array $elements
* @return mixed
*/
public function getVar(array $context, array $elements)
{
// only 1 element
if(count($elements) == 1)
{
if(array_key_exists($elements[0], $context))
{
return $context[$elements[0]];
}
return null;
}
$i = 1;
foreach($elements as $index => $value)
{
// first element is custom
if($i == 1)
{
$variable = $context[$value];
}
// other (regular) elements
else
{
// an array
if(is_array($variable))
{
$variable = $variable[$value];
}
// an object
elseif(is_object($variable))
{
$arguments = (is_array($value)) ? $value : null;
$element = (is_array($value)) ? $index : $value;
// public property exists (and is not null)
if(property_exists($variable, $element) && isset($variable->$element))
{
$variable = $variable->$element;
}
// public method exists
elseif(method_exists($variable, $element) && is_callable(array($variable, $element)))
{
if($arguments !== null)
{
$variable = call_user_func_array(array($variable, $element), $arguments);
}
else $variable = $variable->$element();
}
// public getter exists
elseif(method_exists($variable, 'get' . ucfirst($element)) && is_callable(array($variable, 'get' . ucfirst($element))))
{
$method = 'get' . ucfirst($element);
if($arguments !== null)
{
$variable = call_user_func_array(array($variable, $method), $arguments);
}
else $variable = $variable->$method();
}
// magic getter
elseif(method_exists($variable, '__get') && is_callable(array($variable, '__get')))
{
if($element !== null) $variable = $variable->$element;
else $variable = null;
}
// everythine failed
else $variable = null;
}
// not an object, nor array
else return null;
}
$i++;
}
return $variable;
}
}