File: //proc/self/root/proc/self/cwd/nueva/modules/wnetsecurity/src/Api/Resource/HydraCollection.php
<?php
/**
* Copyright since 2014 Waynet Sp. z o.o.
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@waynet.pl so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop-project.org/ for more information.
*
* @author Waynet Sp. z o.o. <kontakt@waynet.pl>
* @copyright since 2014 Waynet Sp. z o.o.
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
declare(strict_types=1);
namespace Waynet\Security\Api\Resource;
use Waynet\Security\Api\ApiClient;
/**
* @template T of HydraResource
* @template-implements \IteratorAggregate<int, T>
*/
class HydraCollection implements \IteratorAggregate, \Countable
{
public const TYPE = 'hydra:Collection';
private $client;
private $memberClass;
private $collectionUri;
private $totalItems;
private $pageData;
/**
* @param class-string<T> $memberClass
*/
public function __construct(ApiClient $client, string $memberClass, string $collectionUri)
{
if (!is_subclass_of($memberClass, HydraResource::class)) {
throw new \InvalidArgumentException(sprintf('%s is not a %s', $memberClass, HydraResource::class));
}
$this->client = $client;
$this->memberClass = $memberClass;
$this->collectionUri = $collectionUri;
}
public function getMemberClass(): string
{
return $this->memberClass;
}
/** @return \Generator<int, T> */
public function getIterator(): \Generator
{
$uri = $this->collectionUri;
do {
$data = $this->getPageData($uri);
$this->totalItems = $data['hydra:totalItems'];
foreach ($data['hydra:member'] as $member) {
yield $this->memberClass::cast($member);
}
$view = isset($data['hydra:view']) ? HydraPartialCollectionView::cast($data['hydra:view']) : null;
} while ($view && $uri = $view->getNextPageUri());
}
public function count(): int
{
if (!isset($this->totalItems)) {
$this->pageData = $this->getPageData($this->collectionUri);
$this->totalItems = $this->pageData['hydra:totalItems'];
}
return $this->totalItems;
}
/**
* @param int $page
*
* @return HydraCollectionPage<T>
*/
public function getPage(int $page = 1): HydraCollectionPage
{
if (0 >= $page) {
throw new \InvalidArgumentException('Page must be greater than 0');
}
$uri = $this->getPageUri($page);
$data = $this->client->getCollection($uri);
return HydraCollectionPage::cast($data, $this->memberClass);
}
private function getPageData(string $uri): array
{
if (isset($this->pageData)) {
$pageData = $this->pageData;
unset($this->pageData);
return $pageData;
}
return $this->client->getCollection($uri);
}
private function getPageUri(int $page): string
{
$pageUri = http_build_url($this->collectionUri, ['query' => 'page=' . $page], HTTP_URL_JOIN_QUERY);
if ('/' === $this->collectionUri[0]) {
return $pageUri;
}
// fix relative path
return substr($pageUri, 1);
}
}