Skip to content

Commit 3a56ff2

Browse files
Merge branch 'dto-serlialization' into extender-dtos
2 parents 4342636 + 87d9c67 commit 3a56ff2

File tree

11 files changed

+61
-13
lines changed

11 files changed

+61
-13
lines changed

src/Common/AbstractDataValueObject.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WordPress\AiClient\Common;
66

7+
use InvalidArgumentException;
78
use JsonSerializable;
89
use stdClass;
910
use WordPress\AiClient\Common\Contracts\WithArrayTransformationInterface;
@@ -31,6 +32,36 @@ abstract class AbstractDataValueObject implements
3132
WithJsonSchemaInterface,
3233
JsonSerializable
3334
{
35+
/**
36+
* Validates that required keys exist in the array data.
37+
*
38+
* @since n.e.x.t
39+
*
40+
* @param TArrayShape $data The array data to validate.
41+
* @param string[] $requiredKeys The keys that must be present.
42+
* @throws InvalidArgumentException If any required key is missing.
43+
*/
44+
protected static function validateFromArrayData(array $data, array $requiredKeys): void
45+
{
46+
$missingKeys = [];
47+
48+
foreach ($requiredKeys as $key) {
49+
if (!isset($data[$key])) {
50+
$missingKeys[] = $key;
51+
}
52+
}
53+
54+
if (!empty($missingKeys)) {
55+
throw new InvalidArgumentException(
56+
sprintf(
57+
'%s::fromArray() missing required keys: %s',
58+
static::class,
59+
implode(', ', $missingKeys)
60+
)
61+
);
62+
}
63+
}
64+
3465
/**
3566
* Converts the object to a JSON-serializable format.
3667
*

src/Files/DTO/File.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ public function toArray(): array
423423
*/
424424
public static function fromArray(array $array): File
425425
{
426+
static::validateFromArrayData($array, ['fileType']);
427+
426428
// Check which properties are set to determine how to construct the File
427429
$mimeType = $array['mimeType'] ?? null;
428430

src/Messages/DTO/Message.php

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

55
namespace WordPress\AiClient\Messages\DTO;
66

7+
use InvalidArgumentException;
78
use WordPress\AiClient\Common\AbstractDataValueObject;
89
use WordPress\AiClient\Messages\Enums\MessageRoleEnum;
910

@@ -126,6 +127,8 @@ public function toArray(): array
126127
*/
127128
final public static function fromArray(array $array): Message
128129
{
130+
static::validateFromArrayData($array, ['role', 'parts']);
131+
129132
$role = MessageRoleEnum::from($array['role']);
130133
$partsData = $array['parts'];
131134
$parts = array_map(function (array $partData) {

src/Messages/DTO/MessagePart.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ public function toArray(): array
255255
*/
256256
public static function fromArray(array $array): MessagePart
257257
{
258+
static::validateFromArrayData($array, ['type']);
259+
258260
// Check which properties are set to determine how to construct the MessagePart
259261
if (isset($array['text'])) {
260262
return new self($array['text']);

src/Operations/DTO/GenerativeAiOperation.php

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

55
namespace WordPress\AiClient\Operations\DTO;
66

7+
use InvalidArgumentException;
78
use WordPress\AiClient\Common\AbstractDataValueObject;
89
use WordPress\AiClient\Operations\Contracts\OperationInterface;
910
use WordPress\AiClient\Operations\Enums\OperationStateEnum;
@@ -168,6 +169,8 @@ public function toArray(): array
168169
*/
169170
public static function fromArray(array $array): GenerativeAiOperation
170171
{
172+
static::validateFromArrayData($array, ['id', 'state']);
173+
171174
$state = OperationStateEnum::from($array['state']);
172175
$result = null;
173176
if (isset($array['result'])) {

src/Results/DTO/Candidate.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ public function toArray(): array
146146
*/
147147
public static function fromArray(array $array): Candidate
148148
{
149+
static::validateFromArrayData($array, ['message', 'finishReason', 'tokenCount']);
150+
149151
$messageData = $array['message'];
150152

151153
return new self(

src/Results/DTO/GenerativeAiResult.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ public function toArray(): array
417417
*/
418418
public static function fromArray(array $array): GenerativeAiResult
419419
{
420+
static::validateFromArrayData($array, ['id', 'candidates', 'tokenUsage']);
421+
420422
$candidates = array_map(fn(array $candidateData) => Candidate::fromArray($candidateData), $array['candidates']);
421423

422424
return new self(

src/Results/DTO/TokenUsage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ public function toArray(): array
141141
*/
142142
public static function fromArray(array $array): TokenUsage
143143
{
144+
static::validateFromArrayData($array, ['promptTokens', 'completionTokens', 'totalTokens']);
145+
144146
return new self(
145147
$array['promptTokens'],
146148
$array['completionTokens'],

src/Tools/DTO/FunctionDeclaration.php

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

55
namespace WordPress\AiClient\Tools\DTO;
66

7+
use InvalidArgumentException;
78
use WordPress\AiClient\Common\AbstractDataValueObject;
89

910
/**
@@ -142,6 +143,8 @@ public function toArray(): array
142143
*/
143144
public static function fromArray(array $array): FunctionDeclaration
144145
{
146+
static::validateFromArrayData($array, ['name', 'description']);
147+
145148
return new self(
146149
$array['name'],
147150
$array['description'],

src/Tools/DTO/FunctionResponse.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ public function toArray(): array
137137
*/
138138
public static function fromArray(array $array): FunctionResponse
139139
{
140+
static::validateFromArrayData($array, ['id', 'name', 'response']);
141+
140142
return new self(
141143
$array['id'],
142144
$array['name'],

0 commit comments

Comments
 (0)