File: //home/cca63905/www/nueva/modules/wnetsecurity/src/Api/Resource/HydraCollectionPage.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;
/**
* @template T of HydraResource
* @template-implements \IteratorAggregate<int, T>
*/
class HydraCollectionPage extends HydraResource implements \IteratorAggregate, \Countable
{
public const TYPE = 'hydra:Collection';
protected const CASTS = [
'hydra:view' => HydraPartialCollectionView::class,
];
private $memberClass;
public static function getType(): string
{
return self::TYPE;
}
/**
* @param class-string<T>|null $memberClass
*/
public function __construct(array $data = [], string $memberClass = null)
{
if (null !== $memberClass && !is_subclass_of($memberClass, HydraResource::class)) {
throw new \InvalidArgumentException(sprintf('%s is not a %s', $memberClass, HydraResource::class));
}
$this->memberClass = $memberClass;
parent::__construct($data);
}
/** @return static */
public static function cast($data, string $memberClass = null): ApiResource
{
$data = (array) $data;
if (!isset($data['@type']) || $data['@type'] !== self::TYPE) {
throw new \InvalidArgumentException(sprintf('Expected "@type" to be "%s", "%s" given', self::TYPE, $data['@type'] ?? ''));
}
return new static($data, $memberClass);
}
public static function castMany(array $data, string $memberClass = null): array
{
return array_map(static function (array $data) use ($memberClass) {
return static::cast($data, $memberClass);
}, $data);
}
public function getMemberClass(): ?string
{
return $this->memberClass;
}
public function getView(): ?HydraPartialCollectionView
{
return $this->getAttribute('hydra:view');
}
public function getNextPageUri(): ?string
{
$view = $this->getView();
return null !== $view ? $view->getNextPageUri() : null;
}
/** @return T[] */
public function getMembers(): array
{
return $this->getAttribute('hydra:member') ?? [];
}
/** @return \Generator<int, T> */
public function getIterator(): \Generator
{
yield from $this->getMembers();
}
public function count(): int
{
return count($this->getMembers());
}
protected function getCastTypes(): array
{
$castTypes = static::CASTS;
if (isset($this->memberClass)) {
$castTypes['hydra:member'] = $this->memberClass . '[]';
}
return $castTypes;
}
}