ArangoDB-PHP-Core API Documentation
  • Namespace
  • Class
  • Deprecated

Namespaces

  • frankmayer
    • ArangoDbPhpCore
      • Api
        • Rest
      • Config
      • Connectors
      • Plugins
      • Protocols
        • Http
      • TraitRepository

Classes

  • frankmayer\ArangoDbPhpCore\Api\Rest\Api
  • frankmayer\ArangoDbPhpCore\Api\Rest\Async
  • frankmayer\ArangoDbPhpCore\Api\Rest\Batch
  • frankmayer\ArangoDbPhpCore\Api\Rest\Collection
  • frankmayer\ArangoDbPhpCore\Api\Rest\Database
  • frankmayer\ArangoDbPhpCore\Api\Rest\Document
  • frankmayer\ArangoDbPhpCore\Api\Rest\DocumentBase
  • frankmayer\ArangoDbPhpCore\Api\Rest\Edge
  • frankmayer\ArangoDbPhpCore\Autoloader
  • frankmayer\ArangoDbPhpCore\Client
  • frankmayer\ArangoDbPhpCore\ClientOptions
  • frankmayer\ArangoDbPhpCore\Config\ConnectionOptions
  • frankmayer\ArangoDbPhpCore\Config\DefaultValues
  • frankmayer\ArangoDbPhpCore\Config\Endpoint
  • frankmayer\ArangoDbPhpCore\Config\UpdatePolicy
  • frankmayer\ArangoDbPhpCore\Connectors\AbstractHttpConnector
  • frankmayer\ArangoDbPhpCore\Plugins\Plugin
  • frankmayer\ArangoDbPhpCore\Plugins\PluginManager
  • frankmayer\ArangoDbPhpCore\Plugins\TestPlugin
  • frankmayer\ArangoDbPhpCore\Plugins\TracerPlugin
  • frankmayer\ArangoDbPhpCore\Protocols\Http\AbstractHttpRequest
  • frankmayer\ArangoDbPhpCore\Protocols\Http\HttpRequest
  • frankmayer\ArangoDbPhpCore\Protocols\Http\HttpResponse

Interfaces

  • frankmayer\ArangoDbPhpCore\Api\ApiInterface
  • frankmayer\ArangoDbPhpCore\Api\RestApiInterface
  • frankmayer\ArangoDbPhpCore\ClientInterface
  • frankmayer\ArangoDbPhpCore\ConnectorInterface
  • frankmayer\ArangoDbPhpCore\Plugins\PluginInterface
  • frankmayer\ArangoDbPhpCore\Protocols\Http\HttpConnectorInterface
  • frankmayer\ArangoDbPhpCore\Protocols\Http\HttpRequestInterface
  • frankmayer\ArangoDbPhpCore\Protocols\Http\HttpResponseInterface
  • frankmayer\ArangoDbPhpCore\Protocols\RequestInterface
  • frankmayer\ArangoDbPhpCore\Protocols\ResponseInterface

Traits

  • frankmayer\ArangoDbPhpCore\Api\Rest\DocumentTrait
  • frankmayer\ArangoDbPhpCore\Api\Rest\EdgeTrait
  • frankmayer\ArangoDbPhpCore\TraitRepository\ClientLogger

Exceptions

  • frankmayer\ArangoDbPhpCore\ClientException
  • frankmayer\ArangoDbPhpCore\ConnectionException
  • frankmayer\ArangoDbPhpCore\Exception
  • frankmayer\ArangoDbPhpCore\ServerException
  1 <?php
  2 
  3 /**
  4  * ArangoDB PHP Core Client: HTTP Response
  5  *
  6  * @author    Frank Mayer
  7  * @copyright Copyright 2013-2015, FRANKMAYER.NET, Athens, Greece
  8  */
  9 
 10 namespace frankmayer\ArangoDbPhpCore\Protocols\Http;
 11 
 12 use frankmayer\ArangoDbPhpCore\ServerException;
 13 
 14 
 15 /**
 16  * Http-Response object holding the raw and objectified Response data.
 17  *
 18  * @package frankmayer\ArangoDbPhpCore\Protocols\Http
 19  */
 20 class HttpResponse implements HttpResponseInterface
 21 {
 22     /**
 23      * @var array An array with the http status codes of the ones, that we want to raise an exception for.
 24      *
 25      * This should be set with an array like array(400,401,402,403);
 26      */
 27     public $enabledHttpServerExceptions;
 28 
 29 
 30     /**
 31      * @var HttpRequest $request The request object
 32      */
 33     public $request;
 34     /**
 35      * @var array $headers The headers in form of an multidimensional array
 36      */
 37     public $headers = [];
 38     /**
 39      * @var string $body The body of the response
 40      */
 41     public $body;
 42     /**
 43      * @var array $batch An array of batch parts
 44      */
 45     public $batch;
 46     /**
 47      * @var string $protocol The protocol used
 48      */
 49     public $protocol;
 50     /**
 51      * @var int $status The http status code of the response
 52      */
 53     public $status;
 54     /**
 55      * @var string $statusPhrase The associated text to the status code
 56      */
 57     public $statusPhrase;
 58     /**
 59      * @var bool $verboseExtractStatusLine get only status code or also the accompanying text
 60      */
 61     public $verboseExtractStatusLine = false;
 62     /**
 63      * @var bool This holds the Batchpart Content-Id of the response if one was supplied to ArangoDB in the request.
 64      */
 65     public $batchContentId;
 66 
 67 
 68     /**
 69      *
 70      */
 71     public function __construct()
 72     {
 73         // 404 intentionally left out as a default, because not finding data, shouldn't raise an exception
 74         $this->enabledHttpServerExceptions = [400, 401, 403, 405, 412, 500, 600, 601];
 75     }
 76 
 77 
 78     /**
 79      * @param $request
 80      *
 81      * @return $this
 82      * @throws ServerException
 83      *
 84      */
 85     public function build($request)
 86     {
 87         if ($request instanceof AbstractHttpRequest) {
 88             $response      = $request->response;
 89             $this->request = $request;
 90 
 91             if ($request->batch === true) {
 92                 $boundary    = $request->batchBoundary;
 93                 $this->batch = $this->deconstructBatchResponseBody($response, $boundary);
 94             }
 95         } else {
 96             $response = $request;
 97         }
 98         $this->splitResponseToHeadersArrayAndBody($response);
 99         $statusLineArray = explode(" ", trim($this->headers['status'][0]));
100 
101         $this->status = (int) $statusLineArray[1];
102 
103         if ($this->verboseExtractStatusLine === true) {
104             $this->protocol = $statusLineArray[0];
105 
106             $this->statusPhrase = $this->decodeGetStatusPhrase($statusLineArray);
107         }
108 
109         if (in_array(intval($this->status), $this->enabledHttpServerExceptions)) {
110             // Ignoring this, as the server needs to have authentication enabled in order to run through this.
111             // @codeCoverageIgnoreStart
112             $this->protocol     = $statusLineArray[0];
113             $this->statusPhrase = $this->decodeGetStatusPhrase($statusLineArray);
114             throw new ServerException($this->statusPhrase, $this->status);
115             // @codeCoverageIgnoreEnd
116         }
117 
118         return $this;
119     }
120 
121 
122     /**
123      * @param $statusLineArray
124      *
125      * @return string
126      */
127     protected function decodeGetStatusPhrase($statusLineArray)
128     {
129         $phrase = '';
130         foreach ($statusLineArray as $key => $part) {
131             if ($key > 1) {
132                 $phrase .= $part . ' ';
133             }
134         }
135         $phrase = trim($phrase, " ");
136 
137         return $phrase;
138     }
139 
140 
141     /**
142      * Splits the response data to a Headers array and a body
143      *
144      * It expects the response data to be in $this->request->response
145      * It puts the headers into $this->headers and
146      * the body into $this->body
147      *
148      * @param $response
149      */
150     protected function splitResponseToHeadersArrayAndBody($response)
151     {
152         //        $tmp = explode("\r\n\r\n", $response, 2);
153         //        var_export($tmp);
154         list($headers, $this->body) = explode("\r\n\r\n", $response, 2);
155 
156         $this->headers = $this->getHeaderArray($headers);
157     }
158 
159 
160     /**
161      * @param $batchResponseBody
162      * @param $boundary
163      *
164      * @return array
165      * @throws ServerException
166      */
167     public function deconstructBatchResponseBody($batchResponseBody, $boundary)
168     {
169         $connector         = $this->request->client->connector;
170         $batchObjects      = [];
171         $batchResponseBody = rtrim($batchResponseBody, '--' . $boundary . '--');
172 
173         $batchParts = explode('--' . $boundary . $connector::HTTP_EOL, $batchResponseBody);
174         array_shift($batchParts);
175         $i = 0;
176         foreach ($batchParts as &$batchPart) {
177             /** @var $batchPart HttpResponse */
178             $batchPartHeaders = static::splitBatchPart($batchPart);
179 
180             $batchObject = new static();
181             $batchObject->build($batchPartHeaders[1]);
182             //            $batchArangoHeader  = explode(PHP_EOL, $batchPartHeaders[0]);
183             $batchArangoHeaderArray = $this->getHeaderArray($batchPartHeaders[0]);
184             if (array_key_exists('Content-Id', $batchArangoHeaderArray)) {
185                 $batchObject->batchContentId = $batchArangoHeaderArray['Content-Id'][0];
186             }
187             $batchObjects[] = $batchObject;
188             $i++;
189         }
190 
191         return $batchObjects;
192     }
193 
194 
195     /**
196      * @param $batchPart
197      *
198      * @return array
199      */
200     public static function splitBatchPart($batchPart)
201     {
202         $parts = explode("\r\n\r\n", trim($batchPart), 2);
203 
204         return $parts;
205     }
206 
207 
208     /**
209      * @param mixed $batch
210      */
211     public function setBatch($batch)
212     {
213         $this->batch = $batch;
214     }
215 
216     /**
217      * @return mixed
218      */
219     public function getBatch()
220     {
221         return $this->batch;
222     }
223 
224     /**
225      * @param mixed $body
226      */
227     public function setBody($body)
228     {
229         $this->body = $body;
230     }
231 
232     /**
233      * @return mixed
234      */
235     public function getBody()
236     {
237         return $this->body;
238     }
239 
240     /**
241      * @param array $headers
242      */
243     public function setHeaders($headers)
244     {
245         $this->headers = $headers;
246     }
247 
248     /**
249      * @return array
250      */
251     public function getHeaders()
252     {
253         return $this->headers;
254     }
255 
256     /**
257      * @param \frankmayer\ArangoDbPhpCore\Protocols\Http\HttpRequest $request
258      */
259     public function setRequest($request)
260     {
261         $this->request = $request;
262     }
263 
264     /**
265      * @return \frankmayer\ArangoDbPhpCore\Protocols\Http\HttpRequest
266      */
267     public function getRequest()
268     {
269         return $this->request;
270     }
271 
272     /**
273      * @param mixed $status
274      */
275     public function setStatus($status)
276     {
277         $this->status = $status;
278     }
279 
280     /**
281      * @return mixed
282      */
283     public function getStatus()
284     {
285         return $this->status;
286     }
287 
288     /**
289      * @return mixed
290      */
291     public function getProtocol()
292     {
293         return $this->protocol;
294     }
295 
296     /**
297      * @return mixed
298      */
299     public function getStatusPhrase()
300     {
301         return $this->statusPhrase;
302     }
303 
304     /**
305      * @param boolean $verboseStatusLine
306      */
307     public function setVerboseExtractStatusLine($verboseStatusLine)
308     {
309         $this->verboseExtractStatusLine = $verboseStatusLine;
310     }
311 
312     /**
313      * @return boolean
314      */
315     public function getVerboseExtractStatusLine()
316     {
317         return $this->verboseExtractStatusLine;
318     }
319 
320     /**
321      * @param $headers
322      *
323      * @return array
324      */
325     protected function getHeaderArray($headers)
326     {
327         $headerArray  = [];
328         $headersArray = explode("\r\n", trim($headers));
329         foreach ($headersArray as $line => $header) {
330             if ($line > 0) {
331                 $pair                    = explode(": ", $header);
332                 $headerArray[$pair[0]][] = $pair[1];
333             } else {
334                 $headerArray['status'][] = $header;
335             }
336         }
337 
338         return $headerArray;
339     }
340 }
341 
ArangoDB-PHP-Core API Documentation API documentation generated by ApiGen