Example #1
0
 /**
  * Populates the current resource from a response object.
  *
  * @param ResponseInterface $response
  *
  * @return $this|ResourceInterface
  */
 public function populateFromResponse(ResponseInterface $response)
 {
     if (strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0) {
         $json = Utils::jsonDecode($response);
         if (!empty($json)) {
             $this->populateFromArray(Utils::flattenJson($json, $this->resourceKey));
         }
     }
     return $this;
 }
Example #2
0
 private function fetchResources()
 {
     if ($this->shouldNotSendAnotherRequest()) {
         return false;
     }
     $response = call_user_func($this->requestFn, $this->currentMarker);
     $json = Utils::flattenJson(Utils::jsonDecode($response), $this->resourcesKey);
     if ($response->getStatusCode() === 204 || empty($json)) {
         return false;
     }
     return $json;
 }
Example #3
0
 /**
  * {@inheritDoc}
  */
 public function enumerate(array $def, array $userVals = [], callable $mapFn = null)
 {
     $operation = $this->getOperation($def);
     $markerKey = $this->markerKey ?: self::DEFAULT_MARKER_KEY;
     $supportsPagination = $operation->hasParam('marker');
     $limit = isset($userVals['limit']) ? $userVals : false;
     $count = 0;
     $totalReached = function ($count) use($limit) {
         return $limit && $count >= $limit;
     };
     while (true) {
         $response = $this->sendRequest($operation, $userVals);
         $json = Utils::jsonDecode($response);
         if (!$json) {
             break;
         }
         $json = Utils::flattenJson($json, $this->resourcesKey);
         if ($response->getStatusCode() === 204 || empty($json)) {
             break;
         }
         foreach ($json as $resourceData) {
             if ($totalReached($count)) {
                 break;
             }
             $count++;
             $resource = $this->newInstance();
             $resource->populateFromArray($resourceData);
             if ($mapFn) {
                 call_user_func_array($mapFn, [$resource]);
             }
             if ($supportsPagination) {
                 $userVals['marker'] = $resource->{$markerKey};
             }
             (yield $resource);
         }
         if ($totalReached($count) || !$supportsPagination) {
             break;
         }
     }
 }
Example #4
0
 /**
  * {@inheritDoc}
  */
 public function populateFromArray(array $array) : self
 {
     return parent::populateFromArray(Utils::flattenJson($array, $this->resourceKey));
 }