1
|
|
|
<?php |
2
|
|
|
namespace VKBansal\FrontMatter; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* FrontMatter Document |
6
|
|
|
* @package VKBansal\FrontMatter\Document |
7
|
|
|
* @version 1.3.0 |
8
|
|
|
* @author Vivek Kumar Bansal <[email protected]> |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
class Document implements \ArrayAccess, \IteratorAggregate |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Constants for Document::merge() behaviour |
15
|
|
|
*/ |
16
|
|
|
const MERGE_CONFIG = 0; |
17
|
|
|
const MERGE_CONTENT_REPLACE = 1; |
18
|
|
|
const MERGE_CONTENT_APPEND = 2; |
19
|
|
|
const MERGE_ALL_REPLACE = 3; |
20
|
|
|
const MERGE_ALL_APPEND = 4; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constants for Document::inherit() behaviour |
24
|
|
|
*/ |
25
|
|
|
const INHERIT_CONFIG = 5; |
26
|
|
|
const INHERIT_CONTENT_REPLACE = 6; |
27
|
|
|
const INHERIT_CONTENT_APPEND = 7; |
28
|
|
|
const INHERIT_ALL_REPLACE = 8; |
29
|
|
|
const INHERIT_ALL_APPEND = 9; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Content of the document |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $content; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Config of the document |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $config; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Document Constructor |
45
|
|
|
* @param string $content content/body of the document |
46
|
|
|
* @param array $config config/header of the document |
47
|
|
|
*/ |
48
|
|
|
public function __construct($content = '', $config = []) |
49
|
|
|
{ |
50
|
|
|
$this->content = $content; |
51
|
|
|
$this->config = $config; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @see "http://php.net/manual/en/language.oop5.magic.php#object.tostring" |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function __toString() |
59
|
|
|
{ |
60
|
|
|
return $this->getContent(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @see "http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members" |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function __get($name) |
69
|
|
|
{ |
70
|
|
|
return $this->config[$name]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @see "http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members" |
75
|
|
|
*/ |
76
|
|
|
public function __set($name, $value) |
77
|
|
|
{ |
78
|
|
|
$this->config[$name] = $value; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @see "http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members" |
83
|
|
|
* @return boolean |
84
|
|
|
*/ |
85
|
|
|
public function __isset($name) |
86
|
|
|
{ |
87
|
|
|
return isset($this->config[$name]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @see "http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members" |
92
|
|
|
*/ |
93
|
|
|
public function __unset($name) |
94
|
|
|
{ |
95
|
|
|
unset($this->config[$name]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Whether or not an offset exists. |
100
|
|
|
* This method is executed when using isset() or empty() on objects implementing ArrayAccess. |
101
|
|
|
* @see "http://php.net/manual/en/arrayaccess.offsetexists.php" |
102
|
|
|
*/ |
103
|
|
|
public function offsetExists($offset) |
104
|
|
|
{ |
105
|
|
|
return isset($this->config[$offset]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the value at specified offset. |
110
|
|
|
* This method is executed when checking if offset is empty(). |
111
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetget.php |
112
|
|
|
*/ |
113
|
|
|
public function offsetGet($offset) |
114
|
|
|
{ |
115
|
|
|
return $this->config[$offset]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Assigns a value to the specified offset. |
120
|
|
|
* @see "http://php.net/manual/en/arrayaccess.offsetset.php" |
121
|
|
|
*/ |
122
|
|
|
public function offsetSet($offset, $value) |
123
|
|
|
{ |
124
|
|
|
$this->config[$offset] = $value; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Unsets an offset. |
129
|
|
|
* @see "http://php.net/manual/en/arrayaccess.offsetunset.php" |
130
|
|
|
*/ |
131
|
|
|
public function offsetUnset($offset) |
132
|
|
|
{ |
133
|
|
|
unset($this->config[$offset]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @see "http://php.net/manual/en/class.iteratoraggregate.php" |
138
|
|
|
* @return \ArrayIterator |
139
|
|
|
*/ |
140
|
|
|
public function getIterator() |
141
|
|
|
{ |
142
|
|
|
return new \ArrayIterator($this->config); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get header/config of the document |
147
|
|
|
* @param mixed $varName Name of the property to get |
148
|
|
|
* @return mixed if name is specified, returns specific property else returns full config/header |
149
|
|
|
*/ |
150
|
|
|
public function getConfig($varName = null) |
151
|
|
|
{ |
152
|
|
|
if ($varName !== null && array_key_exists($varName, $this->config)) { |
153
|
|
|
return $this->config[$varName]; |
154
|
|
|
} |
155
|
|
|
return $this->config; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set header/config of the document |
160
|
|
|
* @param mixed $property If an array is provided, header is replaced. If a string is provided, the poperty is replaced/set. |
161
|
|
|
* @param mixed $value Value of the property to set |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
public function setConfig($property, $value = null) |
165
|
|
|
{ |
166
|
|
|
if (is_array($property)) { |
167
|
|
|
$this->config = $property; |
168
|
|
|
} elseif (is_string($property)) { |
169
|
|
|
$this->config[$property] = $value; |
170
|
|
|
} |
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the content of the document |
176
|
|
|
* @return string Content of the document |
177
|
|
|
*/ |
178
|
|
|
public function getContent() |
179
|
|
|
{ |
180
|
|
|
return $this->content; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set the content of the document |
185
|
|
|
* @param string $content Content of the document |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
public function setContent($content) |
189
|
|
|
{ |
190
|
|
|
$this->content = $content; |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Inherit from parent document |
196
|
|
|
* @param Document $parent Document to be inherited |
197
|
|
|
* @param int $mode Inherit Mode |
198
|
|
|
* @return $this |
199
|
|
|
*/ |
200
|
|
|
public function inherit(Document $parent, $mode = self::INHERIT_CONFIG) |
201
|
|
|
{ |
202
|
|
|
if (in_array($mode, [self::INHERIT_CONFIG, self::INHERIT_ALL_APPEND, self::INHERIT_ALL_REPLACE])) { |
203
|
|
|
$this->config = $this->mergeRecursive($parent->getConfig(), $this->config); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if (in_array($mode, [self::INHERIT_ALL_REPLACE, self::INHERIT_CONTENT_REPLACE])) { |
207
|
|
|
$this->content = $parent->getContent(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if (in_array($mode, [self::INHERIT_ALL_APPEND, self::INHERIT_CONTENT_APPEND])) { |
211
|
|
|
$this->content = $parent->getContent().$this->content; |
212
|
|
|
} |
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Merge current document with given document |
218
|
|
|
* @param Document $document Document to be merged in |
219
|
|
|
* @param int $mode Merge mode |
220
|
|
|
* @return $this |
221
|
|
|
*/ |
222
|
|
|
public function merge(Document $document, $mode = self::MERGE_CONFIG) |
223
|
|
|
{ |
224
|
|
|
if (in_array($mode, [self::MERGE_CONFIG, self::MERGE_ALL_APPEND, self::MERGE_ALL_REPLACE])) { |
225
|
|
|
$this->config = $this->mergeRecursive($this->config, $document->getConfig()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if (in_array($mode, [self::MERGE_ALL_REPLACE, self::MERGE_CONTENT_REPLACE])) { |
229
|
|
|
$this->content = $document->getContent(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if (in_array($mode, [self::MERGE_ALL_APPEND, self::MERGE_CONTENT_APPEND])) { |
233
|
|
|
$this->content .= $document->getContent(); |
234
|
|
|
} |
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Recursively merges second array into first |
240
|
|
|
* @param array $itemA Array to be merged in |
241
|
|
|
* @param array $itemB Array to be merged |
242
|
|
|
* @return array merged array |
243
|
|
|
*/ |
244
|
|
|
private function mergeRecursive($itemA, $itemB) |
245
|
|
|
{ |
246
|
|
|
foreach ($itemB as $key => $value) { |
247
|
|
|
if (is_integer($key)) { |
248
|
|
|
$itemA[] = $value; |
249
|
|
|
} elseif (array_key_exists($key, $itemA) && is_array($itemA[$key]) && is_array($value)) { |
250
|
|
|
$itemA[$key] = $this->mergeRecursive($itemA[$key], $value); |
251
|
|
|
} else { |
252
|
|
|
$itemA[$key] = $value; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
return $itemA; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|