Skip to content

Commit 95b27e8

Browse files
chore(refactor): simplify base page interface
1 parent 86ba48b commit 95b27e8

File tree

4 files changed

+40
-36
lines changed

4 files changed

+40
-36
lines changed

src/Core/Concerns/SdkModel.php

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

77
use CasParser\Core\Contracts\BaseModel;
8-
use CasParser\Core\Contracts\BasePage;
98
use CasParser\Core\Conversion;
109
use CasParser\Core\Conversion\CoerceState;
1110
use CasParser\Core\Conversion\Contracts\Converter;
@@ -15,7 +14,7 @@
1514
/**
1615
* @internal
1716
*
18-
* @template-covariant Data of array<string, mixed>
17+
* @template-covariant Shape of array<string, mixed>
1918
*/
2019
trait SdkModel
2120
{
@@ -41,12 +40,12 @@ public function __serialize(): array
4140
/**
4241
* @internal
4342
*
44-
* @param array<mixed> $data
43+
* @param array<string, mixed> $data
4544
*/
4645
public function __unserialize(array $data): void
4746
{
4847
foreach ($data as $key => $value) {
49-
$this->offsetSet($key, value: $value);
48+
$this->offsetSet($key, value: $value); // @phpstan-ignore-line
5049
}
5150
}
5251

@@ -73,12 +72,14 @@ public function __toString(): string
7372
* a native class property, indicating an omitted value,
7473
* or a property overridden with an incongruent type
7574
*
75+
* @return value-of<Shape>
76+
*
7677
* @throws \Exception
7778
*/
7879
public function __get(string $key): mixed
7980
{
8081
if (!array_key_exists($key, array: self::$converter->properties)) {
81-
throw new \Exception("Property '{$key}' does not exist in {$this}::class");
82+
throw new \RuntimeException("Property '{$key}' does not exist in {$this}::class");
8283
}
8384

8485
// The unset property was overridden by a value with an incongruent type.
@@ -91,11 +92,11 @@ public function __get(string $key): mixed
9192

9293
// An optional property which was unset to be omitted from serialized is being accessed.
9394
// Return null to match user's expectations.
94-
return null;
95+
return null; // @phpstan-ignore-line
9596
}
9697

9798
/**
98-
* @return Data
99+
* @return Shape
99100
*/
100101
public function toArray(): array
101102
{
@@ -104,6 +105,8 @@ public function toArray(): array
104105

105106
/**
106107
* @internal
108+
*
109+
* @param key-of<Shape> $offset
107110
*/
108111
public function offsetExists(mixed $offset): bool
109112
{
@@ -130,26 +133,32 @@ public function offsetExists(mixed $offset): bool
130133

131134
/**
132135
* @internal
136+
*
137+
* @param key-of<Shape> $offset
138+
*
139+
* @return value-of<Shape>
133140
*/
134141
public function &offsetGet(mixed $offset): mixed
135142
{
136143
if (!is_string($offset)) { // @phpstan-ignore-line
137144
throw new \InvalidArgumentException;
138145
}
139146

140-
if (!$this->offsetExists($offset)) {
141-
return null;
147+
if (!$this->offsetExists($offset)) { // @phpstan-ignore-line
148+
return null; // @phpstan-ignore-line
142149
}
143150

144151
if (array_key_exists($offset, array: $this->_data)) {
145-
return $this->_data[$offset];
152+
return $this->_data[$offset]; // @phpstan-ignore-line
146153
}
147154

148-
return $this->{$offset};
155+
return $this->{$offset}; // @phpstan-ignore-line
149156
}
150157

151158
/**
152159
* @internal
160+
*
161+
* @param key-of<Shape> $offset
153162
*/
154163
public function offsetSet(mixed $offset, mixed $value): void
155164
{
@@ -163,9 +172,9 @@ public function offsetSet(mixed $offset, mixed $value): void
163172

164173
$coerced = Conversion::coerce($type, value: $value, state: new CoerceState(translateNames: false));
165174

166-
if (property_exists($this, property: $offset)) {
175+
if (property_exists($this, property: $offset)) { // @phpstan-ignore-line
167176
try {
168-
$this->{$offset} = $coerced;
177+
$this->{$offset} = $coerced; // @phpstan-ignore-line
169178
unset($this->_data[$offset]);
170179

171180
return;
@@ -179,14 +188,16 @@ public function offsetSet(mixed $offset, mixed $value): void
179188

180189
/**
181190
* @internal
191+
*
192+
* @param key-of<Shape> $offset
182193
*/
183194
public function offsetUnset(mixed $offset): void
184195
{
185196
if (!is_string($offset)) { // @phpstan-ignore-line
186197
throw new \InvalidArgumentException;
187198
}
188199

189-
if (property_exists($this, property: $offset)) {
200+
if (property_exists($this, property: $offset)) { // @phpstan-ignore-line
190201
unset($this->{$offset});
191202
}
192203

@@ -205,7 +216,7 @@ public function jsonSerialize(): array
205216
/**
206217
* @param array<string, mixed> $data
207218
*/
208-
public static function fromArray(array $data): self
219+
public static function fromArray(array $data): static
209220
{
210221
return self::converter()->from($data); // @phpstan-ignore-line
211222
}
@@ -249,7 +260,7 @@ private function unsetOptionalProperties(): void
249260
*/
250261
private static function serialize(mixed $value): mixed
251262
{
252-
if ($value instanceof BaseModel || $value instanceof BasePage) {
263+
if ($value instanceof BaseModel) {
253264
return $value->toArray();
254265
}
255266

src/Core/Concerns/SdkPage.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ public function pagingEachItem(): \Generator
100100
}
101101
}
102102

103+
/**
104+
* @param array<string, mixed> $data
105+
*
106+
* @return static<Item>
107+
*/
108+
abstract public static function fromArray(array $data): static;
109+
103110
/**
104111
* @return array{normalized_request, RequestOptions}
105112
*/

src/Core/Contracts/BasePage.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
*
1515
* @template Item
1616
*
17-
* @extends \ArrayAccess<string, mixed>
1817
* @extends \IteratorAggregate<int, static>
1918
*/
20-
interface BasePage extends \ArrayAccess, \JsonSerializable, \Stringable, \IteratorAggregate
19+
interface BasePage extends \IteratorAggregate
2120
{
2221
/**
2322
* @internal
@@ -32,16 +31,6 @@ public function __construct(
3231
mixed $data,
3332
);
3433

35-
/**
36-
* @param array<string, mixed> $data
37-
*
38-
* @return static<Item>
39-
*/
40-
public static function fromArray(array $data): self;
41-
42-
/** @return array<string, mixed> */
43-
public function toArray(): array;
44-
4534
public function hasNextPage(): bool;
4635

4736
/**

src/Core/Conversion/ModelOf.php

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

77
use CasParser\Core\Attributes\Api;
88
use CasParser\Core\Contracts\BaseModel;
9-
use CasParser\Core\Contracts\BasePage;
109
use CasParser\Core\Conversion;
1110
use CasParser\Core\Conversion\Contracts\Converter;
1211

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

2322
/**
24-
* @param \ReflectionClass<BaseModel|BasePage<mixed>> $class
23+
* @param \ReflectionClass<BaseModel> $class
2524
*/
2625
public function __construct(public readonly \ReflectionClass $class)
2726
{
@@ -89,15 +88,13 @@ public function coerce(mixed $value, CoerceState $state): mixed
8988
$acc[$name] = $item;
9089
}
9190

92-
return $this->from($acc);
91+
return $this->from($acc); // @phpstan-ignore-line
9392
}
9493

9594
/**
96-
* @param array<mixed> $data
97-
*
98-
* @return BaseModel|BasePage<mixed>
95+
* @param array<string, mixed> $data
9996
*/
100-
public function from(array $data): BaseModel|BasePage
97+
public function from(array $data): BaseModel
10198
{
10299
$instance = $this->class->newInstanceWithoutConstructor();
103100
$instance->__unserialize($data); // @phpstan-ignore-line
@@ -107,7 +104,7 @@ public function from(array $data): BaseModel|BasePage
107104

108105
public function dump(mixed $value, DumpState $state): mixed
109106
{
110-
if ($value instanceof BaseModel || $value instanceof BasePage) {
107+
if ($value instanceof BaseModel) {
111108
$value = $value->toArray();
112109
}
113110

0 commit comments

Comments
 (0)