Debian Codegraph @

vendor/clue/redis-protocol/src/Clue/Redis/Protocol/Model/MultiBulkReply.php

raw
1 <?php
2
3 namespace Clue\Redis\Protocol\Model;
4
5 use InvalidArgumentException;
6 use UnexpectedValueException;
7 use Clue\Redis\Protocol\Serializer\SerializerInterface;
8
9 class MultiBulkReply implements ModelInterface
10 {
11 /**
12 * @var array|null
13 */
14 private $data;
15
16 /**
17 * create multi bulk reply (an array of other replies, usually bulk replies)
18 *
19 * @param array|null $data
20 * @throws InvalidArgumentException
21 */
22 public function __construct(array $data = null)
23 {
24 $this->data = $data;
25 }
26
27 public function getValueNative()
28 {
29 if ($this->data === null) {
30 return null;
31 }
32
33 $ret = array();
34 foreach ($this->data as $one) {
35 if ($one instanceof ModelInterface) {
36 $ret []= $one->getValueNative();
37 } else {
38 $ret []= $one;
39 }
40 }
41 return $ret;
42 }
43
44 public function getMessageSerialized(SerializerInterface $serializer)
45 {
46 return $serializer->getMultiBulkMessage($this->data);
47 }
48
49 /**
50 * Checks whether this model represents a valid unified request protocol message
51 *
52 * The new unified protocol was introduced in Redis 1.2, but it became the
53 * standard way for talking with the Redis server in Redis 2.0. The unified
54 * request protocol is what Redis already uses in replies in order to send
55 * list of items to clients, and is called a Multi Bulk Reply.
56 *
57 * @return boolean
58 * @link http://redis.io/topics/protocol
59 */
60 public function isRequest()
61 {
62 if (!$this->data) {
63 return false;
64 }
65
66 foreach ($this->data as $one) {
67 if (!($one instanceof BulkReply) && !is_string($one)) {
68 return false;
69 }
70 }
71
72 return true;
73 }
74
75 public function getRequestModel()
76 {
77 if (!$this->data) {
78 throw new UnexpectedValueException('Null-multi-bulk message can not be represented as a request, must contain string/bulk values');
79 }
80
81 $command = null;
82 $args = array();
83
84 foreach ($this->data as $one) {
85 if ($one instanceof BulkReply) {
86 $one = $one->getValueNative();
87 } elseif (!is_string($one)) {
88 throw new UnexpectedValueException('Message can not be represented as a request, must only contain string/bulk values');
89 }
90
91 if ($command === null) {
92 $command = $one;
93 } else {
94 $args []= $one;
95 }
96 }
97
98 return new Request($command, $args);
99 }
100 }