Skip to content

Commit 3e36f0f

Browse files
committed
refactor: simplifies Fiel and MessagePart toArray
1 parent 2811597 commit 3e36f0f

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/Files/DTO/File.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,12 @@ public function toArray(): array
401401
'mimeType' => $this->getMimeType(),
402402
];
403403

404-
if ($this->fileType->isRemote() && $this->url !== null) {
404+
if ($this->url !== null) {
405405
$data['url'] = $this->url;
406406
} elseif (!$this->fileType->isRemote() && $this->base64Data !== null) {
407407
$data['base64Data'] = $this->base64Data;
408+
} else {
409+
throw new RuntimeException('File requires either url or base64Data. This should not be a possible condition.');
408410
}
409411

410412
return $data;

src/Messages/DTO/MessagePart.php

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

55
namespace WordPress\AiClient\Messages\DTO;
66

7+
use InvalidArgumentException;
8+
use RuntimeException;
79
use WordPress\AiClient\Common\AbstractDataValueObject;
810
use WordPress\AiClient\Files\DTO\File;
911
use WordPress\AiClient\Messages\Enums\MessagePartTypeEnum;
@@ -228,14 +230,16 @@ public function toArray(): array
228230
{
229231
$data = ['type' => $this->type->value];
230232

231-
if ($this->type->isText() && $this->text !== null) {
233+
if ($this->text !== null) {
232234
$data['text'] = $this->text;
233-
} elseif ($this->type->isFile() && $this->file !== null) {
235+
} elseif ($this->file !== null) {
234236
$data['file'] = $this->file->toArray();
235-
} elseif ($this->type->isFunctionCall() && $this->functionCall !== null) {
237+
} elseif ($this->functionCall !== null) {
236238
$data['functionCall'] = $this->functionCall->toArray();
237-
} elseif ($this->type->isFunctionResponse() && $this->functionResponse !== null) {
239+
} elseif ($this->functionResponse !== null) {
238240
$data['functionResponse'] = $this->functionResponse->toArray();
241+
} else {
242+
throw new RuntimeException('MessagePart requires one of: text, file, functionCall, or functionResponse. This should not be a possible condition.');
239243
}
240244

241245
return $data;

0 commit comments

Comments
 (0)