1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\View; |
4
|
|
|
|
5
|
|
|
use Anax\View\Exception; |
6
|
|
|
use Anax\View\ViewRenderFile; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A view connected to a template file, supporting Anax DI. |
11
|
|
|
*/ |
12
|
|
|
class View |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var $template Template file or array |
16
|
|
|
* @var $templateData Data to send to template file |
17
|
|
|
* @var $sortOrder For sorting views |
18
|
|
|
* @var $type Type of view |
19
|
|
|
*/ |
20
|
|
|
private $template; |
21
|
|
|
private $templateData = []; |
22
|
|
|
private $sortOrder; |
23
|
|
|
private $type; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set values for the view. |
29
|
|
|
* |
30
|
|
|
* @param array|string|callable $template the template file, array |
31
|
|
|
* or callable |
32
|
|
|
* @param array $data variables to make available to the |
33
|
|
|
* view, default is empty |
34
|
|
|
* @param integer $sort which order to display the views, |
35
|
|
|
* if suitable |
36
|
|
|
* @param string $type which type of view |
37
|
|
|
* |
38
|
|
|
* @return self |
39
|
|
|
*/ |
40
|
6 |
|
public function set($template, $data = [], $sort = 0, $type = "file") |
41
|
|
|
{ |
42
|
6 |
|
if (empty($template)) { |
43
|
|
|
echo "DUMP"; |
44
|
|
|
$type = "empty"; |
45
|
6 |
|
} elseif (is_array($template)) { |
46
|
2 |
|
if (isset($template["callback"])) { |
47
|
2 |
|
$type = "callback"; |
48
|
2 |
|
$this->template = $template["callback"]; |
49
|
|
|
} else { |
50
|
|
|
$this->template = $template["template"]; |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
$this->sortOrder = $template["sort"] ?? $sort; |
54
|
2 |
|
$this->type = $template["type"] ?? $type; |
55
|
|
|
|
56
|
|
|
// Merge data array |
57
|
2 |
|
$data1 = $template["data"] ?? []; |
58
|
2 |
|
if (empty($data)) { |
59
|
|
|
$this->templateData = $data1; |
60
|
2 |
|
} else if (empty($data1)) { |
61
|
|
|
$this->templateData = $data; |
62
|
|
|
} else { |
63
|
2 |
|
foreach ($data as $key => $val) { |
64
|
2 |
|
if (is_array($val)) { |
65
|
1 |
|
$data1[$key] = array_merge($data1[$key], $val); |
66
|
|
|
} else { |
67
|
2 |
|
$data1[$key] = $val; |
68
|
|
|
} |
69
|
2 |
|
$this->templateData = $data1; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
$this->template = $template; |
77
|
4 |
|
$this->templateData = $data; |
78
|
4 |
|
$this->sortOrder = $sort; |
79
|
4 |
|
$this->type = $type; |
80
|
|
|
|
81
|
4 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Render the view by its type. |
88
|
|
|
* |
89
|
|
|
* @param object $di optional with access to the framework resources. |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
6 |
|
public function render(ContainerInterface $di = null) |
94
|
|
|
{ |
95
|
6 |
|
switch ($this->type) { |
96
|
|
|
case "file": |
97
|
|
|
if ($di->has("viewRenderFile")) { |
98
|
|
|
$viewRender = $di->get("viewRenderFile"); |
|
|
|
|
99
|
|
|
} else { |
100
|
|
|
$viewRender = new ViewRenderFile($di); |
|
|
|
|
101
|
|
|
$viewRender->setDI($di); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
$viewRender->render($this->template, $this->templateData); |
104
|
|
|
break; |
105
|
|
|
|
106
|
|
|
case "callback": |
107
|
4 |
|
if (!is_callable($this->template)) { |
108
|
|
|
throw new Exception("View is expecting a valid callback, provided callback seems to not be a callable."); |
109
|
|
|
} |
110
|
4 |
|
echo call_user_func($this->template, $this->templateData); |
111
|
4 |
|
break; |
112
|
|
|
|
113
|
|
|
case "string": |
114
|
1 |
|
echo $this->template; |
115
|
1 |
|
break; |
116
|
|
|
|
117
|
|
|
case "empty": |
118
|
|
|
break; |
119
|
|
|
|
120
|
|
|
default: |
121
|
1 |
|
throw new Exception("Not a valid template type: '{$this->type}'."); |
122
|
|
|
} |
123
|
5 |
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Give the sort order for this view. |
129
|
|
|
* |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
|
|
public function sortOrder() |
133
|
|
|
{ |
134
|
|
|
return $this->sortOrder; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: