This change updates the namespace across multiple files to reflect the new naming convention.
184 lines
5.1 KiB
PHP
184 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Uwil\ArcadiaApiPhpClient\Lib\ApiResponse;
|
|
|
|
use UnexpectedValueException;
|
|
use Uwil\ArcadiaApiPhpClient\Exception\InvalidApiResponse;
|
|
use Uwil\ArcadiaApiPhpClient\Utils\StringEncoder;
|
|
use Uwil\ArcadiaApiPhpClient\Utils\Utils;
|
|
|
|
// https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/734
|
|
// Les property hook ne sont pas supportés.
|
|
// phpcs:disable
|
|
|
|
final class ApiResponse
|
|
{
|
|
private const DATA_KEY = 'data';
|
|
|
|
private const PAGINATION_KEY = 'pagination';
|
|
|
|
public readonly ?ApiPagination $pagination;
|
|
|
|
/** @var array<string, mixed> */
|
|
public array $data {
|
|
get {
|
|
return Utils::deepCopy($this->data);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $responseData
|
|
*/
|
|
public function __construct(
|
|
array $responseData
|
|
) {
|
|
$this->pagination = $this->toPagination($responseData);
|
|
$this->data = $this->toData(
|
|
$responseData,
|
|
$this->pagination !== null
|
|
);
|
|
}
|
|
|
|
public function isEmpty(): bool
|
|
{
|
|
return count($this->data) === 0;
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $data
|
|
*/
|
|
public static function fromBody(array $data): self
|
|
{
|
|
return new ApiResponse($data);
|
|
}
|
|
|
|
/**
|
|
* @template T of mixed
|
|
*
|
|
* NOTE: Can only be call when reponse is paginated.
|
|
*
|
|
* @param callable(array<string,mixed>):T $hook
|
|
* @return T[]
|
|
*/
|
|
public function mapData(callable $hook): array
|
|
{
|
|
if ($this->pagination === null) {
|
|
throw new \LogicException('Response is not paginated.');
|
|
}
|
|
|
|
return array_map(fn (array $item) => $hook($item), $this->data);
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $responseData
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function toData(array $responseData, bool $isPaginated): array
|
|
{
|
|
if (
|
|
! array_key_exists(self::DATA_KEY, $responseData)
|
|
|| ! is_array($responseData[self::DATA_KEY])
|
|
) {
|
|
throw new InvalidApiResponse(
|
|
sprintf(
|
|
'Missing key %s form in response',
|
|
self::DATA_KEY
|
|
)
|
|
);
|
|
}
|
|
|
|
/** @var array<string,mixed> $output */
|
|
$output = $responseData[self::DATA_KEY];
|
|
|
|
if ($isPaginated && count($output)) {
|
|
// NOTE: When there is only one result output is not a list but a record (Not documentend, seen on 30/03/26).
|
|
$keys = array_keys($output);
|
|
|
|
/** @var array<string, mixed> */
|
|
return (array_shift($keys) === 0 || ! count($output)) ?
|
|
$output : [$output];
|
|
}
|
|
|
|
// * Make string any string is encoded in proper UTF-8 string.
|
|
$this->encodeCallOutputToUtf8($output);
|
|
|
|
if (! is_array($output)) {
|
|
throw new UnexpectedValueException('Output shall be ann array here');
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
protected function toPagination(array $data): ?ApiPagination
|
|
{
|
|
if (
|
|
! array_key_exists(self::PAGINATION_KEY, $data)
|
|
|| ! is_array($data[self::PAGINATION_KEY])
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
$skel = [
|
|
'page' => 1,
|
|
'pageSize' => 1,
|
|
'totalItems' => 0,
|
|
'totalPages' => 0,
|
|
'links' => [
|
|
'first' => 'https://none.example.ldc.fr',
|
|
'last' => 'https://none.example.ldc.fr',
|
|
],
|
|
];
|
|
|
|
$expectedKeys = array_keys($skel);
|
|
$actualKeys = array_keys($data[self::PAGINATION_KEY] ?? []);
|
|
|
|
$missing = array_diff($expectedKeys, $actualKeys);
|
|
$extra = array_diff($actualKeys, $expectedKeys);
|
|
|
|
if ($missing || $extra) {
|
|
throw new InvalidApiResponse(
|
|
sprintf(
|
|
'Unexpected %s form in response, mssing keys: %s, extra keys : %s',
|
|
self::PAGINATION_KEY,
|
|
json_encode($missing),
|
|
json_encode($extra),
|
|
)
|
|
);
|
|
}
|
|
|
|
$page = intval($data[self::PAGINATION_KEY]['page']);
|
|
$pageSize = intval($data[self::PAGINATION_KEY]['pageSize']);
|
|
$totalItems = intval($data[self::PAGINATION_KEY]['totalItems']);
|
|
$totalPages = intval($data[self::PAGINATION_KEY]['totalPages']);
|
|
$links = ApiPaginationLinks::fromArray($data[self::PAGINATION_KEY]['links']);
|
|
|
|
return new ApiPagination(
|
|
$page,
|
|
$pageSize,
|
|
$totalItems,
|
|
$totalPages,
|
|
$links,
|
|
);
|
|
}
|
|
|
|
/** @param array<string,mixed> $output */
|
|
private function encodeCallOutputToUtf8(null | string | array &$output): void
|
|
{
|
|
if (is_array($output)) {
|
|
array_walk_recursive($output, function (null | string | array &$nestedOutput): void {
|
|
$this->encodeCallOutputToUtf8($nestedOutput);
|
|
});
|
|
}
|
|
|
|
if (! is_string($output)) {
|
|
return;
|
|
}
|
|
|
|
$output = StringEncoder::detectAndConvertToUtf8($output);
|
|
}
|
|
}
|