Skip to content

Commit 7c13ac1

Browse files
committed
refactor: removes type check from File and MessagePart fromArray
1 parent 0a5ed46 commit 7c13ac1

File tree

2 files changed

+18
-34
lines changed

2 files changed

+18
-34
lines changed

src/Files/DTO/File.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -419,18 +419,15 @@ public function toArray(): array
419419
*/
420420
public static function fromArray(array $array): File
421421
{
422-
$fileType = FileTypeEnum::from($array['fileType']);
422+
// Check which properties are set to determine how to construct the File
423+
$mimeType = $array['mimeType'] ?? null;
423424

424-
if ($fileType->isRemote()) {
425-
if (!isset($array['url']) || !isset($array['mimeType'])) {
426-
throw new InvalidArgumentException('Remote file requires url and mimeType.');
427-
}
428-
return new self($array['url'], $array['mimeType']);
425+
if (isset($array['url'])) {
426+
return new self($array['url'], $mimeType);
427+
} elseif (isset($array['base64Data'])) {
428+
return new self($array['base64Data'], $mimeType);
429429
} else {
430-
if (!isset($array['mimeType']) || !isset($array['base64Data'])) {
431-
throw new InvalidArgumentException('Inline file requires mimeType and base64Data.');
432-
}
433-
return new self($array['base64Data'], $array['mimeType']);
430+
throw new InvalidArgumentException('File requires either url or base64Data.');
434431
}
435432
}
436433
}

src/Messages/DTO/MessagePart.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -249,32 +249,19 @@ public function toArray(): array
249249
*/
250250
public static function fromArray(array $array): MessagePart
251251
{
252-
$type = MessagePartTypeEnum::from($array['type']);
253-
254-
if ($type->isText()) {
255-
if (!isset($array['text'])) {
256-
throw new InvalidArgumentException('Text message part requires text field.');
257-
}
252+
// Check which properties are set to determine how to construct the MessagePart
253+
if (isset($array['text'])) {
258254
return new self($array['text']);
259-
} elseif ($type->isFile()) {
260-
if (!isset($array['file'])) {
261-
throw new InvalidArgumentException('File message part requires file field.');
262-
}
263-
$fileData = $array['file'];
264-
return new self(File::fromArray($fileData));
265-
} elseif ($type->isFunctionCall()) {
266-
if (!isset($array['functionCall'])) {
267-
throw new InvalidArgumentException('Function call message part requires functionCall field.');
268-
}
269-
$functionCallData = $array['functionCall'];
270-
return new self(FunctionCall::fromArray($functionCallData));
255+
} elseif (isset($array['file'])) {
256+
return new self(File::fromArray($array['file']));
257+
} elseif (isset($array['functionCall'])) {
258+
return new self(FunctionCall::fromArray($array['functionCall']));
259+
} elseif (isset($array['functionResponse'])) {
260+
return new self(FunctionResponse::fromArray($array['functionResponse']));
271261
} else {
272-
// Function response is the only remaining option
273-
if (!isset($array['functionResponse'])) {
274-
throw new InvalidArgumentException('Function response message part requires functionResponse field.');
275-
}
276-
$functionResponseData = $array['functionResponse'];
277-
return new self(FunctionResponse::fromArray($functionResponseData));
262+
throw new InvalidArgumentException(
263+
'MessagePart requires one of: text, file, functionCall, or functionResponse.'
264+
);
278265
}
279266
}
280267
}

0 commit comments

Comments
 (0)