Skip to content

Commit 041e76c

Browse files
fix: basic pagination should work
1 parent b306fa4 commit 041e76c

File tree

7 files changed

+55
-28
lines changed

7 files changed

+55
-28
lines changed

src/Core.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
namespace CasParser\Core;
66

7-
/**
8-
* @internal
9-
*/
10-
enum Omittable
11-
{
12-
case OMIT;
13-
}
7+
use CasParser\Core\Implementation\Omittable;
148

159
const OMIT = Omittable::OMIT;

src/Core/BaseClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@ public function request(
7979
[$req, $opts] = $this->buildRequest(method: $method, path: $path, query: $query, headers: $headers, body: $body, opts: $options);
8080
['method' => $method, 'path' => $uri, 'headers' => $headers] = $req;
8181

82-
$req = $this->requestFactory->createRequest($method, uri: $uri);
83-
$req = Util::withSetHeaders($req, headers: $headers);
82+
$request = $this->requestFactory->createRequest($method, uri: $uri);
83+
$request = Util::withSetHeaders($request, headers: $headers);
8484

8585
// @phpstan-ignore-next-line
86-
$rsp = $this->sendRequest($req, data: $body, opts: $opts, redirectCount: 0, retryCount: 0);
86+
$rsp = $this->sendRequest($request, data: $body, opts: $opts, redirectCount: 0, retryCount: 0);
8787

8888
$decoded = Util::decodeContent($rsp);
8989

9090
if (!is_null($stream)) {
9191
return new $stream(
9292
convert: $convert,
93-
request: $req,
93+
request: $request,
9494
response: $rsp,
9595
stream: $decoded
9696
);

src/Core/Concerns/SdkModel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CasParser\Core\Concerns;
66

77
use CasParser\Core\Contracts\BaseModel;
8+
use CasParser\Core\Contracts\BasePage;
89
use CasParser\Core\Conversion;
910
use CasParser\Core\Conversion\CoerceState;
1011
use CasParser\Core\Conversion\Contracts\Converter;
@@ -244,7 +245,7 @@ private function unsetOptionalProperties(): void
244245
*/
245246
private static function serialize(mixed $value): mixed
246247
{
247-
if ($value instanceof BaseModel) {
248+
if ($value instanceof BaseModel || $value instanceof BasePage) {
248249
return $value->toArray();
249250
}
250251

src/Core/Pagination/AbstractPage.php renamed to src/Core/Concerns/SdkPage.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace CasParser\Core\Pagination;
5+
namespace CasParser\Core\Concerns;
66

77
use CasParser\Client;
8-
use CasParser\Core\Contracts\BasePage;
98
use CasParser\Core\Conversion\Contracts\Converter;
109
use CasParser\Core\Conversion\Contracts\ConverterSource;
1110
use CasParser\Core\Errors\APIStatusError;
@@ -16,19 +15,20 @@
1615
*
1716
* @template Item
1817
*
19-
* @implements BasePage<Item>
20-
*
2118
* @phpstan-import-type normalized_request from \CasParser\Core\BaseClient
2219
*/
23-
abstract class AbstractPage implements BasePage
20+
trait SdkPage
2421
{
25-
public function __construct(
26-
protected Converter|ConverterSource|string $convert,
27-
protected Client $client,
28-
protected array $request,
29-
protected RequestOptions $options,
30-
protected mixed $data,
31-
) {}
22+
private Converter|ConverterSource|string $convert;
23+
24+
private Client $client;
25+
26+
/**
27+
* normalized_request $request.
28+
*/
29+
private array $request;
30+
31+
private RequestOptions $options;
3232

3333
/**
3434
* @return list<Item>

src/Core/Contracts/BasePage.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
use CasParser\RequestOptions;
1111

1212
/**
13+
* @internal
14+
*
1315
* @template Item
1416
*
17+
* @extends \ArrayAccess<string, mixed>
1518
* @extends \IteratorAggregate<int, static>
1619
*/
17-
interface BasePage extends \IteratorAggregate
20+
interface BasePage extends \ArrayAccess, \JsonSerializable, \Stringable, \IteratorAggregate
1821
{
1922
/**
2023
* @internal
@@ -29,6 +32,14 @@ public function __construct(
2932
mixed $data,
3033
);
3134

35+
/**
36+
* @return static<Item>
37+
*/
38+
public static function fromArray(mixed $data): self;
39+
40+
/** @return array<string, mixed> */
41+
public function toArray(): array;
42+
3243
public function hasNextPage(): bool;
3344

3445
/**
@@ -40,4 +51,9 @@ public function getPaginatedItems(): array;
4051
* @return static<Item>
4152
*/
4253
public function getNextPage(): static;
54+
55+
/**
56+
* @return \Generator<Item>
57+
*/
58+
public function pagingEachItem(): \Generator;
4359
}

src/Core/Conversion/ModelOf.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use CasParser\Core\Attributes\Api;
88
use CasParser\Core\Contracts\BaseModel;
9+
use CasParser\Core\Contracts\BasePage;
910
use CasParser\Core\Conversion;
1011
use CasParser\Core\Conversion\Contracts\Converter;
1112

@@ -20,7 +21,7 @@ final class ModelOf implements Converter
2021
public readonly array $properties;
2122

2223
/**
23-
* @param \ReflectionClass<BaseModel> $class
24+
* @param \ReflectionClass<BaseModel|BasePage<mixed>> $class
2425
*/
2526
public function __construct(public readonly \ReflectionClass $class)
2627
{
@@ -93,8 +94,10 @@ public function coerce(mixed $value, CoerceState $state): mixed
9394

9495
/**
9596
* @param array<mixed> $data
97+
*
98+
* @return BaseModel|BasePage<mixed>
9699
*/
97-
public function from(array $data): BaseModel
100+
public function from(array $data): BaseModel|BasePage
98101
{
99102
$instance = $this->class->newInstanceWithoutConstructor();
100103
$instance->__unserialize($data); // @phpstan-ignore-line
@@ -104,7 +107,7 @@ public function from(array $data): BaseModel
104107

105108
public function dump(mixed $value, DumpState $state): mixed
106109
{
107-
if ($value instanceof BaseModel) {
110+
if ($value instanceof BaseModel || $value instanceof BasePage) {
108111
$value = $value->toArray();
109112
}
110113

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CasParser\Core\Implementation;
6+
7+
/**
8+
* @internal
9+
*/
10+
enum Omittable
11+
{
12+
case OMIT;
13+
}

0 commit comments

Comments
 (0)