1 | <?php |
||
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 = []) |
||
53 | |||
54 | /** |
||
55 | * @see "http://php.net/manual/en/language.oop5.magic.php#object.tostring" |
||
56 | * @return string |
||
57 | */ |
||
58 | public function __toString() |
||
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) |
||
72 | |||
73 | /** |
||
74 | * @see "http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members" |
||
75 | */ |
||
76 | public function __set($name, $value) |
||
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) |
||
89 | |||
90 | /** |
||
91 | * @see "http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members" |
||
92 | */ |
||
93 | public function __unset($name) |
||
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) |
||
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) |
||
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) |
||
126 | |||
127 | /** |
||
128 | * Unsets an offset. |
||
129 | * @see "http://php.net/manual/en/arrayaccess.offsetunset.php" |
||
130 | */ |
||
131 | public function offsetUnset($offset) |
||
135 | |||
136 | /** |
||
137 | * @see "http://php.net/manual/en/class.iteratoraggregate.php" |
||
138 | * @return \ArrayIterator |
||
139 | */ |
||
140 | public function getIterator() |
||
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) |
||
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) |
||
173 | |||
174 | /** |
||
175 | * Get the content of the document |
||
176 | * @return string Content of the document |
||
177 | */ |
||
178 | public function getContent() |
||
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) |
||
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) |
||
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) |
||
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) |
||
257 | } |
||
258 |