Debian Codegraph @

vendor/clue/redis-protocol/src/Clue/Redis/Protocol/Serializer/RecursiveSerializer.php

raw
1 <?php
2
3 namespace Clue\Redis\Protocol\Serializer;
4
5 use Clue\Redis\Protocol\Model\StatusReply;
6 use InvalidArgumentException;
7 use Exception;
8 use Clue\Redis\Protocol\Model\BulkReply;
9 use Clue\Redis\Protocol\Model\IntegerReply;
10 use Clue\Redis\Protocol\Model\ErrorReply;
11 use Clue\Redis\Protocol\Model\MultiBulkReply;
12 use Clue\Redis\Protocol\Model\ModelInterface;
13 use Clue\Redis\Protocol\Model\Request;
14
15 class RecursiveSerializer implements SerializerInterface
16 {
17 const CRLF = "\r\n";
18
19 public function getRequestMessage($command, array $args = array())
20 {
21 $data = '*' . (count($args) + 1) . "\r\n$" . strlen($command) . "\r\n" . $command . "\r\n";
22 foreach ($args as $arg) {
23 $data .= '$' . strlen($arg) . "\r\n" . $arg . "\r\n";
24 }
25 return $data;
26 }
27
28 public function createRequestModel($command, array $args = array())
29 {
30 return new Request($command, $args);
31 }
32
33 public function getReplyMessage($data)
34 {
35 if (is_string($data) || $data === null) {
36 return $this->getBulkMessage($data);
37 } else if (is_int($data) || is_float($data) || is_bool($data)) {
38 return $this->getIntegerMessage($data);
39 } else if ($data instanceof Exception) {
40 return $this->getErrorMessage($data->getMessage());
41 } else if (is_array($data)) {
42 return $this->getMultiBulkMessage($data);
43 } else {
44 throw new InvalidArgumentException('Invalid data type passed for serialization');
45 }
46 }
47
48 public function createReplyModel($data)
49 {
50 if (is_string($data) || $data === null) {
51 return new BulkReply($data);
52 } else if (is_int($data) || is_float($data) || is_bool($data)) {
53 return new IntegerReply($data);
54 } else if ($data instanceof Exception) {
55 return new ErrorReply($data->getMessage());
56 } else if (is_array($data)) {
57 $models = array();
58 foreach ($data as $one) {
59 $models []= $this->createReplyModel($one);
60 }
61 return new MultiBulkReply($models);
62 } else {
63 throw new InvalidArgumentException('Invalid data type passed for serialization');
64 }
65 }
66
67 public function getBulkMessage($data)
68 {
69 if ($data === null) {
70 /* null bulk reply */
71 return '$-1' . self::CRLF;
72 }
73 /* bulk reply */
74 return '$' . strlen($data) . self::CRLF . $data . self::CRLF;
75 }
76
77 public function getErrorMessage($data)
78 {
79 /* error status reply */
80 return '-' . $data . self::CRLF;
81 }
82
83 public function getIntegerMessage($data)
84 {
85 return ':' . (int)$data . self::CRLF;
86 }
87
88 public function getMultiBulkMessage($data)
89 {
90 if ($data === null) {
91 /* null multi bulk reply */
92 return '*-1' . self::CRLF;
93 }
94 /* multi bulk reply */
95 $ret = '*' . count($data) . self::CRLF;
96 foreach ($data as $one) {
97 if ($one instanceof ModelInterface) {
98 $ret .= $one->getMessageSerialized($this);
99 } else {
100 $ret .= $this->getReplyMessage($one);
101 }
102 }
103 return $ret;
104 }
105
106 public function getStatusMessage($data)
107 {
108 /* status reply */
109 return '+' . $data . self::CRLF;
110 }
111 }