Skip to content

Commit d2e23b5

Browse files
authored
Merge pull request #31 from WordPress/extender-dtos
Adds Extender API DTOs
2 parents b0976a0 + fb76c99 commit d2e23b5

30 files changed

+4707
-57
lines changed

src/Common/AbstractEnum.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use BadMethodCallException;
88
use InvalidArgumentException;
99
use ReflectionClass;
10+
use RuntimeException;
1011

1112
/**
1213
* Abstract base class for enum-like behavior in PHP 7.4.
@@ -250,7 +251,7 @@ private static function getInstance(string $value, string $name): self
250251
* @since n.e.x.t
251252
*
252253
* @return array<string, string> Map of constant names to values.
253-
* @throws \RuntimeException If invalid constant found.
254+
* @throws RuntimeException If invalid constant found.
254255
*/
255256
final protected static function getConstants(): array
256257
{
@@ -265,7 +266,7 @@ final protected static function getConstants(): array
265266
foreach ($constants as $name => $value) {
266267
// Check if constant name follows uppercase snake_case pattern
267268
if (!preg_match('/^[A-Z][A-Z0-9_]*$/', $name)) {
268-
throw new \RuntimeException(
269+
throw new RuntimeException(
269270
sprintf(
270271
'Invalid enum constant name "%s" in %s. Constants must be UPPER_SNAKE_CASE.',
271272
$name,
@@ -276,7 +277,7 @@ final protected static function getConstants(): array
276277

277278
// Check if value is valid type
278279
if (!is_string($value)) {
279-
throw new \RuntimeException(
280+
throw new RuntimeException(
280281
sprintf(
281282
'Invalid enum value type for constant %s::%s. ' .
282283
'Only string values are allowed, %s given.',

src/Files/DTO/File.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class File extends AbstractDataValueObject
6060
*
6161
* @param string $file The file string (URL, base64 data, or local path).
6262
* @param string|null $mimeType The MIME type of the file (optional).
63-
* @throws \InvalidArgumentException If the file format is invalid or MIME type cannot be determined.
63+
* @throws InvalidArgumentException If the file format is invalid or MIME type cannot be determined.
6464
*/
6565
public function __construct(string $file, ?string $mimeType = null)
6666
{
@@ -75,7 +75,7 @@ public function __construct(string $file, ?string $mimeType = null)
7575
*
7676
* @param string $file The file string to process.
7777
* @param string|null $providedMimeType The explicitly provided MIME type.
78-
* @throws \InvalidArgumentException If the file format is invalid or MIME type cannot be determined.
78+
* @throws InvalidArgumentException If the file format is invalid or MIME type cannot be determined.
7979
*/
8080
private function detectAndProcessFile(string $file, ?string $providedMimeType): void
8181
{
@@ -110,7 +110,7 @@ private function detectAndProcessFile(string $file, ?string $providedMimeType):
110110
// Check if it's plain base64
111111
if (preg_match('/^[A-Za-z0-9+\/]*={0,2}$/', $file)) {
112112
if ($providedMimeType === null) {
113-
throw new \InvalidArgumentException(
113+
throw new InvalidArgumentException(
114114
'MIME type is required when providing plain base64 data without data URI format.'
115115
);
116116
}
@@ -120,7 +120,7 @@ private function detectAndProcessFile(string $file, ?string $providedMimeType):
120120
return;
121121
}
122122

123-
throw new \InvalidArgumentException(
123+
throw new InvalidArgumentException(
124124
'Invalid file provided. Expected URL, base64 data, or valid local file path.'
125125
);
126126
}
@@ -146,14 +146,14 @@ private function isUrl(string $string): bool
146146
*
147147
* @param string $filePath The path to the local file.
148148
* @return string The base64-encoded file data.
149-
* @throws \RuntimeException If the file cannot be read.
149+
* @throws RuntimeException If the file cannot be read.
150150
*/
151151
private function convertFileToBase64(string $filePath): string
152152
{
153153
$fileContent = @file_get_contents($filePath);
154154

155155
if ($fileContent === false) {
156-
throw new \RuntimeException(
156+
throw new RuntimeException(
157157
sprintf('Unable to read file: %s', $filePath)
158158
);
159159
}
@@ -294,7 +294,7 @@ public function isText(): bool
294294
* @param string|null $extractedMimeType The MIME type extracted from data URI.
295295
* @param string|null $pathOrUrl The file path or URL to extract extension from.
296296
* @return MimeType The determined MIME type.
297-
* @throws \InvalidArgumentException If MIME type cannot be determined.
297+
* @throws InvalidArgumentException If MIME type cannot be determined.
298298
*/
299299
private function determineMimeType(
300300
?string $providedMimeType,
@@ -326,14 +326,14 @@ private function determineMimeType(
326326
if (!empty($extension)) {
327327
try {
328328
return MimeType::fromExtension($extension);
329-
} catch (\InvalidArgumentException $e) {
329+
} catch (InvalidArgumentException $e) {
330330
// Extension not recognized, continue to error
331331
unset($e);
332332
}
333333
}
334334
}
335335

336-
throw new \InvalidArgumentException(
336+
throw new InvalidArgumentException(
337337
'Unable to determine MIME type. Please provide it explicitly.'
338338
);
339339
}

src/Files/ValueObjects/MimeType.php

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

55
namespace WordPress\AiClient\Files\ValueObjects;
66

7+
use InvalidArgumentException;
8+
79
/**
810
* Value object representing a MIME type.
911
*
@@ -115,12 +117,12 @@ final class MimeType
115117
* @since n.e.x.t
116118
*
117119
* @param string $value The MIME type value.
118-
* @throws \InvalidArgumentException If the MIME type is invalid.
120+
* @throws InvalidArgumentException If the MIME type is invalid.
119121
*/
120122
public function __construct(string $value)
121123
{
122124
if (!self::isValid($value)) {
123-
throw new \InvalidArgumentException(
125+
throw new InvalidArgumentException(
124126
sprintf('Invalid MIME type: %s', $value)
125127
);
126128
}
@@ -135,14 +137,14 @@ public function __construct(string $value)
135137
*
136138
* @param string $extension The file extension (without the dot).
137139
* @return self The MimeType instance.
138-
* @throws \InvalidArgumentException If the extension is not recognized.
140+
* @throws InvalidArgumentException If the extension is not recognized.
139141
*/
140142
public static function fromExtension(string $extension): self
141143
{
142144
$extension = strtolower($extension);
143145

144146
if (!isset(self::$extensionMap[$extension])) {
145-
throw new \InvalidArgumentException(
147+
throw new InvalidArgumentException(
146148
sprintf('Unknown file extension: %s', $extension)
147149
);
148150
}
@@ -245,7 +247,7 @@ public function equals($other): bool
245247
return $this->value === strtolower($other);
246248
}
247249

248-
throw new \InvalidArgumentException(
250+
throw new InvalidArgumentException(
249251
sprintf('Invalid MIME type comparison: %s', gettype($other))
250252
);
251253
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\DTO;
6+
7+
use WordPress\AiClient\Common\AbstractDataValueObject;
8+
use WordPress\AiClient\Providers\Enums\ProviderTypeEnum;
9+
10+
/**
11+
* Represents metadata about an AI provider.
12+
*
13+
* This class contains information about an AI provider, including its
14+
* unique identifier, display name, and type (cloud, server, or client).
15+
*
16+
* @since n.e.x.t
17+
*
18+
* @phpstan-type ProviderMetadataArrayShape array{
19+
* id: string,
20+
* name: string,
21+
* type: string
22+
* }
23+
*
24+
* @extends AbstractDataValueObject<ProviderMetadataArrayShape>
25+
*/
26+
class ProviderMetadata extends AbstractDataValueObject
27+
{
28+
public const KEY_ID = 'id';
29+
public const KEY_NAME = 'name';
30+
public const KEY_TYPE = 'type';
31+
32+
/**
33+
* @var string The provider's unique identifier.
34+
*/
35+
protected string $id;
36+
37+
/**
38+
* @var string The provider's display name.
39+
*/
40+
protected string $name;
41+
42+
/**
43+
* @var ProviderTypeEnum The provider type.
44+
*/
45+
protected ProviderTypeEnum $type;
46+
47+
/**
48+
* Constructor.
49+
*
50+
* @since n.e.x.t
51+
*
52+
* @param string $id The provider's unique identifier.
53+
* @param string $name The provider's display name.
54+
* @param ProviderTypeEnum $type The provider type.
55+
*/
56+
public function __construct(string $id, string $name, ProviderTypeEnum $type)
57+
{
58+
$this->id = $id;
59+
$this->name = $name;
60+
$this->type = $type;
61+
}
62+
63+
/**
64+
* Gets the provider's unique identifier.
65+
*
66+
* @since n.e.x.t
67+
*
68+
* @return string The provider ID.
69+
*/
70+
public function getId(): string
71+
{
72+
return $this->id;
73+
}
74+
75+
/**
76+
* Gets the provider's display name.
77+
*
78+
* @since n.e.x.t
79+
*
80+
* @return string The provider name.
81+
*/
82+
public function getName(): string
83+
{
84+
return $this->name;
85+
}
86+
87+
/**
88+
* Gets the provider type.
89+
*
90+
* @since n.e.x.t
91+
*
92+
* @return ProviderTypeEnum The provider type.
93+
*/
94+
public function getType(): ProviderTypeEnum
95+
{
96+
return $this->type;
97+
}
98+
99+
/**
100+
* {@inheritDoc}
101+
*
102+
* @since n.e.x.t
103+
*/
104+
public static function getJsonSchema(): array
105+
{
106+
return [
107+
'type' => 'object',
108+
'properties' => [
109+
self::KEY_ID => [
110+
'type' => 'string',
111+
'description' => 'The provider\'s unique identifier.',
112+
],
113+
self::KEY_NAME => [
114+
'type' => 'string',
115+
'description' => 'The provider\'s display name.',
116+
],
117+
self::KEY_TYPE => [
118+
'type' => 'string',
119+
'enum' => ProviderTypeEnum::getValues(),
120+
'description' => 'The provider type (cloud, server, or client).',
121+
],
122+
],
123+
'required' => [self::KEY_ID, self::KEY_NAME, self::KEY_TYPE],
124+
];
125+
}
126+
127+
/**
128+
* {@inheritDoc}
129+
*
130+
* @since n.e.x.t
131+
*
132+
* @return ProviderMetadataArrayShape
133+
*/
134+
public function toArray(): array
135+
{
136+
return [
137+
self::KEY_ID => $this->id,
138+
self::KEY_NAME => $this->name,
139+
self::KEY_TYPE => $this->type->value,
140+
];
141+
}
142+
143+
/**
144+
* {@inheritDoc}
145+
*
146+
* @since n.e.x.t
147+
*/
148+
public static function fromArray(array $array): self
149+
{
150+
static::validateFromArrayData($array, [self::KEY_ID, self::KEY_NAME, self::KEY_TYPE]);
151+
152+
return new self(
153+
$array[self::KEY_ID],
154+
$array[self::KEY_NAME],
155+
ProviderTypeEnum::from($array[self::KEY_TYPE])
156+
);
157+
}
158+
}

0 commit comments

Comments
 (0)