-
Notifications
You must be signed in to change notification settings - Fork 23
/
SerializableObject.php
264 lines (251 loc) · 7.02 KB
/
SerializableObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace Flipside;
/**
* An easily serializable class
*
* This file describes a serializable object
*
* PHP version 5 and 7
*
* @author Patrick Boyd / problem@burningflipside.com
* @copyright Copyright (c) 2015, Austin Artistic Reconstruction
* @license http://www.apache.org/licenses/ Apache 2.0 License
*/
/**
* An object that can be serialized and accessed as an array.
*
* This class can be serialized to various formats
*/
class SerializableObject implements \ArrayAccess, \JsonSerializable
{
/**
* Create the object from an array
*
* @param boolean|array $array The array of object properties
*/
public function __construct($array = false)
{
if($array !== false)
{
if(is_object($array))
{
$array = get_object_vars($array);
}
if(is_array($array))
{
foreach($array as $key => $value)
{
$this->{$key} = $value;
}
}
}
}
/**
* Serialize the object into a format consumable by json_encode
*
* @return array The object in a more serialized format
*/
public function jsonSerialize()
{
return (array)$this;
}
/**
* Convert the object into an XML string
*
* @return string The XML format of the object
*/
public function xmlSerialize()
{
$xml = new \XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0');
$this->php7XmlSerialize($xml);
$xml->endElement();
return $xml->outputMemory(true);
}
/**
* Convert the object into an XML string for PHP 7 or greater
*
* @param XmlWriter $xml The XmlWriter Instancce to use in serializing
*
* @return string The XML format of the object
*/
private function php7XmlSerialize(\XMLWriter $xml)
{
if(isset($this[0]))
{
$xml->startElement('Array');
$this->array2XML($xml, 'Entity', get_object_vars($this));
$xml->endElement();
}
else
{
$this->object2XML($xml, $this);
}
}
/**
* Convert an object to XML without document tags
*
* @param XmlWriter $xml The XMLWriter to write the object to
* @param mixed $data The data to serialze to XML
*/
private function object2XML(\XMLWriter $xml, $data)
{
foreach($data as $key => $value)
{
if(is_array($value) || is_numeric($key))
{
$this->array2XML($xml, $key, (array)$value);
}
else if(is_object($value))
{
$xml->startElement($key);
$this->object2XML($xml, $value);
$xml->endElement();
}
else
{
if($key[0] === '$')
{
$xml->writeElement(substr($key, 1), $value);
}
else
{
$key = strtr($key, array(' '=>'', ','=>''));
$xml->writeElement($key, $value);
}
}
}
}
/**
* Determine if an array has any string keys
*
* @param array $array The array to test
*
* @return boolean True if the array has string keys, false otherwise
*/
private function arrayHasStringKeys(array $array)
{
return count(array_filter(array_keys($array), 'is_string')) > 0;
}
/**
* Convert an array to XML without document tags
*
* @param XmlWriter $xml The XMLWriter to write the object to
* @param string $keyParent The key of the parent object
* @param mixed $data The data to serialze to XML
*/
private function array2XML(\XMLWriter $xml, $keyParent, $data)
{
$data = array_values($data);
$count = count($data);
for($i = 0; $i < $count; $i++)
{
$value = $data[$i];
if(is_array($value) && isset($value[0]))
{
$xml->startElement($keyParent);
$this->array2XML($xml, 'Child', $value);
$xml->endElement();
}
else if(is_array($value) && $this->arrayHasStringKeys($value))
{
$xml->startElement($keyParent);
$this->object2XML($xml, $value);
$xml->endElement();
}
else if(is_object($value))
{
$xml->startElement($keyParent);
$this->object2XML($xml, $value);
$xml->endElement();
}
else
{
$xml->writeElement($keyParent, $value);
}
}
}
/**
* Convert json back to an object
*
* @param string $json The JSON string to deserialize back into an object
*
* @return SerializableObject The object the json deserializes into
*/
public static function jsonDeserialize($json)
{
$array = json_decode($json, true);
return new self($array);
}
/**
* Convert the object to a serizlized string
*
* @param string $fmt The format to serialize into
* @param array|false $select Which fields to include
*
* @return string The object in string format
*/
public function serializeObject($fmt = 'json', $select = false)
{
$copy = $this;
if($select !== false)
{
$copy = new self();
$count = count($select);
for($i = 0; $i < $count; $i++)
{
$copy->{$select[$i]} = $this->offsetGet($select[$i]);
}
}
switch($fmt)
{
case 'json':
return json_encode($copy);
default:
throw new \Exception('Unsupported fmt '.$fmt);
}
}
/**
* Function to allow the caller to set a value in the object via object[offset] = value
*
* @param string $offset The key to set
* @param mixed $value The value for the key
*/
public function offsetSet($offset, $value)
{
$this->{$offset} = $value;
}
/**
* Function to allow the caller to determin if a value in the object is set
*
* @param string $offset The key to determine if it has a value
*
* @return boolean Does the key have a value?
*/
public function offsetExists($offset)
{
return isset($this->{$offset});
}
/**
* Function to allow the caller to delete the value in the object for a key
*
* @param string $offset The key to unset
*/
public function offsetUnset($offset)
{
unset($this->{$offset});
}
/**
* Function to allow the caller to obtain the value for a key
*
* @param string $offset The key to return the value for
*
* @return mixed the value in the key
*/
public function offsetGet($offset)
{
return $this->{$offset};
}
}
/* vim: set tabstop=4 shiftwidth=4 expandtab: */