Example #1
0
 public function execute()
 {
     $this->hasExecuted = true;
     $this->items = [];
     /** @var integer $pageNum */
     $pageNum = 1;
     /** @var integer $numberResults */
     $numberResults = 0;
     /** @var int $pageSize */
     if (!$this->getLimit() || $this->getLimit() > self::MAX_PAGE_LIMIT) {
         $pageSize = self::MAX_PAGE_LIMIT;
     } else {
         $pageSize = $this->getLimit();
     }
     /** @var string[] $params */
     $params = array_merge($this->entity->getDefaultQueryParams(), $this->filters);
     $params['page'] = $pageNum;
     $params['per_page'] = $pageSize;
     if ($this->getSortBy()) {
         $params['sort'] = ($this->getSortDir() == self::SORT_DESC ? '-' : '') . $this->getSortBy();
     }
     /** @var string $resultsKey */
     $resultsKey = $this->entity->getApiEntityCollectionName();
     //sprintf('%ss', $this->entity->getApiEntityName());
     while (true) {
         // clip last page if it exceeds requested result limit
         if ($this->getLimit() && $this->getLimit() - $numberResults < $pageSize) {
             $params['per_page'] = $this->getLimit() - $numberResults;
         }
         /** @var string $url */
         $url = $this->api->getUrl($this->entity->getApiResourceName(), $params);
         /** @var mixed[] $response */
         $response = $this->api->GET($url);
         $response = $this->entity->parseCollectionResponse($response);
         if (!isset($response[$resultsKey])) {
             throw new Exception("{$resultsKey} could not be found in query result");
         }
         /** @var mixed[] $data */
         $data = $response[$resultsKey];
         /** @var mixed[] $entityData */
         foreach ($data as $entityData) {
             $entity = $this->entity->deserialize($entityData);
             $entity->setApi($this->api);
             $this->items[] = $entity;
         }
         $numberResults += sizeof($data);
         if (!$this->entity->isPaginated()) {
             break;
         }
         // end of results reached?
         if (sizeof($data) < $pageSize) {
             break;
         }
         if ($this->getLimit() && $numberResults >= $this->getLimit()) {
             break;
         }
         ++$params['page'];
     }
 }
 /**
  * @test
  */
 public function getOne()
 {
     /** @var AbstractEntity $first */
     $first = $this->entity->query()->getFirst();
     if (!$first) {
         $this->fail(sprintf('No instances of %s found, cannot test', get_class($this->entity)));
     }
     /** @var AbstractEntity $entity */
     $entity = $this->entity->getByUrl($first->getUrl());
     $this->assertInstanceOf($this->getClassName(), $entity);
 }
Example #3
0
 /**
  * @return \string[]
  */
 public function getDefaultQueryParams()
 {
     /** @var string[] $params */
     $params = parent::getDefaultQueryParams();
     $params['nested_invoice_items'] = true;
     return $params;
 }
Example #4
0
 public function parseCollectionResponse(array $response)
 {
     $collection = [];
     foreach ($response as $key => $values) {
         $collection = array_merge($collection, array_values($values));
     }
     $response = ['categories' => $collection];
     return parent::parseCollectionResponse($response);
 }