Skip to content

Commit a408659

Browse files
AstritAerdemkose
authored andcommitted
ImagePart extend FilePart to keep compatibility and add content functions
1 parent f4cd5a1 commit a408659

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

src/Resources/Content.php

+37-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use GeminiAPI\Enums\MimeType;
88
use GeminiAPI\Enums\Role;
9+
use GeminiAPI\Resources\Parts\FilePart;
910
use GeminiAPI\Traits\ArrayTypeValidator;
1011
use GeminiAPI\Resources\Parts\ImagePart;
1112
use GeminiAPI\Resources\Parts\PartInterface;
@@ -40,6 +41,13 @@ public function addImage(MimeType $mimeType, string $image): self
4041
return $this;
4142
}
4243

44+
public function addFile(MimeType $mimeType, string $file): self
45+
{
46+
$this->parts[] = new FilePart($mimeType, $file);
47+
48+
return $this;
49+
}
50+
4351
public static function text(
4452
string $text,
4553
Role $role = Role::User,
@@ -65,6 +73,19 @@ public static function image(
6573
);
6674
}
6775

76+
public static function file(
77+
MimeType $mimeType,
78+
string $file,
79+
Role $role = Role::User
80+
): self {
81+
return new self(
82+
[
83+
new FilePart($mimeType, $file),
84+
],
85+
$role,
86+
);
87+
}
88+
6889
public static function textAndImage(
6990
string $text,
7091
MimeType $mimeType,
@@ -80,6 +101,21 @@ public static function textAndImage(
80101
);
81102
}
82103

104+
public static function textAndFile(
105+
string $text,
106+
MimeType $mimeType,
107+
string $file,
108+
Role $role = Role::User,
109+
): self {
110+
return new self(
111+
[
112+
new TextPart($text),
113+
new FilePart($mimeType, $file),
114+
],
115+
$role,
116+
);
117+
}
118+
83119
/**
84120
* @param array{
85121
* parts: array<int, array{text?: string, inlineData?: array{mimeType: string, data: string}}>,
@@ -97,7 +133,7 @@ public static function fromArray(array $content): self
97133

98134
if (!empty($part['inlineData'])) {
99135
$mimeType = MimeType::from($part['inlineData']['mimeType']);
100-
$parts[] = new ImagePart($mimeType, $part['inlineData']['data']);
136+
$parts[] = new FilePart($mimeType, $part['inlineData']['data']);
101137
}
102138
}
103139

src/Resources/Parts/ImagePart.php

+1-34
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,6 @@
44

55
namespace GeminiAPI\Resources\Parts;
66

7-
use GeminiAPI\Enums\MimeType;
8-
use JsonSerializable;
9-
10-
use function json_encode;
11-
12-
class ImagePart implements PartInterface, JsonSerializable
7+
class ImagePart extends FilePart
138
{
14-
public function __construct(
15-
public readonly MimeType $mimeType,
16-
public readonly string $data,
17-
) {
18-
}
19-
20-
/**
21-
* @return array{
22-
* inlineData: array{
23-
* mimeType: string,
24-
* data: string,
25-
* },
26-
* }
27-
*/
28-
public function jsonSerialize(): array
29-
{
30-
return [
31-
'inlineData' => [
32-
'mimeType' => $this->mimeType->value,
33-
'data' => $this->data,
34-
],
35-
];
36-
}
37-
38-
public function __toString(): string
39-
{
40-
return json_encode($this) ?: '';
41-
}
429
}

0 commit comments

Comments
 (0)