1 | <?php |
||
19 | class SerializableObject implements ArrayAccess, JsonSerializable |
||
20 | { |
||
21 | /** |
||
22 | * Create the object from an array |
||
23 | * |
||
24 | * @param array $array The array of object properties |
||
25 | */ |
||
26 | public function __construct($array = false) |
||
43 | |||
44 | /** |
||
45 | * Serialize the object into a format consumable by json_encode |
||
46 | * |
||
47 | * @return array The object in a more serialized format |
||
48 | */ |
||
49 | public function jsonSerialize() |
||
53 | |||
54 | /** |
||
55 | * Convert the object into an XML string |
||
56 | * |
||
57 | * @return string The XML format of the object |
||
58 | */ |
||
59 | public function xmlSerialize() |
||
60 | { |
||
61 | $xml = new XmlWriter(); |
||
62 | $xml->openMemory(); |
||
63 | $xml->startDocument('1.0'); |
||
64 | if(version_compare(PHP_VERSION, '7.0.0', '>=')) |
||
65 | { |
||
66 | $this->php7XmlSerialize($xml); |
||
67 | } |
||
68 | else |
||
69 | { |
||
70 | $this->oldPhpSerialize($xml); |
||
71 | } |
||
72 | $xml->endElement(); |
||
73 | return $xml->outputMemory(true); |
||
74 | } |
||
75 | |||
76 | private function php7XmlSerialize(XMLWriter $xml) |
||
89 | |||
90 | private function oldPhpSerialize(XMLWriter $xml) |
||
109 | |||
110 | /** |
||
111 | * Convert an object to XML without document tags |
||
112 | * |
||
113 | * @param XmlWriter $xml The XMLWriter to write the object to |
||
114 | * @param mixed $data The data to serialze to XML |
||
115 | */ |
||
116 | private function object2XML(XMLWriter $xml, $data) |
||
144 | |||
145 | /** |
||
146 | * Determine if an array has any string keys |
||
147 | * |
||
148 | * @param array $array The array to test |
||
149 | * |
||
150 | * @return boolean True if the array has string keys, false otherwise |
||
151 | */ |
||
152 | private function arrayHasStringKeys(array $array) |
||
156 | |||
157 | /** |
||
158 | * Convert an array to XML without document tags |
||
159 | * |
||
160 | * @param XmlWriter $xml The XMLWriter to write the object to |
||
161 | * @param string $keyParent The key of the parent object |
||
162 | * @param mixed $data The data to serialze to XML |
||
163 | */ |
||
164 | private function array2XML(XMLWriter $xml, $keyParent, $data) |
||
195 | |||
196 | /** |
||
197 | * Convert json back to an object |
||
198 | * |
||
199 | * @param string $json The JSON string to deserialize back into an object |
||
200 | * |
||
201 | * @return SerializableObject The object the json deserializes into |
||
202 | */ |
||
203 | public static function jsonDeserialize($json) |
||
208 | |||
209 | /** |
||
210 | * Convert the object to a serizlized string |
||
211 | * |
||
212 | * @param string $fmt The format to serialize into |
||
213 | * @param array|false $select Which fields to include |
||
214 | * |
||
215 | * @return string The object in string format |
||
216 | */ |
||
217 | public function serializeObject($fmt = 'json', $select = false) |
||
237 | |||
238 | /** |
||
239 | * Function to allow the caller to set a value in the object via object[offset] = value |
||
240 | * |
||
241 | * @param string $offset The key to set |
||
242 | * @param mixed $value The value for the key |
||
243 | */ |
||
244 | public function offsetSet($offset, $value) |
||
248 | |||
249 | /** |
||
250 | * Function to allow the caller to determin if a value in the object is set |
||
251 | * |
||
252 | * @param string $offset The key to determine if it has a value |
||
253 | * |
||
254 | * @return boolean Does the key have a value? |
||
255 | */ |
||
256 | public function offsetExists($offset) |
||
260 | |||
261 | /** |
||
262 | * Function to allow the caller to delete the value in the object for a key |
||
263 | * |
||
264 | * @param string $offset The key to unset |
||
265 | */ |
||
266 | public function offsetUnset($offset) |
||
270 | |||
271 | /** |
||
272 | * Function to allow the caller to obtain the value for a key |
||
273 | * |
||
274 | * @param string $offset The key to return the value for |
||
275 | * |
||
276 | * @return mixed the value in the key |
||
277 | */ |
||
278 | public function offsetGet($offset) |
||
282 | } |
||
283 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
284 |