@@ -249,32 +249,19 @@ public function toArray(): array
249
249
*/
250
250
public static function fromArray (array $ array ): MessagePart
251
251
{
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 ' ])) {
258
254
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 ' ]));
271
261
} 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
+ );
278
265
}
279
266
}
280
267
}
0 commit comments