2020 * Class KVTree
2121 * @package DCarbone\PHPConsulAPI\KV
2222 */
23- class KVTree implements \RecursiveIterator, \Countable, \JsonSerializable, \ArrayAccess
23+ class KVTree implements \RecursiveIterator, \Countable, \JsonSerializable, \ArrayAccess, \Serializable
2424{
2525 /** @var string */
2626 private $ _prefix ;
@@ -126,26 +126,6 @@ public function count()
126126 return count ($ this ->_children );
127127 }
128128
129- /**
130- * Specify data which should be serialized to JSON
131- * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
132- * @return array data which can be serialized by json_encode,which is a value of any type other than a resource.
133- */
134- public function jsonSerialize ()
135- {
136- $ json = array ($ this ->_prefix => array ());
137- foreach ($ this ->_children as $ k =>$ child )
138- {
139- if ($ child instanceof KVTree)
140- $ json [$ this ->_prefix ] = $ child ;
141- else if ($ child instanceof KVPair)
142- $ json [$ this ->_prefix ][$ child ->Key ] = $ child ;
143- else
144- $ json [$ this ->_prefix ][$ k ] = $ child ;
145- }
146- return $ json ;
147- }
148-
149129 /**
150130 * Whether a offset exists
151131 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
@@ -249,4 +229,47 @@ public function offsetUnset($offset)
249229 {
250230 // do nothing, yo...
251231 }
232+
233+ /**
234+ * String representation of object
235+ * @link http://php.net/manual/en/serializable.serialize.php
236+ * @return string the string representation of the object or null
237+ */
238+ public function serialize ()
239+ {
240+ return serialize ([$ this ->_prefix , $ this ->_children ]);
241+ }
242+
243+ /**
244+ * Constructs the object
245+ * @link http://php.net/manual/en/serializable.unserialize.php
246+ * @param string $serialized The string representation of the object.
247+ * @return void
248+ */
249+ public function unserialize ($ serialized )
250+ {
251+ $ data = unserialize ($ serialized );
252+ $ this ->_prefix = $ data [0 ];
253+ $ this ->_children = $ data [1 ];
254+ }
255+
256+ /**
257+ * Specify data which should be serialized to JSON
258+ * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
259+ * @return array data which can be serialized by json_encode,which is a value of any type other than a resource.
260+ */
261+ public function jsonSerialize ()
262+ {
263+ $ json = array ($ this ->_prefix => array ());
264+ foreach ($ this ->_children as $ k =>$ child )
265+ {
266+ if ($ child instanceof KVTree)
267+ $ json [$ this ->_prefix ] = $ child ;
268+ else if ($ child instanceof KVPair)
269+ $ json [$ this ->_prefix ][$ child ->Key ] = $ child ;
270+ else
271+ $ json [$ this ->_prefix ][$ k ] = $ child ;
272+ }
273+ return $ json ;
274+ }
252275}
0 commit comments