1 <?php
2
3 /**
4 * ArangoDB PHP client: batchpart
5 *
6 * @package triagens\ArangoDb
7 * @author Frank Mayer
8 * @since 1.1
9 *
10 */
11
12 namespace triagens\ArangoDb;
13
14 /**
15 * Provides batch part functionality
16 *
17 * <br>
18 *
19 * @package triagens\ArangoDb
20 * @since 1.1
21 */
22
23
24 class BatchPart
25 {
26
27
28 /**
29 * An array of BatchPartCursor options
30 *
31 * @var array $_batchParts
32 */
33 private $_cursorOptions = [];
34
35
36 /**
37 * An array of BatchPartCursor options
38 *
39 * @var array $_batchParts
40 */
41 private $_id;
42
43
44 /**
45 * An array of BatchPartCursor options
46 *
47 * @var array $_batchParts
48 */
49 private $_type;
50
51
52 /**
53 * An array of BatchPartCursor options
54 *
55 * @var array $_batchParts
56 */
57 private $_request = [];
58
59
60 /**
61 * An array of BatchPartCursor options
62 *
63 * @var HttpResponse $_response
64 */
65 private $_response = [];
66
67
68 /**
69 * The batch that this instance is part of
70 *
71 * @var Batch $_batch
72 */
73 private $_batch;
74
75
76 /**
77 * Constructor
78 *
79 * @param Batch $batch the batch object, that this part belongs to
80 * @param mixed $id The id of the batch part. TMust be unique and wil be passed to the server in the content-id header
81 * @param mixed $type The type of the request. This is to distinguish the different request type in order to return correct results.
82 * @param mixed $request The request string
83 * @param mixed $response The response string
84 * @param mixed $options optional, options like sanitize, that can be passed to the request/response handler.
85 *
86 */
87
88 public function __construct($batch, $id, $type, $request, $response, $options)
89 {
90 $sanitize = false;
91 $options = array_merge($options, $this->getCursorOptions());
92 extract($options, EXTR_IF_EXISTS);
93 $this->setBatch($batch);
94 $this->setId($id);
95 $this->setType($type);
96 $this->setRequest($request);
97 $this->setResponse($response);
98 $this->_cursorOptions[Cursor::ENTRY_SANITIZE] = $sanitize;
99 }
100
101
102 /**
103 * Sets the id for the current batch part.
104 *
105 * @param Batch $batch
106 *
107 * @return BatchPart
108 */
109 public function setBatch($batch)
110 {
111 $this->_batch = $batch;
112
113 return $this;
114 }
115
116
117 /**
118 * Sets the id for the current batch part.
119 *
120 * @param mixed $id
121 *
122 * @return BatchPart
123 */
124 public function setId($id)
125 {
126 $this->_id = $id;
127
128 return $this;
129 }
130
131
132 /**
133 * Gets the id for the current batch part.
134 *
135 * @return mixed
136 */
137 public function getId()
138 {
139 return $this->_id;
140 }
141
142
143 /**
144 * Sets the type for the current batch part.
145 *
146 * @param mixed $type
147 *
148 * @return BatchPart
149 */
150 public function setType($type)
151 {
152 $this->_type = $type;
153
154 return $this;
155 }
156
157
158 /**
159 * Gets the type for the current batch part.
160 *
161 * @return mixed
162 */
163 public function getType()
164 {
165 return $this->_type;
166 }
167
168
169 /**
170 * Sets the request for the current batch part.
171 *
172 * @param mixed $request
173 *
174 * @return BatchPart
175 */
176 public function setRequest($request)
177 {
178 $this->_request = $request;
179
180 return $this;
181 }
182
183
184 /**
185 * Gets the request for the current batch part.
186 *
187 * @return array
188 */
189 public function getRequest()
190 {
191 return $this->_request;
192 }
193
194
195 /**
196 * Sets the response for the current batch part.
197 *
198 * @param mixed $response
199 *
200 * @return BatchPart
201 */
202 public function setResponse($response)
203 {
204 $this->_response = $response;
205
206 return $this;
207 }
208
209
210 /**
211 * Gets the response for the current batch part.
212 *
213 * @return HttpResponse
214 */
215 public function getResponse()
216 {
217 return $this->_response;
218 }
219
220
221 /**
222 * Gets the HttpCode for the current batch part.
223 *
224 * @return int
225 */
226 public function getHttpCode()
227 {
228 return $this->getResponse()->getHttpCode();
229 }
230
231
232 /**
233 * Get the batch part identified by the array key (0...n) or its id (if it was set with nextBatchPartId($id) )
234 *
235 * @throws ClientException
236 * @return mixed $partId
237 */
238 public function getProcessedResponse()
239 {
240 $response = $this->getResponse();
241 switch ($this->_type) {
242 case 'getdocument':
243 $json = $response->getJson();
244 $options = $this->getCursorOptions();
245 $options['isNew'] = false;
246 $response = Document::createFromArray($json, $options);
247 break;
248 case 'document':
249 $json = $response->getJson();
250 if (!isset($json['error']) || $json['error'] === false) {
251 $id = $json[Document::ENTRY_ID];
252 $response = $id;
253 }
254 break;
255 case 'getedge':
256 $json = $response->getJson();
257 $options = $this->getCursorOptions();
258 $options['isNew'] = false;
259 $response = Edge::createFromArray($json, $options);
260 break;
261 case 'edge':
262 $json = $response->getJson();
263 if (!isset($json['error']) || $json['error'] === false) {
264 $id = $json[Edge::ENTRY_ID];
265 $response = $id;
266 }
267 break;
268 case 'getcollection':
269 $json = $response->getJson();
270 $response = Collection::createFromArray($json);
271 break;
272 case 'collection':
273 $json = $response->getJson();
274 if (!isset($json['error']) || $json['error'] === false) {
275 $id = $json[Collection::ENTRY_ID];
276 $response = $id;
277 }
278 break;
279 case 'cursor':
280 $options = $this->getCursorOptions();
281 $options['isNew'] = false;
282 $response = new Cursor($this->_batch->getConnection(), $response->getJson(), $options);
283 break;
284 default:
285 throw new ClientException('Could not determine response data type.');
286 break;
287 }
288
289 return $response;
290 }
291
292
293 /**
294 * Return an array of cursor options
295 *
296 * @return array - array of options
297 */
298 private function getCursorOptions()
299 {
300 return $this->_cursorOptions;
301 }
302 }
303