Skip to content

Commit 18ffdb4

Browse files
committed
refactor: changes to array terminology over json
1 parent 404699a commit 18ffdb4

33 files changed

+568
-581
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Common\Contracts;
6+
7+
/**
8+
* Interface for objects that support array transformation.
9+
*
10+
* @since 1.0.0
11+
*
12+
* @template TArrayShape of array<string, mixed>
13+
*/
14+
interface WithArrayTransformationInterface
15+
{
16+
/**
17+
* Converts the object to an array representation.
18+
*
19+
* @since 1.0.0
20+
*
21+
* @return TArrayShape The array representation.
22+
*/
23+
public function toArray(): array;
24+
25+
/**
26+
* Creates an instance from array data.
27+
*
28+
* @since 1.0.0
29+
*
30+
* @param TArrayShape $array The array data.
31+
* @return static The created instance.
32+
*/
33+
public static function fromArray(array $array);
34+
}

src/Common/Contracts/WithJsonSerialization.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Files/DTO/File.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace WordPress\AiClient\Files\DTO;
66

77
use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
8-
use WordPress\AiClient\Common\Contracts\WithJsonSerialization;
8+
use WordPress\AiClient\Common\Contracts\WithArrayTransformationInterface;
99
use WordPress\AiClient\Files\Enums\FileTypeEnum;
1010
use WordPress\AiClient\Files\ValueObjects\MimeType;
1111

