1 | <?php |
||
32 | class PageIndexerResponse |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * Unique request ID. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $requestId = null; |
||
41 | |||
42 | /** |
||
43 | * The actions' results as action => result pairs. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $results = array(); |
||
48 | |||
49 | /** |
||
50 | * Turns a JSON encoded result string back into its PHP representation. |
||
51 | * |
||
52 | * @param string $jsonEncodedResponse JSON encoded result string |
||
53 | * @return array|bool An array of action => result pairs or FALSE if the response could not be decoded |
||
54 | */ |
||
55 | 5 | public static function getResultsFromJson($jsonEncodedResponse) |
|
56 | { |
||
57 | 5 | $responseData = json_decode($jsonEncodedResponse, true); |
|
58 | |||
59 | 5 | if (is_array($responseData['actionResults'])) { |
|
60 | 4 | foreach ($responseData['actionResults'] as $action => $serializedActionResult) { |
|
61 | 4 | $responseData['actionResults'][$action] = unserialize($serializedActionResult); |
|
62 | } |
||
63 | 1 | } elseif (is_null($responseData)) { |
|
64 | 1 | $responseData = false; |
|
65 | } |
||
66 | |||
67 | 5 | return $responseData; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Adds an action's result. |
||
72 | * |
||
73 | * @param string $action The action name. |
||
74 | * @param mixed $result The action's result. |
||
75 | * @throws \RuntimeException if $action is null |
||
76 | */ |
||
77 | 5 | public function addActionResult($action, $result) |
|
88 | |||
89 | /** |
||
90 | * Gets the complete set of results or a specific action's results. |
||
91 | * |
||
92 | * @param string $action Optional action name. |
||
93 | * @return array |
||
94 | */ |
||
95 | 4 | public function getActionResult($action = null) |
|
96 | { |
||
97 | 4 | $result = $this->results; |
|
98 | |||
99 | 4 | if (!empty($action)) { |
|
100 | 3 | $result = $this->results[$action]; |
|
101 | } |
||
102 | |||
103 | 4 | return $result; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * Sends the response's headers. |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | public function sendHeaders() |
||
120 | |||
121 | /** |
||
122 | * Compiles the response's content so that it can be sent back to the |
||
123 | * Index Queue page indexer. |
||
124 | * |
||
125 | * @return string The response content |
||
126 | */ |
||
127 | public function getContent() |
||
131 | |||
132 | /** |
||
133 | * Converts the response's data to JSON. |
||
134 | * |
||
135 | * @return string JSON representation of the results. |
||
136 | */ |
||
137 | protected function toJson() |
||
138 | { |
||
139 | $serializedActionResults = array(); |
||
140 | |||
141 | foreach ($this->results as $action => $result) { |
||
142 | $serializedActionResults[$action] = serialize($result); |
||
143 | } |
||
144 | |||
145 | $responseData = array( |
||
146 | 'requestId' => $this->requestId, |
||
147 | 'actionResults' => $serializedActionResults |
||
148 | ); |
||
149 | |||
150 | return json_encode($responseData); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Gets the Id of the request this response belongs to. |
||
155 | * |
||
156 | * @return string Request Id. |
||
157 | */ |
||
158 | 1 | public function getRequestId() |
|
159 | { |
||
160 | 1 | return $this->requestId; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * Sets the Id of the request this response belongs to. |
||
165 | * |
||
166 | * @param string $requestId Request Id. |
||
167 | * @return void |
||
168 | */ |
||
169 | 3 | public function setRequestId($requestId) |
|
173 | } |
||
174 |