Skip to content

Commit 00fd8dd

Browse files
Merge branch 'feature/initial-enums' into feature/unit-tests
2 parents 530a9f7 + 5cc061f commit 00fd8dd

File tree

10 files changed

+235
-212
lines changed

10 files changed

+235
-212
lines changed

src/Common/AbstractEnum.php

Lines changed: 81 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@
3535
abstract class AbstractEnum
3636
{
3737
/**
38-
* @var string|int The value of the enum instance
38+
* @var string The value of the enum instance
3939
*/
40-
private $value;
40+
private string $value;
4141

4242
/**
4343
* @var string The name of the enum constant
4444
*/
4545
private string $name;
4646

4747
/**
48-
* @var array<string, array<string, string|int>> Cache for reflection data
48+
* @var array<string, array<string, string>> Cache for reflection data
4949
*/
5050
private static array $cache = [];
5151

@@ -55,25 +55,27 @@ abstract class AbstractEnum
5555
private static array $instances = [];
5656

5757
/**
58-
* Constructor is private to ensure instances are created through static methods
58+
* Constructor is private to ensure instances are created through static methods.
5959
*
6060
* @since n.e.x.t
61-
* @param string|int $value The enum value
62-
* @param string $name The constant name
61+
*
62+
* @param string $value The enum value.
63+
* @param string $name The constant name.
6364
*/
64-
final private function __construct($value, string $name)
65+
final private function __construct(string $value, string $name)
6566
{
6667
$this->value = $value;
6768
$this->name = $name;
6869
}
6970

7071
/**
71-
* Magic getter to provide read-only access to properties
72+
* Provides read-only access to properties.
7273
*
7374
* @since n.e.x.t
74-
* @param string $property The property name
75-
* @return mixed
76-
* @throws BadMethodCallException If property doesn't exist
75+
*
76+
* @param string $property The property name.
77+
* @return mixed The property value.
78+
* @throws BadMethodCallException If property doesn't exist.
7779
*/
7880
final public function __get(string $property)
7981
{
@@ -87,12 +89,13 @@ final public function __get(string $property)
8789
}
8890

8991
/**
90-
* Magic setter to prevent property modification
92+
* Prevents property modification.
9193
*
9294
* @since n.e.x.t
93-
* @param string $property The property name
94-
* @param mixed $value The value to set
95-
* @throws BadMethodCallException Always, as enum properties are read-only
95+
*
96+
* @param string $property The property name.
97+
* @param mixed $value The value to set.
98+
* @throws BadMethodCallException Always, as enum properties are read-only.
9699
*/
97100
final public function __set(string $property, $value): void
98101
{
@@ -102,32 +105,34 @@ final public function __set(string $property, $value): void
102105
}
103106

104107
/**
105-
* Create an enum instance from a value, throws exception if invalid
108+
* Creates an enum instance from a value, throws exception if invalid.
106109
*
107110
* @since n.e.x.t
108-
* @param string|int $value The enum value
109-
* @return static
110-
* @throws InvalidArgumentException If the value is not valid
111+
*
112+
* @param string $value The enum value.
113+
* @return static The enum instance.
114+
* @throws InvalidArgumentException If the value is not valid.
111115
*/
112-
final public static function from($value): self
116+
final public static function from(string $value): self
113117
{
114118
$instance = self::tryFrom($value);
115119
if ($instance === null) {
116120
throw new InvalidArgumentException(
117-
sprintf('%s is not a valid backing value for enum %s', (string) $value, static::class)
121+
sprintf('%s is not a valid backing value for enum %s', $value, static::class)
118122
);
119123
}
120124
return $instance;
121125
}
122126

123127
/**
124-
* Try to create an enum instance from a value, returns null if invalid
128+
* Tries to create an enum instance from a value, returns null if invalid.
125129
*
126130
* @since n.e.x.t
127-
* @param string|int $value The enum value
128-
* @return static|null
131+
*
132+
* @param string $value The enum value.
133+
* @return static|null The enum instance or null.
129134
*/
130-
final public static function tryFrom($value): ?self
135+
final public static function tryFrom(string $value): ?self
131136
{
132137
$constants = self::getConstants();
133138
foreach ($constants as $name => $constantValue) {
@@ -139,10 +144,11 @@ final public static function tryFrom($value): ?self
139144
}
140145

141146
/**
142-
* Get all enum cases
147+
* Gets all enum cases.
143148
*
144149
* @since n.e.x.t
145-
* @return static[]
150+
*
151+
* @return static[] Array of all enum instances.
146152
*/
147153
final public static function cases(): array
148154
{
@@ -155,11 +161,12 @@ final public static function cases(): array
155161
}
156162

157163
/**
158-
* Check if this enum has the same value as the given value
164+
* Checks if this enum has the same value as the given value.
159165
*
160166
* @since n.e.x.t
161-
* @param string|int|self $other The value or enum to compare
162-
* @return bool
167+
*
168+
* @param string|self $other The value or enum to compare.
169+
* @return bool True if values are equal.
163170
*/
164171
final public function equals($other): bool
165172
{
@@ -171,49 +178,53 @@ final public function equals($other): bool
171178
}
172179

173180
/**
174-
* Check if this enum is the same instance type and value as another enum
181+
* Checks if this enum is the same instance type and value as another enum.
175182
*
176183
* @since n.e.x.t
177-
* @param self $other The other enum to compare
178-
* @return bool
184+
*
185+
* @param self $other The other enum to compare.
186+
* @return bool True if enums are identical.
179187
*/
180188
final public function is(self $other): bool
181189
{
182190
return $this === $other; // Since we're using singletons, we can use identity comparison
183191
}
184192

185193
/**
186-
* Get all valid values for this enum
194+
* Gets all valid values for this enum.
187195
*
188196
* @since n.e.x.t
189-
* @return array<string, string|int>
197+
*
198+
* @return array<string, string> Map of constant names to values.
190199
*/
191200
final public static function getValues(): array
192201
{
193202
return self::getConstants();
194203
}
195204

196205
/**
197-
* Check if a value is valid for this enum
206+
* Checks if a value is valid for this enum.
198207
*
199208
* @since n.e.x.t
200-
* @param string|int $value The value to check
201-
* @return bool
209+
*
210+
* @param string $value The value to check.
211+
* @return bool True if value is valid.
202212
*/
203-
final public static function isValidValue($value): bool
213+
final public static function isValidValue(string $value): bool
204214
{
205215
return in_array($value, self::getValues(), true);
206216
}
207217

208218
/**
209-
* Get or create a singleton instance for the given value and name
219+
* Gets or creates a singleton instance for the given value and name.
210220
*
211221
* @since n.e.x.t
212-
* @param string|int $value The enum value
213-
* @param string $name The constant name
214-
* @return static
222+
*
223+
* @param string $value The enum value.
224+
* @param string $name The constant name.
225+
* @return static The enum instance.
215226
*/
216-
private static function getInstance($value, string $name): self
227+
private static function getInstance(string $value, string $name): self
217228
{
218229
$className = static::class;
219230

@@ -231,11 +242,12 @@ private static function getInstance($value, string $name): self
231242
}
232243

233244
/**
234-
* Get all constants for this enum class
245+
* Gets all constants for this enum class.
235246
*
236247
* @since n.e.x.t
237-
* @return array<string, string|int>
238-
* @throws \RuntimeException If invalid constant found
248+
*
249+
* @return array<string, string> Map of constant names to values.
250+
* @throws \RuntimeException If invalid constant found.
239251
*/
240252
final protected static function getConstants(): array
241253
{
@@ -260,11 +272,11 @@ final protected static function getConstants(): array
260272
}
261273

262274
// Check if value is valid type
263-
if (!is_string($value) && !is_int($value)) {
275+
if (!is_string($value)) {
264276
throw new \RuntimeException(
265277
sprintf(
266278
'Invalid enum value type for constant %s::%s. ' .
267-
'Only string and int values are allowed, %s given.',
279+
'Only string values are allowed, %s given.',
268280
$className,
269281
$name,
270282
gettype($value)
@@ -282,13 +294,14 @@ final protected static function getConstants(): array
282294
}
283295

284296
/**
285-
* Handle dynamic method calls for enum checking
297+
* Handles dynamic method calls for enum checking.
286298
*
287299
* @since n.e.x.t
288-
* @param string $name The method name
289-
* @param array<mixed> $arguments The method arguments
290-
* @return bool
291-
* @throws BadMethodCallException If the method doesn't exist
300+
*
301+
* @param string $name The method name.
302+
* @param array<mixed> $arguments The method arguments.
303+
* @return bool True if the enum value matches.
304+
* @throws BadMethodCallException If the method doesn't exist.
292305
*/
293306
final public function __call(string $name, array $arguments): bool
294307
{
@@ -308,13 +321,14 @@ final public function __call(string $name, array $arguments): bool
308321
}
309322

310323
/**
311-
* Handle static method calls for enum creation
324+
* Handles static method calls for enum creation.
312325
*
313326
* @since n.e.x.t
314-
* @param string $name The method name
315-
* @param array<mixed> $arguments The method arguments
316-
* @return static
317-
* @throws BadMethodCallException If the method doesn't exist
327+
*
328+
* @param string $name The method name.
329+
* @param array<mixed> $arguments The method arguments.
330+
* @return static The enum instance.
331+
* @throws BadMethodCallException If the method doesn't exist.
318332
*/
319333
final public static function __callStatic(string $name, array $arguments): self
320334
{
@@ -331,11 +345,12 @@ final public static function __callStatic(string $name, array $arguments): self
331345
}
332346

333347
/**
334-
* Convert camelCase to CONSTANT_CASE
348+
* Converts camelCase to CONSTANT_CASE.
335349
*
336350
* @since n.e.x.t
337-
* @param string $camelCase The camelCase string
338-
* @return string The CONSTANT_CASE version
351+
*
352+
* @param string $camelCase The camelCase string.
353+
* @return string The CONSTANT_CASE version.
339354
*/
340355
private static function camelCaseToConstant(string $camelCase): string
341356
{
@@ -347,13 +362,14 @@ private static function camelCaseToConstant(string $camelCase): string
347362
}
348363

349364
/**
350-
* String representation of the enum
365+
* Returns string representation of the enum.
351366
*
352367
* @since n.e.x.t
353-
* @return string
368+
*
369+
* @return string The enum value.
354370
*/
355371
final public function __toString(): string
356372
{
357-
return (string) $this->value;
373+
return $this->value;
358374
}
359375
}

src/Messages/Enums/MessagePartTypeEnum.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,45 @@
77
use WordPress\AiClient\Common\AbstractEnum;
88

99
/**
10-
* Enum for message part types
10+
* Enum for message part types.
1111
*
1212
* @since n.e.x.t
13-
* @method static self text() Create an instance for TEXT type
14-
* @method static self inlineFile() Create an instance for INLINE_FILE type
15-
* @method static self remoteFile() Create an instance for REMOTE_FILE type
16-
* @method static self functionCall() Create an instance for FUNCTION_CALL type
17-
* @method static self functionResponse() Create an instance for FUNCTION_RESPONSE type
18-
* @method bool isText() Check if the type is TEXT
19-
* @method bool isInlineFile() Check if the type is INLINE_FILE
20-
* @method bool isRemoteFile() Check if the type is REMOTE_FILE
21-
* @method bool isFunctionCall() Check if the type is FUNCTION_CALL
22-
* @method bool isFunctionResponse() Check if the type is FUNCTION_RESPONSE
13+
*
14+
* @method static self text() Creates an instance for TEXT type.
15+
* @method static self inlineFile() Creates an instance for INLINE_FILE type.
16+
* @method static self remoteFile() Creates an instance for REMOTE_FILE type.
17+
* @method static self functionCall() Creates an instance for FUNCTION_CALL type.
18+
* @method static self functionResponse() Creates an instance for FUNCTION_RESPONSE type.
19+
* @method bool isText() Checks if the type is TEXT.
20+
* @method bool isInlineFile() Checks if the type is INLINE_FILE.
21+
* @method bool isRemoteFile() Checks if the type is REMOTE_FILE.
22+
* @method bool isFunctionCall() Checks if the type is FUNCTION_CALL.
23+
* @method bool isFunctionResponse() Checks if the type is FUNCTION_RESPONSE.
2324
*/
2425
class MessagePartTypeEnum extends AbstractEnum
2526
{
2627
/**
27-
* Text content
28+
* Text content.
2829
*/
2930
public const TEXT = 'text';
3031

3132
/**
32-
* Inline file content (base64 encoded)
33+
* Inline file content (base64 encoded).
3334
*/
3435
public const INLINE_FILE = 'inline_file';
3536

3637
/**
37-
* Remote file reference (URL)
38+
* Remote file reference (URL).
3839
*/
3940
public const REMOTE_FILE = 'remote_file';
4041

4142
/**
42-
* Function call request
43+
* Function call request.
4344
*/
4445
public const FUNCTION_CALL = 'function_call';
4546

4647
/**
47-
* Function response
48+
* Function response.
4849
*/
4950
public const FUNCTION_RESPONSE = 'function_response';
5051
}

0 commit comments

Comments
 (0)