1: <?php
2: namespace Ratchet\Session\Serialize;
3:
4: class PhpHandler implements HandlerInterface {
5: 6: 7: 8:
9: function serialize(array $data) {
10: $preSerialized = array();
11: $serialized = '';
12:
13: if (count($data)) {
14: foreach ($data as $bucket => $bucketData) {
15: $preSerialized[] = $bucket . '|' . serialize($bucketData);
16: }
17: $serialized = implode('', $preSerialized);
18: }
19:
20: return $serialized;
21: }
22:
23: 24: 25: 26: 27:
28: public function unserialize($raw) {
29: $returnData = array();
30: $offset = 0;
31:
32: while ($offset < strlen($raw)) {
33: if (!strstr(substr($raw, $offset), "|")) {
34: throw new \UnexpectedValueException("invalid data, remaining: " . substr($raw, $offset));
35: }
36:
37: $pos = strpos($raw, "|", $offset);
38: $num = $pos - $offset;
39: $varname = substr($raw, $offset, $num);
40: $offset += $num + 1;
41: $data = unserialize(substr($raw, $offset));
42:
43: $returnData[$varname] = $data;
44: $offset += strlen(serialize($data));
45: }
46:
47: return $returnData;
48: }
49: }
50: