예제 #1
0
 public function __construct(\GuzzleHttp\Psr7\Response $response)
 {
     $json = false;
     $data = $response->getBody();
     $this->rawData = $data;
     $this->response = $response;
     if ($response->hasHeader('Content-Type')) {
         // Let's see if it is JSON
         $contentType = $response->getHeader('Content-Type');
         if (strstr($contentType[0], 'json')) {
             $json = true;
             $data = json_decode($data);
         }
     }
     if (!$json) {
         // We can do another test here
         $decoded = json_decode($response->getBody());
         if ($decoded) {
             $json = true;
             $data = $decoded;
         }
     }
     $this->setData($data);
     $this->setIsJson($json);
 }
예제 #2
0
 public function testCreatesResponseWithAddedHeaderArray()
 {
     $r = new Response();
     $r2 = $r->withAddedHeader('foo', ['baz', 'bar']);
     $this->assertFalse($r->hasHeader('foo'));
     $this->assertEquals('baz, bar', $r2->getHeaderLine('foo'));
 }
 /**
  * Get cache ttl value
  *
  * @param Response $response
  *
  * @return int
  */
 protected function getCacheTtl(Response $response)
 {
     if ($this->useHeaderTtl && $response->hasHeader('Cache-Control')) {
         $cacheControl = $response->getHeader('Cache-Control')[0];
         if (preg_match('`max-age=(\\d+)`', $cacheControl, $match)) {
             return intval($match[1]);
         }
     }
     return $this->defaultTtl;
 }
예제 #4
0
 /**
  * @param ObjectInterface $object
  * @param array           $data
  * @param Response        $response
  * @return ObjectInterface
  */
 public function create(ObjectInterface $object, array $data, Response $response = null)
 {
     // If object had relationId, we pass it to new object
     if ($object->getAttribute("relationId")) {
         $relationId = $object->getAttribute("relationId");
         $object = new $object($relationId);
     } else {
         $object = new $object();
     }
     if (isset($data['items']) or isset($data[0])) {
         // If we have list of items
         $object = $this->createList($object, $data);
     } else {
         if (isset($data["id"])) {
             // TODO: Some things don't provide ID, possible BUG, that it isn't implemented in CREST
             $object->setAttribute("id", $data['id']);
             $uri = $object->getAttribute("uri") . $object->getAttribute("id") . "/";
             $object->setAttribute("uri", $uri);
         } elseif (!empty($data['href']) && is_numeric(basename($data["href"]))) {
             // TODO: Meybe not bug, so we just take it from href
             $object->setAttribute("id", (int) basename($data["href"]));
             $uri = $object->getAttribute("uri") . $object->getAttribute("id") . "/";
             $object->setAttribute("uri", $uri);
         }
         foreach ($data as $key => $value) {
             if (array_key_exists($key, $object->getRelations())) {
                 // ˇˇ
                 if (is_string($value)) {
                     // TODO: Sometimes relationship is only a string with href, not array like usually.
                     // TODO: When crest behaves as it should, remove this!
                     // Seen in GET crest.../wars/21/  value "killmails"
                     $object->setValue($key, $value);
                     continue;
                 }
                 // ^^
                 // Create new relation object with relation_id
                 $relation = $object->getRelations($key);
                 $object->setValue($key, $this->create(new $relation($object->getAttribute("id")), $value));
             } else {
                 $object->setValue($key, $value);
             }
         }
     }
     // Add cache-control header to Object, but only if this Endpoint made request
     //  we cant add cache-control for relationships, as we don't have data for them
     // Add HTTP Code, might be useful
     if ($response) {
         // On POST/PUT/DELETE responses cache-control isn't provided.
         if ($response->hasHeader("cache-control")) {
             $object->setAttribute("cache", $response->getHeader("cache-control")[0]);
         }
         $object->setAttribute("httpCode", $response->getStatusCode());
     }
     return $object;
 }
예제 #5
0
 /**
  * @inheritdoc
  */
 public function parser(Response $response)
 {
     return $response->hasHeader('ETag');
 }
예제 #6
0
파일: Response.php 프로젝트: bugotech/rfm
 /**
  * Get HTTP header.
  *
  * @param $key
  * @param mixed $default
  *
  * @return mixed
  */
 public function header($key, $default = null)
 {
     return $this->response->hasHeader($key) ? $this->response->getHeaderLine($key) : $default;
 }
예제 #7
0
 /**
  * @inheritdoc
  */
 public function parser(Response $response)
 {
     return $response->getStatusCode() == 200 && $response->hasHeader('ETag');
 }
예제 #8
0
 public function testWithoutHeaderThatExists()
 {
     $r = new Response(200, ['Foo' => 'Bar', 'Baz' => 'Bam']);
     $r2 = $r->withoutHeader('foO');
     $this->assertTrue($r->hasHeader('foo'));
     $this->assertSame(['Foo' => ['Bar'], 'Baz' => ['Bam']], $r->getHeaders());
     $this->assertFalse($r2->hasHeader('foo'));
     $this->assertSame(['Baz' => ['Bam']], $r2->getHeaders());
 }