Skip to content

Commit 5eb9294

Browse files
committed
feat: adds validation and improves RequiredOption description
1 parent e1e7312 commit 5eb9294

File tree

5 files changed

+34
-19
lines changed

5 files changed

+34
-19
lines changed

src/Providers/DTO/ProviderModelsMetadata.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ public function toArray(): array
123123
*/
124124
public static function fromArray(array $array): self
125125
{
126+
static::validateFromArrayData($array, [self::KEY_PROVIDER, self::KEY_MODELS]);
127+
126128
return new self(
127129
ProviderMetadata::fromArray($array[self::KEY_PROVIDER]),
128130
array_map(

src/Providers/Models/DTO/ModelMetadata.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ public function toArray(): array
185185
*/
186186
public static function fromArray(array $array): self
187187
{
188+
static::validateFromArrayData($array, [
189+
self::KEY_ID,
190+
self::KEY_NAME,
191+
self::KEY_SUPPORTED_CAPABILITIES,
192+
self::KEY_SUPPORTED_OPTIONS,
193+
]);
194+
188195
return new self(
189196
$array[self::KEY_ID],
190197
$array[self::KEY_NAME],

src/Providers/Models/DTO/ModelRequirements.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
use WordPress\AiClient\Providers\Models\Enums\CapabilityEnum;
99

1010
/**
11-
* Represents requirements for selecting an AI model.
11+
* Represents requirements that implementing code has for AI model selection.
1212
*
13-
* This class defines the capabilities and options that are required
14-
* when selecting a model for a specific task.
13+
* This class defines the capabilities and options that a model must support
14+
* in order to be considered suitable for the implementing code's needs.
1515
*
1616
* @since n.e.x.t
1717
*
@@ -30,12 +30,12 @@ class ModelRequirements extends AbstractDataValueObject
3030
public const KEY_REQUIRED_OPTIONS = 'requiredOptions';
3131

3232
/**
33-
* @var CapabilityEnum[] The required capabilities.
33+
* @var CapabilityEnum[] The capabilities that the model must support.
3434
*/
3535
protected array $requiredCapabilities;
3636

3737
/**
38-
* @var RequiredOption[] The required options.
38+
* @var RequiredOption[] The options that the model must support with specific values.
3939
*/
4040
protected array $requiredOptions;
4141

@@ -44,8 +44,8 @@ class ModelRequirements extends AbstractDataValueObject
4444
*
4545
* @since n.e.x.t
4646
*
47-
* @param CapabilityEnum[] $requiredCapabilities The required capabilities.
48-
* @param RequiredOption[] $requiredOptions The required options.
47+
* @param CapabilityEnum[] $requiredCapabilities The capabilities that the model must support.
48+
* @param RequiredOption[] $requiredOptions The options that the model must support with specific values.
4949
*/
5050
public function __construct(array $requiredCapabilities, array $requiredOptions)
5151
{
@@ -54,7 +54,7 @@ public function __construct(array $requiredCapabilities, array $requiredOptions)
5454
}
5555

5656
/**
57-
* Gets the required capabilities.
57+
* Gets the capabilities that the model must support.
5858
*
5959
* @since n.e.x.t
6060
*
@@ -66,7 +66,7 @@ public function getRequiredCapabilities(): array
6666
}
6767

6868
/**
69-
* Gets the required options.
69+
* Gets the options that the model must support with specific values.
7070
*
7171
* @since n.e.x.t
7272
*
@@ -93,12 +93,12 @@ public static function getJsonSchema(): array
9393
'type' => 'string',
9494
'enum' => CapabilityEnum::getValues(),
9595
],
96-
'description' => 'The required capabilities.',
96+
'description' => 'The capabilities that the model must support.',
9797
],
9898
self::KEY_REQUIRED_OPTIONS => [
9999
'type' => 'array',
100100
'items' => RequiredOption::getJsonSchema(),
101-
'description' => 'The required options.',
101+
'description' => 'The options that the model must support with specific values.',
102102
],
103103
],
104104
'required' => [self::KEY_REQUIRED_CAPABILITIES, self::KEY_REQUIRED_OPTIONS],
@@ -133,6 +133,8 @@ public function toArray(): array
133133
*/
134134
public static function fromArray(array $array): self
135135
{
136+
static::validateFromArrayData($array, [self::KEY_REQUIRED_CAPABILITIES, self::KEY_REQUIRED_OPTIONS]);
137+
136138
return new self(
137139
array_map(
138140
static fn(string $capability): CapabilityEnum => CapabilityEnum::from($capability),

src/Providers/Models/DTO/RequiredOption.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use WordPress\AiClient\Common\AbstractDataValueObject;
88

99
/**
10-
* Represents a required configuration option for an AI model.
10+
* Represents an option that the implementing code requires the model to support.
1111
*
12-
* This class defines an option that must be set when using a model,
13-
* including its name and the required value.
12+
* This class defines an option that the model must support with a specific value
13+
* for it to be considered suitable for the implementing code's requirements.
1414
*
1515
* @since n.e.x.t
1616
*
@@ -32,7 +32,7 @@ class RequiredOption extends AbstractDataValueObject
3232
protected string $name;
3333

3434
/**
35-
* @var mixed The required value.
35+
* @var mixed The value that the model must support for this option.
3636
*/
3737
protected $value;
3838

@@ -42,7 +42,7 @@ class RequiredOption extends AbstractDataValueObject
4242
* @since n.e.x.t
4343
*
4444
* @param string $name The option name.
45-
* @param mixed $value The required value.
45+
* @param mixed $value The value that the model must support for this option.
4646
*/
4747
public function __construct(string $name, $value)
4848
{
@@ -63,11 +63,11 @@ public function getName(): string
6363
}
6464

6565
/**
66-
* Gets the required value.
66+
* Gets the value that the model must support for this option.
6767
*
6868
* @since n.e.x.t
6969
*
70-
* @return mixed The required value.
70+
* @return mixed The value that the model must support.
7171
*/
7272
public function getValue()
7373
{
@@ -97,7 +97,7 @@ public static function getJsonSchema(): array
9797
['type' => 'array'],
9898
['type' => 'object'],
9999
],
100-
'description' => 'The required value.',
100+
'description' => 'The value that the model must support for this option.',
101101
],
102102
],
103103
'required' => [self::KEY_NAME, self::KEY_VALUE],
@@ -126,6 +126,8 @@ public function toArray(): array
126126
*/
127127
public static function fromArray(array $array): self
128128
{
129+
static::validateFromArrayData($array, [self::KEY_NAME, self::KEY_VALUE]);
130+
129131
return new self(
130132
$array[self::KEY_NAME],
131133
$array[self::KEY_VALUE]

src/Providers/Models/DTO/SupportedOption.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public function toArray(): array
142142
*/
143143
public static function fromArray(array $array): self
144144
{
145+
static::validateFromArrayData($array, [self::KEY_NAME, self::KEY_SUPPORTED_VALUES]);
146+
145147
return new self(
146148
$array[self::KEY_NAME],
147149
$array[self::KEY_SUPPORTED_VALUES]

0 commit comments

Comments
 (0)