Skip to content

Commit cf5b304

Browse files
committed
refactor: further cleaning up of types
1 parent f6a1605 commit cf5b304

17 files changed

+53
-88
lines changed

src/Files/DTO/File.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @implements WithJsonSerialization<FileJsonShape>
2828
*/
29-
class File implements WithJsonSchemaInterface, WithJsonSerialization
29+
final class File implements WithJsonSchemaInterface, WithJsonSerialization
3030
{
3131
/**
3232
* @var MimeType The MIME type of the file.
@@ -393,7 +393,7 @@ public static function getJsonSchema(): array
393393
*
394394
* @since n.e.x.t
395395
*
396-
* @return array<string, mixed>
396+
* @return FileJsonShape
397397
*/
398398
public function jsonSerialize(): array
399399
{
@@ -402,9 +402,9 @@ public function jsonSerialize(): array
402402
'mimeType' => $this->getMimeType(),
403403
];
404404

405-
if ($this->fileType->isRemote()) {
405+
if ($this->fileType->isRemote() && $this->url !== null) {
406406
$data['url'] = $this->url;
407-
} else {
407+
} elseif (!$this->fileType->isRemote() && $this->base64Data !== null) {
408408
$data['base64Data'] = $this->base64Data;
409409
}
410410

src/Messages/DTO/Message.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function getJsonSchema(): array
106106
*
107107
* @since n.e.x.t
108108
*
109-
* @return array<string, mixed>
109+
* @return MessageJsonShape
110110
*/
111111
public function jsonSerialize(): array
112112
{
@@ -123,14 +123,27 @@ public function jsonSerialize(): array
123123
*
124124
* @since n.e.x.t
125125
*/
126-
public static function fromJson(array $json): Message
126+
final public static function fromJson(array $json): Message
127127
{
128128
$role = MessageRoleEnum::from($json['role']);
129129
$partsData = $json['parts'];
130130
$parts = array_map(function (array $partData) {
131131
return MessagePart::fromJson($partData);
132132
}, $partsData);
133133

134+
// Determine which concrete class to instantiate based on role
135+
if ($role->isUser()) {
136+
/** @phpstan-ignore-next-line */
137+
return new UserMessage($parts);
138+
} elseif ($role->isModel()) {
139+
/** @phpstan-ignore-next-line */
140+
return new ModelMessage($parts);
141+
} elseif ($role->isSystem()) {
142+
/** @phpstan-ignore-next-line */
143+
return new SystemMessage($parts);
144+
}
145+
146+
/** @phpstan-ignore-next-line */
134147
return new self($role, $parts);
135148
}
136149
}

src/Messages/DTO/MessagePart.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* @implements WithJsonSerialization<MessagePartJsonShape>
3535
*/
36-
class MessagePart implements WithJsonSchemaInterface, WithJsonSerialization
36+
final class MessagePart implements WithJsonSchemaInterface, WithJsonSerialization
3737
{
3838
/**
3939
* @var MessagePartTypeEnum The type of this message part.
@@ -223,7 +223,7 @@ public static function getJsonSchema(): array
223223
*
224224
* @since n.e.x.t
225225
*
226-
* @return array<string, mixed>
226+
* @return MessagePartJsonShape
227227
*/
228228
public function jsonSerialize(): array
229229
{

src/Messages/DTO/ModelMessage.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @since n.e.x.t
1616
*/
17-
class ModelMessage extends Message
17+
final class ModelMessage extends Message
1818
{
1919
/**
2020
* Constructor.
@@ -27,19 +27,4 @@ public function __construct(array $parts)
2727
{
2828
parent::__construct(MessageRoleEnum::model(), $parts);
2929
}
30-
31-
/**
32-
* {@inheritDoc}
33-
*
34-
* @since n.e.x.t
35-
*/
36-
public static function fromJson(array $json): ModelMessage
37-
{
38-
$partsData = $json['parts'];
39-
$parts = array_map(function (array $partData) {
40-
return MessagePart::fromJson($partData);
41-
}, $partsData);
42-
43-
return new self($parts);
44-
}
4530
}

src/Messages/DTO/SystemMessage.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @since n.e.x.t
1616
*/
17-
class SystemMessage extends Message
17+
final class SystemMessage extends Message
1818
{
1919
/**
2020
* Constructor.
@@ -27,19 +27,4 @@ public function __construct(array $parts)
2727
{
2828
parent::__construct(MessageRoleEnum::system(), $parts);
2929
}
30-
31-
/**
32-
* {@inheritDoc}
33-
*
34-
* @since n.e.x.t
35-
*/
36-
public static function fromJson(array $json): SystemMessage
37-
{
38-
$partsData = $json['parts'];
39-
$parts = array_map(function (array $partData) {
40-
return MessagePart::fromJson($partData);
41-
}, $partsData);
42-
43-
return new self($parts);
44-
}
4530
}

src/Messages/DTO/UserMessage.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @since n.e.x.t
1515
*/
16-
class UserMessage extends Message
16+
final class UserMessage extends Message
1717
{
1818
/**
1919
* Constructor.
@@ -26,19 +26,4 @@ public function __construct(array $parts)
2626
{
2727
parent::__construct(MessageRoleEnum::user(), $parts);
2828
}
29-
30-
/**
31-
* {@inheritDoc}
32-
*
33-
* @since n.e.x.t
34-
*/
35-
public static function fromJson(array $json): UserMessage
36-
{
37-
$partsData = $json['parts'];
38-
$parts = array_map(function (array $partData) {
39-
return MessagePart::fromJson($partData);
40-
}, $partsData);
41-
42-
return new self($parts);
43-
}
4429
}

src/Operations/Contracts/OperationInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*
1717
* @since n.e.x.t
1818
*
19-
* @extends WithJsonSerialization<array<string, mixed>>
19+
* @template TJsonShape of array<string, mixed>
20+
*
21+
* @extends WithJsonSerialization<TJsonShape>
2022
*/
2123
interface OperationInterface extends WithJsonSchemaInterface, WithJsonSerialization
2224
{

src/Operations/DTO/GenerativeAiOperation.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
*
2121
* @phpstan-type GenerativeAiOperationJsonShape array{id: string, state: string, result?: GenerativeAiResultJsonShape}
2222
*
23-
* @implements WithJsonSerialization<GenerativeAiOperationJsonShape>
23+
* @implements OperationInterface<GenerativeAiOperationJsonShape>
2424
*/
25-
class GenerativeAiOperation implements OperationInterface
25+
final class GenerativeAiOperation implements OperationInterface
2626
{
2727
/**
2828
* @var string Unique identifier for this operation.
@@ -144,7 +144,7 @@ public static function getJsonSchema(): array
144144
*
145145
* @since n.e.x.t
146146
*
147-
* @return array<string, mixed>
147+
* @return GenerativeAiOperationJsonShape
148148
*/
149149
public function jsonSerialize(): array
150150
{
@@ -170,8 +170,7 @@ public static function fromJson(array $json): GenerativeAiOperation
170170
$state = OperationStateEnum::from($json['state']);
171171
$result = null;
172172
if (isset($json['result'])) {
173-
$resultData = $json['result'];
174-
$result = GenerativeAiResult::fromJson($resultData);
173+
$result = GenerativeAiResult::fromJson($json['result']);
175174
}
176175

177176
return new self($json['id'], $state, $result);

src/Results/Contracts/ResultInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*
1717
* @since n.e.x.t
1818
*
19-
* @extends WithJsonSerialization<array<string, mixed>>
19+
* @template TJsonShape of array<string, mixed>
20+
*
21+
* @extends WithJsonSerialization<TJsonShape>
2022
*/
2123
interface ResultInterface extends WithJsonSchemaInterface, WithJsonSerialization
2224
{

src/Results/DTO/Candidate.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @implements WithJsonSerialization<CandidateJsonShape>
2525
*/
26-
class Candidate implements WithJsonSchemaInterface, WithJsonSerialization
26+
final class Candidate implements WithJsonSchemaInterface, WithJsonSerialization
2727
{
2828
/**
2929
* @var Message The generated message.
@@ -128,7 +128,7 @@ public static function getJsonSchema(): array
128128
*
129129
* @since n.e.x.t
130130
*
131-
* @return array<string, mixed>
131+
* @return CandidateJsonShape
132132
*/
133133
public function jsonSerialize(): array
134134
{
@@ -151,7 +151,7 @@ public static function fromJson(array $json): Candidate
151151
return new self(
152152
Message::fromJson($messageData),
153153
FinishReasonEnum::from($json['finishReason']),
154-
$json['tokenCount']
154+
(int) $json['tokenCount']
155155
);
156156
}
157157
}

0 commit comments

Comments
 (0)