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 $template the template file, or array |
31
|
|
|
* @param array $data variables to make available to the |
32
|
|
|
* view, default is empty |
33
|
|
|
* @param integer $sort which order to display the views, |
34
|
|
|
* if suitable |
35
|
|
|
* @param string $type which type of view |
36
|
|
|
* |
37
|
|
|
* @return self |
38
|
|
|
*/ |
39
|
2 |
|
public function set($template, $data = [], $sort = 0, $type = "file") |
40
|
|
|
{ |
41
|
2 |
|
if (is_array($template)) { |
42
|
|
|
if (isset($template["callback"])) { |
43
|
|
|
$type = "callback"; |
44
|
|
|
$this->template = $template; |
45
|
|
|
} else { |
46
|
|
|
$this->template = $template["template"]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->sortOrder = $template["sort"] ?? $sort; |
50
|
|
|
$this->type = $template["type"] ?? $type; |
51
|
|
|
|
52
|
|
|
// Merge data array |
53
|
|
|
$data1 = $template["data"] ?? []; |
54
|
|
|
if (empty($data)) { |
55
|
|
|
$this->templateData = $data1; |
56
|
|
|
} else if (empty($data1)) { |
57
|
|
|
$this->templateData = $data; |
58
|
|
|
} else { |
59
|
|
|
foreach ($data as $key => $val) { |
60
|
|
|
if (is_array($val)) { |
61
|
|
|
$data1[$key] = array_merge($data1[$key], $val); |
62
|
|
|
} else { |
63
|
|
|
$data1[$key] = $val; |
64
|
|
|
} |
65
|
|
|
$this->templateData = $data1; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
$this->template = $template; |
73
|
2 |
|
$this->templateData = $data; |
74
|
2 |
|
$this->sortOrder = $sort; |
75
|
2 |
|
$this->type = $type; |
76
|
|
|
|
77
|
2 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Render the view by its type. |
84
|
|
|
* |
85
|
|
|
* @param object $di optional with access to the framework resources. |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
2 |
|
public function render(ContainerInterface $di = null) |
90
|
|
|
{ |
91
|
2 |
|
switch ($this->type) { |
92
|
|
|
case "file": |
93
|
|
|
if ($di->has("viewRenderFile")) { |
94
|
|
|
$viewRender = $di->get("viewRenderFile"); |
|
|
|
|
95
|
|
|
} else { |
96
|
|
|
$viewRender = new ViewRenderFile($di); |
|
|
|
|
97
|
|
|
$viewRender->setDI($di); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
$viewRender->render($this->template, $this->templateData); |
100
|
|
|
break; |
101
|
|
|
|
102
|
|
|
case "callback": |
103
|
|
|
if (!isset($this->template["callback"]) || !is_callable($this->template["callback"])) { |
104
|
|
|
throw new Exception("View missing callback."); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
echo call_user_func($this->template["callback"]); |
108
|
|
|
|
109
|
|
|
break; |
110
|
|
|
|
111
|
|
|
case "string": |
112
|
1 |
|
echo $this->template; |
113
|
|
|
|
114
|
1 |
|
break; |
115
|
|
|
|
116
|
|
|
default: |
117
|
1 |
|
throw new Exception("Not a valid template type: {$this->type}"); |
118
|
|
|
} |
119
|
1 |
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Give the sort order for this view. |
125
|
|
|
* |
126
|
|
|
* @return int |
127
|
|
|
*/ |
128
|
|
|
public function sortOrder() |
129
|
|
|
{ |
130
|
|
|
return $this->sortOrder; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: