Debian Codegraph @

vendor/clue/redis-protocol/src/Serializer/RecursiveSerializer.php

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