|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\App\View; |
|
4
|
|
|
|
|
5
|
|
|
use JsonSerializable; |
|
6
|
|
|
|
|
7
|
|
|
use function array_merge; |
|
8
|
|
|
|
|
9
|
|
|
class Store implements JsonSerializable |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The view renderer |
|
13
|
|
|
* |
|
14
|
|
|
* @var ViewInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $xRenderer; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The view namespace |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $sNamespace; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The view name |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $sViewName; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The view data |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $aViewData = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Make a piece of data available for the rendered view |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $sName The data name |
|
|
|
|
|
|
43
|
|
|
* @param mixed $xValue The data value |
|
|
|
|
|
|
44
|
|
|
* |
|
45
|
|
|
* @return Store |
|
46
|
|
|
*/ |
|
47
|
|
|
public function with(string $sName, $xValue): Store |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$this->aViewData[$sName] = $xValue; |
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Set the data to be rendered |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $aViewData The view data |
|
|
|
|
|
|
57
|
|
|
* |
|
58
|
|
|
* @return Store |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setData(array $aViewData): Store |
|
61
|
|
|
{ |
|
62
|
|
|
$this->aViewData = array_merge($this->aViewData, $aViewData); |
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set the view to be rendered, with optional data |
|
68
|
|
|
* |
|
69
|
|
|
* @param ViewInterface $xRenderer The view renderer |
|
|
|
|
|
|
70
|
|
|
* @param string $sNamespace The view namespace |
|
|
|
|
|
|
71
|
|
|
* @param string $sViewName The view name |
|
|
|
|
|
|
72
|
|
|
* @param array $aViewData The view data |
|
|
|
|
|
|
73
|
|
|
* |
|
74
|
|
|
* @return Store |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setView(ViewInterface $xRenderer, string $sNamespace, string $sViewName, array $aViewData = []): Store |
|
77
|
|
|
{ |
|
78
|
|
|
$this->xRenderer = $xRenderer; |
|
79
|
|
|
$this->sNamespace = trim($sNamespace); |
|
80
|
|
|
$this->sViewName = trim($sViewName); |
|
81
|
|
|
$this->aViewData = array_merge($this->aViewData, $aViewData); |
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the view namespace |
|
87
|
|
|
* |
|
88
|
|
|
* @return string The view namespace |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getNamespace(): string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->sNamespace; |
|
93
|
|
|
} |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the view name |
|
97
|
|
|
* |
|
98
|
|
|
* @return string The view name |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getViewName(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->sViewName; |
|
103
|
|
|
} |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get the view data |
|
107
|
|
|
* |
|
108
|
|
|
* @return array The view data |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getViewData(): array |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->aViewData; |
|
113
|
|
|
} |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Render a view using third party view system |
|
117
|
|
|
* |
|
118
|
|
|
* @return string The string representation of the view |
|
119
|
|
|
*/ |
|
120
|
|
|
public function __toString() |
|
121
|
|
|
{ |
|
122
|
|
|
return ($this->xRenderer) ? $this->xRenderer->render($this) : ''; |
|
123
|
|
|
} |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Convert this object to string for json. |
|
127
|
|
|
* |
|
128
|
|
|
* This is a method of the JsonSerializable interface. |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function jsonSerialize(): string |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->__toString(); |
|
135
|
|
|
} |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|