@@ -17,16 +17,16 @@
1717
*
1818
* @since n.e.x.t
1919
*
20-
* @phpstan-type FileJsonShape array{
20+
* @phpstan-type FileArrayShape array{
2121
* fileType: string,
2222
* url?: string,
2323
* mimeType?: string,
2424
* base64Data?: string
2525
* }
2626
*
27-
* @implements WithJsonSerialization<FileJsonShape>
27+
* @implements WithArrayTransformationInterface<FileArrayShape>
2828
*/
29-
final class File implements WithJsonSchemaInterface, WithJsonSerialization
29+
final class File implements WithJsonSchemaInterface, WithArrayTransformationInterface
3030
{
3131
/**
3232
* @var MimeType The MIME type of the file.
@@ -393,9 +393,9 @@ public static function getJsonSchema(): array
393393
*
394394
* @since n.e.x.t
395395
*
396-
* @return FileJsonShape
396+
* @return FileArrayShape
397397
*/
398-
public function jsonSerialize(): array
398+
public function toArray(): array
399399
{
400400
$data = [
401401
'fileType' => $this->fileType->value,
@@ -416,22 +416,22 @@ public function jsonSerialize(): array
416416
*
417417
* @since n.e.x.t
418418
*/
419-
public static function fromJson(array $json): File
419+
public static function fromArray(array $array): File
420420
{
421-
$fileType = FileTypeEnum::from($json['fileType']);
421+
$fileType = FileTypeEnum::from($array['fileType']);
422422

423423
if ($fileType->isRemote()) {
424-
if (!isset($json['url']) || !isset($json['mimeType'])) {
424+
if (!isset($array['url']) || !isset($array['mimeType'])) {
425425
throw new \InvalidArgumentException('Remote file requires url and mimeType.');
426426
}
427-
return new self($json['url'], $json['mimeType']);
427+
return new self($array['url'], $array['mimeType']);
428428
} else {
429-
if (!isset($json['mimeType']) || !isset($json['base64Data'])) {
429+
if (!isset($array['mimeType']) || !isset($array['base64Data'])) {
430430
throw new \InvalidArgumentException('Inline file requires mimeType and base64Data.');
431431
}
432432
// Create data URI from base64 data and mime type
433-
$mimeType = $json['mimeType'];
434-
$base64Data = $json['base64Data'];
433+
$mimeType = $array['mimeType'];
434+
$base64Data = $array['base64Data'];
435435
$dataUri = sprintf('data:%s;base64,%s', $mimeType, $base64Data);
436436
return new self($dataUri);
437437
}

src/Messages/DTO/Message.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace WordPress\AiClient\Messages\DTO;
66

77
use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
8-
use WordPress\AiClient\Common\Contracts\WithJsonSerialization;
8+
use WordPress\AiClient\Common\Contracts\WithArrayTransformationInterface;
99
use WordPress\AiClient\Messages\Enums\MessageRoleEnum;
1010

1111
/**
@@ -16,16 +16,16 @@
1616
*
1717
* @since n.e.x.t
1818
*
19-
* @phpstan-import-type MessagePartJsonShape from MessagePart
19+
* @phpstan-import-type MessagePartArrayShape from MessagePart
2020
*
21-
* @phpstan-type MessageJsonShape array{
21+
* @phpstan-type MessageArrayShape array{
2222
* role: string,
23-
* parts: array<MessagePartJsonShape>
23+
* parts: array<MessagePartArrayShape>
2424
* }
2525
*
26-
* @implements WithJsonSerialization<MessageJsonShape>
26+
* @implements WithArrayTransformationInterface<MessageArrayShape>
2727
*/
28-
class Message implements WithJsonSchemaInterface, WithJsonSerialization
28+
class Message implements WithJsonSchemaInterface, WithArrayTransformationInterface
2929
{
3030
/**
3131
* @var MessageRoleEnum The role of the message sender.
@@ -106,14 +106,14 @@ public static function getJsonSchema(): array
106106
*
107107
* @since n.e.x.t
108108
*
109-
* @return MessageJsonShape
109+
* @return MessageArrayShape
110110
*/
111-
public function jsonSerialize(): array
111+
public function toArray(): array
112112
{
113113
return [
114114
'role' => $this->role->value,
115115
'parts' => array_map(function (MessagePart $part) {
116-
return $part->jsonSerialize();
116+
return $part->toArray();
117117
}, $this->parts),
118118
];
119119
}
@@ -125,12 +125,12 @@ public function jsonSerialize(): array
125125
*
126126
* @return self|UserMessage|ModelMessage|SystemMessage
127127
*/
128-
final public static function fromJson(array $json): Message
128+
final public static function fromArray(array $array): Message
129129
{
130-
$role = MessageRoleEnum::from($json['role']);
131-
$partsData = $json['parts'];
130+
$role = MessageRoleEnum::from($array['role']);
131+
$partsData = $array['parts'];
132132
$parts = array_map(function (array $partData) {
133-
return MessagePart::fromJson($partData);
133+
return MessagePart::fromArray($partData);
134134
}, $partsData);
135135

136136
// Determine which concrete class to instantiate based on role

src/Messages/DTO/MessagePart.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace WordPress\AiClient\Messages\DTO;
66

77
use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
8-
use WordPress\AiClient\Common\Contracts\WithJsonSerialization;
8+
use WordPress\AiClient\Common\Contracts\WithArrayTransformationInterface;
99
use WordPress\AiClient\Files\DTO\File;
1010
use WordPress\AiClient\Messages\Enums\MessagePartTypeEnum;
1111
use WordPress\AiClient\Tools\DTO\FunctionCall;
@@ -19,21 +19,21 @@
1919
*
2020
* @since n.e.x.t
2121
*
22-
* @phpstan-import-type FileJsonShape from File
23-
* @phpstan-import-type FunctionCallJsonShape from FunctionCall
24-
* @phpstan-import-type FunctionResponseJsonShape from FunctionResponse
22+
* @phpstan-import-type FileArrayShape from File
23+
* @phpstan-import-type FunctionCallArrayShape from FunctionCall
24+
* @phpstan-import-type FunctionResponseArrayShape from FunctionResponse
2525
*
26-
* @phpstan-type MessagePartJsonShape array{
26+
* @phpstan-type MessagePartArrayShape array{
2727
* type: string,
2828
* text?: string,
29-
* file?: FileJsonShape,
30-
* functionCall?: FunctionCallJsonShape,
31-
* functionResponse?: FunctionResponseJsonShape
29+
* file?: FileArrayShape,
30+
* functionCall?: FunctionCallArrayShape,
31+
* functionResponse?: FunctionResponseArrayShape
3232
* }
3333
*
34-
* @implements WithJsonSerialization<MessagePartJsonShape>
34+
* @implements WithArrayTransformationInterface<MessagePartArrayShape>
3535
*/
36-
final class MessagePart implements WithJsonSchemaInterface, WithJsonSerialization
36+
final class MessagePart implements WithJsonSchemaInterface, WithArrayTransformationInterface
3737
{
3838
/**
3939
* @var MessagePartTypeEnum The type of this message part.
@@ -223,20 +223,20 @@ public static function getJsonSchema(): array
223223
*
224224
* @since n.e.x.t
225225
*
226-
* @return MessagePartJsonShape
226+
* @return MessagePartArrayShape
227227
*/
228-
public function jsonSerialize(): array
228+
public function toArray(): array
229229
{
230230
$data = ['type' => $this->type->value];
231231

232232
if ($this->type->isText() && $this->text !== null) {
233233
$data['text'] = $this->text;
234234
} elseif ($this->type->isFile() && $this->file !== null) {
235-
$data['file'] = $this->file->jsonSerialize();
235+
$data['file'] = $this->file->toArray();
236236
} elseif ($this->type->isFunctionCall() && $this->functionCall !== null) {
237-
$data['functionCall'] = $this->functionCall->jsonSerialize();
237+
$data['functionCall'] = $this->functionCall->toArray();
238238
} elseif ($this->type->isFunctionResponse() && $this->functionResponse !== null) {
239-
$data['functionResponse'] = $this->functionResponse->jsonSerialize();
239+
$data['functionResponse'] = $this->functionResponse->toArray();
240240
}
241241

242242
return $data;
@@ -247,35 +247,35 @@ public function jsonSerialize(): array
247247
*
248248
* @since n.e.x.t
249249
*/
250-
public static function fromJson(array $json): MessagePart
250+
public static function fromArray(array $array): MessagePart
251251
{
252-
$type = MessagePartTypeEnum::from($json['type']);
252+
$type = MessagePartTypeEnum::from($array['type']);
253253

254254
if ($type->isText()) {
255-
if (!isset($json['text'])) {
255+
if (!isset($array['text'])) {
256256
throw new \InvalidArgumentException('Text message part requires text field.');
257257
}
258-
return new self($json['text']);
258+
return new self($array['text']);
259259
} elseif ($type->isFile()) {
260-
if (!isset($json['file'])) {
260+
if (!isset($array['file'])) {
261261
throw new \InvalidArgumentException('File message part requires file field.');
262262
}
263-
$fileData = $json['file'];
264-
return new self(File::fromJson($fileData));
263+
$fileData = $array['file'];
264+
return new self(File::fromArray($fileData));
265265
} elseif ($type->isFunctionCall()) {
266-
if (!isset($json['functionCall'])) {
266+
if (!isset($array['functionCall'])) {
267267
throw new \InvalidArgumentException('Function call message part requires functionCall field.');
268268
}
269-
$functionCallData = $json['functionCall'];
270-
return new self(FunctionCall::fromJson($functionCallData));
269+
$functionCallData = $array['functionCall'];
270+
return new self(FunctionCall::fromArray($functionCallData));
271271
} elseif ($type->isFunctionResponse()) {
272-
if (!isset($json['functionResponse'])) {
272+
if (!isset($array['functionResponse'])) {
273273
throw new \InvalidArgumentException('Function response message part requires functionResponse field.');
274274
}
275-
$functionResponseData = $json['functionResponse'];
276-
return new self(FunctionResponse::fromJson($functionResponseData));
275+
$functionResponseData = $array['functionResponse'];
276+
return new self(FunctionResponse::fromArray($functionResponseData));
277277
}
278278

279-
throw new \InvalidArgumentException(sprintf('Unknown message part type: %s', $json['type']));
279+
throw new \InvalidArgumentException(sprintf('Unknown message part type: %s', $array['type']));
280280
}
281281
}

src/Operations/Contracts/OperationInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace WordPress\AiClient\Operations\Contracts;
66

77
use WordPress\AiClient\Common\Contracts\WithJsonSchemaInterface;
8-
use WordPress\AiClient\Common\Contracts\WithJsonSerialization;
8+
use WordPress\AiClient\Common\Contracts\WithArrayTransformationInterface;
99
use WordPress\AiClient\Operations\Enums\OperationStateEnum;
1010

1111
/**
@@ -16,11 +16,11 @@
1616
*
1717
* @since n.e.x.t
1818
*
19-
* @template TJsonShape of array<string, mixed>
19+
* @template TArrayShape of array<string, mixed>
2020
*
21-
* @extends WithJsonSerialization<TJsonShape>
21+
* @extends WithArrayTransformationInterface<TArrayShape>
2222
*/
23-
interface OperationInterface extends WithJsonSchemaInterface, WithJsonSerialization
23+
interface OperationInterface extends WithJsonSchemaInterface, WithArrayTransformationInterface
2424
{
2525
/**
2626
* Gets the operation ID.

src/Operations/DTO/GenerativeAiOperation.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
*
1717
* @since n.e.x.t
1818
*
19-
* @phpstan-import-type GenerativeAiResultJsonShape from GenerativeAiResult
19+
* @phpstan-import-type GenerativeAiResultArrayShape from GenerativeAiResult
2020
*
21-
* @phpstan-type GenerativeAiOperationJsonShape array{id: string, state: string, result?: GenerativeAiResultJsonShape}
21+
* @phpstan-type GenerativeAiOperationArrayShape array{id: string, state: string, result?: GenerativeAiResultArrayShape}
2222
*
23-
* @implements OperationInterface<GenerativeAiOperationJsonShape>
23+
* @implements OperationInterface<GenerativeAiOperationArrayShape>
2424
*/
2525
final class GenerativeAiOperation implements OperationInterface
2626
{
@@ -144,17 +144,17 @@ public static function getJsonSchema(): array
144144
*
145145
* @since n.e.x.t
146146
*
147-
* @return GenerativeAiOperationJsonShape
147+
* @return GenerativeAiOperationArrayShape
148148
*/
149-
public function jsonSerialize(): array
149+
public function toArray(): array
150150
{
151151
$data = [
152152
'id' => $this->id,
153153
'state' => $this->state->value,
154154
];
155155

156156
if ($this->result !== null) {
157-
$data['result'] = $this->result->jsonSerialize();
157+
$data['result'] = $this->result->toArray();
158158
}
159159

160160
return $data;
@@ -165,14 +165,14 @@ public function jsonSerialize(): array
165165
*
166166
* @since n.e.x.t
167167
*/
168-
public static function fromJson(array $json): GenerativeAiOperation
168+
public static function fromArray(array $array): GenerativeAiOperation
169169
{
170-
$state = OperationStateEnum::from($json['state']);
170+
$state = OperationStateEnum::from($array['state']);
171171
$result = null;
172-
if (isset($json['result'])) {
173-
$result = GenerativeAiResult::fromJson($json['result']);
172+
if (isset($array['result'])) {
173+
$result = GenerativeAiResult::fromArray($array['result']);
174174
}
175175

176-
return new self($json['id'], $state, $result);
176+
return new self($array['id'], $state, $result);
177177
}
178178
}

0 commit comments

Comments
 (0)