1 | <?php |
||
20 | class SerializableObject implements \ArrayAccess, \JsonSerializable |
||
21 | { |
||
22 | /** |
||
23 | * Create the object from an array |
||
24 | * |
||
25 | * @param boolean|array $array The array of object properties |
||
26 | */ |
||
27 | public function __construct($array = false) |
||
44 | |||
45 | /** |
||
46 | * Serialize the object into a format consumable by json_encode |
||
47 | * |
||
48 | * @return array The object in a more serialized format |
||
49 | */ |
||
50 | public function jsonSerialize() |
||
54 | |||
55 | /** |
||
56 | * Convert the object into an XML string |
||
57 | * |
||
58 | * @return string The XML format of the object |
||
59 | */ |
||
60 | public function xmlSerialize() |
||
69 | |||
70 | /** |
||
71 | * Convert the object into an XML string for PHP 7 or greater |
||
72 | * |
||
73 | * @param XmlWriter $xml The XmlWriter Instancce to use in serializing |
||
74 | * |
||
75 | * @return string The XML format of the object |
||
76 | */ |
||
77 | private function php7XmlSerialize(\XMLWriter $xml) |
||
90 | |||
91 | /** |
||
92 | * Convert an object to XML without document tags |
||
93 | * |
||
94 | * @param XmlWriter $xml The XMLWriter to write the object to |
||
95 | * @param mixed $data The data to serialze to XML |
||
96 | */ |
||
97 | private function object2XML(\XMLWriter $xml, $data) |
||
125 | |||
126 | /** |
||
127 | * Determine if an array has any string keys |
||
128 | * |
||
129 | * @param array $array The array to test |
||
130 | * |
||
131 | * @return boolean True if the array has string keys, false otherwise |
||
132 | */ |
||
133 | private function arrayHasStringKeys(array $array) |
||
137 | |||
138 | /** |
||
139 | * Convert an array to XML without document tags |
||
140 | * |
||
141 | * @param XmlWriter $xml The XMLWriter to write the object to |
||
142 | * @param string $keyParent The key of the parent object |
||
143 | * @param mixed $data The data to serialze to XML |
||
144 | */ |
||
145 | private function array2XML(\XMLWriter $xml, $keyParent, $data) |
||
176 | |||
177 | /** |
||
178 | * Convert json back to an object |
||
179 | * |
||
180 | * @param string $json The JSON string to deserialize back into an object |
||
181 | * |
||
182 | * @return SerializableObject The object the json deserializes into |
||
183 | */ |
||
184 | public static function jsonDeserialize($json) |
||
189 | |||
190 | /** |
||
191 | * Convert the object to a serizlized string |
||
192 | * |
||
193 | * @param string $fmt The format to serialize into |
||
194 | * @param array|false $select Which fields to include |
||
195 | * |
||
196 | * @return string The object in string format |
||
197 | */ |
||
198 | public function serializeObject($fmt = 'json', $select = false) |
||
218 | |||
219 | /** |
||
220 | * Function to allow the caller to set a value in the object via object[offset] = value |
||
221 | * |
||
222 | * @param string $offset The key to set |
||
223 | * @param mixed $value The value for the key |
||
224 | */ |
||
225 | public function offsetSet($offset, $value) |
||
229 | |||
230 | /** |
||
231 | * Function to allow the caller to determin if a value in the object is set |
||
232 | * |
||
233 | * @param string $offset The key to determine if it has a value |
||
234 | * |
||
235 | * @return boolean Does the key have a value? |
||
236 | */ |
||
237 | public function offsetExists($offset) |
||
241 | |||
242 | /** |
||
243 | * Function to allow the caller to delete the value in the object for a key |
||
244 | * |
||
245 | * @param string $offset The key to unset |
||
246 | */ |
||
247 | public function offsetUnset($offset) |
||
251 | |||
252 | /** |
||
253 | * Function to allow the caller to obtain the value for a key |
||
254 | * |
||
255 | * @param string $offset The key to return the value for |
||
256 | * |
||
257 | * @return mixed the value in the key |
||
258 | */ |
||
259 | public function offsetGet($offset) |
||
263 | } |
||
264 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
265 |