1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace brendt\stitcher\site; |
4
|
|
|
|
5
|
|
|
use brendt\stitcher\exception\TemplateNotFoundException; |
6
|
|
|
|
7
|
|
|
class Page { |
8
|
|
|
|
9
|
|
|
/** @var string */ |
10
|
|
|
protected $id; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
protected $template; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
protected $variables = []; |
17
|
|
|
|
18
|
|
|
/** @var Adapter[] */ |
19
|
|
|
protected $adapters; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
protected $parsedFields = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Page constructor. |
26
|
|
|
* |
27
|
|
|
* @param $id |
28
|
|
|
* @param $data |
29
|
|
|
* |
30
|
|
|
* @throws TemplateNotFoundException |
31
|
|
|
*/ |
32
|
|
|
public function __construct($id, $data) { |
33
|
|
|
if (!isset($data['template'])) { |
34
|
|
|
throw new TemplateNotFoundException("No template was set for page {$id}"); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->id = $id; |
38
|
|
|
$this->template = $data['template']; |
39
|
|
|
|
40
|
|
|
if (isset($data['data'])) { |
41
|
|
|
$this->variables = $data['data']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (isset($data['adapters'])) { |
45
|
|
|
foreach ($data['adapters'] as $type => $adapterConfig) { |
46
|
|
|
$this->adapters[$type] = $adapterConfig; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getTemplate() { |
55
|
|
|
return $this->template; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getId() { |
62
|
|
|
return $this->id; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getVariables() { |
69
|
|
|
return $this->variables; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return Adapter[] |
74
|
|
|
*/ |
75
|
|
|
public function getAdapters() { |
76
|
|
|
return $this->adapters; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $name |
81
|
|
|
* |
82
|
|
|
* @return Adapter|null |
83
|
|
|
*/ |
84
|
|
|
public function getAdapter($name) { |
85
|
|
|
if (!isset($this->adapters[$name])) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->adapters[$name]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $name |
94
|
|
|
* |
95
|
|
|
* @return mixed|null |
96
|
|
|
*/ |
97
|
|
|
public function getVariable($name) { |
98
|
|
|
if (!isset($this->variables[$name])) { |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->variables[$name]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $name |
107
|
|
|
* @param $data |
108
|
|
|
* |
109
|
|
|
* @return Page |
110
|
|
|
*/ |
111
|
|
|
public function setVariable($name, $data) { |
112
|
|
|
$this->variables[$name] = $data; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $name |
119
|
|
|
* |
120
|
|
|
* @return Page |
121
|
|
|
*/ |
122
|
|
|
public function clearAdapter($name) { |
123
|
|
|
if (isset($this->adapters[$name])) { |
124
|
|
|
unset($this->adapters[$name]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $id |
132
|
|
|
* |
133
|
|
|
* @return Page |
134
|
|
|
*/ |
135
|
|
|
public function setId($id) { |
136
|
|
|
$this->id = $id; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $name |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isParsedField($name) { |
147
|
|
|
return isset($this->parsedFields[$name]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $name |
152
|
|
|
* |
153
|
|
|
* @return Page |
154
|
|
|
*/ |
155
|
|
|
public function setParsedField($name) { |
156
|
|
|
$this->parsedFields[$name] = true; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|