Subir proyecto
This commit is contained in:
@@ -0,0 +1,882 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Providers\Models\DTO;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractDataTransferObject;
|
||||
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
|
||||
use WordPress\AiClient\Files\Enums\FileTypeEnum;
|
||||
use WordPress\AiClient\Files\Enums\MediaOrientationEnum;
|
||||
use WordPress\AiClient\Messages\Enums\ModalityEnum;
|
||||
use WordPress\AiClient\Tools\DTO\FunctionDeclaration;
|
||||
use WordPress\AiClient\Tools\DTO\WebSearch;
|
||||
/**
|
||||
* Represents configuration for an AI model.
|
||||
*
|
||||
* This class allows configuring various parameters for model behavior,
|
||||
* including output modalities, system instructions, generation parameters,
|
||||
* and tool integrations.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @phpstan-import-type FunctionDeclarationArrayShape from FunctionDeclaration
|
||||
* @phpstan-import-type WebSearchArrayShape from WebSearch
|
||||
*
|
||||
* @phpstan-type ModelConfigArrayShape array{
|
||||
* outputModalities?: list<string>,
|
||||
* systemInstruction?: string,
|
||||
* candidateCount?: int,
|
||||
* maxTokens?: int,
|
||||
* temperature?: float,
|
||||
* topP?: float,
|
||||
* topK?: int,
|
||||
* stopSequences?: list<string>,
|
||||
* presencePenalty?: float,
|
||||
* frequencyPenalty?: float,
|
||||
* logprobs?: bool,
|
||||
* topLogprobs?: int,
|
||||
* functionDeclarations?: list<FunctionDeclarationArrayShape>,
|
||||
* webSearch?: WebSearchArrayShape,
|
||||
* outputFileType?: string,
|
||||
* outputMimeType?: string,
|
||||
* outputSchema?: array<string, mixed>,
|
||||
* outputMediaOrientation?: string,
|
||||
* outputMediaAspectRatio?: string,
|
||||
* outputSpeechVoice?: string,
|
||||
* customOptions?: array<string, mixed>
|
||||
* }
|
||||
*
|
||||
* @extends AbstractDataTransferObject<ModelConfigArrayShape>
|
||||
*/
|
||||
class ModelConfig extends AbstractDataTransferObject
|
||||
{
|
||||
public const KEY_OUTPUT_MODALITIES = 'outputModalities';
|
||||
public const KEY_SYSTEM_INSTRUCTION = 'systemInstruction';
|
||||
public const KEY_CANDIDATE_COUNT = 'candidateCount';
|
||||
public const KEY_MAX_TOKENS = 'maxTokens';
|
||||
public const KEY_TEMPERATURE = 'temperature';
|
||||
public const KEY_TOP_P = 'topP';
|
||||
public const KEY_TOP_K = 'topK';
|
||||
public const KEY_STOP_SEQUENCES = 'stopSequences';
|
||||
public const KEY_PRESENCE_PENALTY = 'presencePenalty';
|
||||
public const KEY_FREQUENCY_PENALTY = 'frequencyPenalty';
|
||||
public const KEY_LOGPROBS = 'logprobs';
|
||||
public const KEY_TOP_LOGPROBS = 'topLogprobs';
|
||||
public const KEY_FUNCTION_DECLARATIONS = 'functionDeclarations';
|
||||
public const KEY_WEB_SEARCH = 'webSearch';
|
||||
public const KEY_OUTPUT_FILE_TYPE = 'outputFileType';
|
||||
public const KEY_OUTPUT_MIME_TYPE = 'outputMimeType';
|
||||
public const KEY_OUTPUT_SCHEMA = 'outputSchema';
|
||||
public const KEY_OUTPUT_MEDIA_ORIENTATION = 'outputMediaOrientation';
|
||||
public const KEY_OUTPUT_MEDIA_ASPECT_RATIO = 'outputMediaAspectRatio';
|
||||
public const KEY_OUTPUT_SPEECH_VOICE = 'outputSpeechVoice';
|
||||
public const KEY_CUSTOM_OPTIONS = 'customOptions';
|
||||
/*
|
||||
* Note: This key is not an actual model config key, but specified here for convenience.
|
||||
* It is relevant for model discovery, to determine which models support which input modalities.
|
||||
* The actual input modalities are part of the message sent to the model, not the model config.
|
||||
*/
|
||||
public const KEY_INPUT_MODALITIES = 'inputModalities';
|
||||
/**
|
||||
* @var list<ModalityEnum>|null Output modalities for the model.
|
||||
*/
|
||||
protected ?array $outputModalities = null;
|
||||
/**
|
||||
* @var string|null System instruction for the model.
|
||||
*/
|
||||
protected ?string $systemInstruction = null;
|
||||
/**
|
||||
* @var int|null Number of response candidates to generate.
|
||||
*/
|
||||
protected ?int $candidateCount = null;
|
||||
/**
|
||||
* @var int|null Maximum number of tokens to generate.
|
||||
*/
|
||||
protected ?int $maxTokens = null;
|
||||
/**
|
||||
* @var float|null Temperature for randomness (0.0 to 2.0).
|
||||
*/
|
||||
protected ?float $temperature = null;
|
||||
/**
|
||||
* @var float|null Top-p nucleus sampling parameter.
|
||||
*/
|
||||
protected ?float $topP = null;
|
||||
/**
|
||||
* @var int|null Top-k sampling parameter.
|
||||
*/
|
||||
protected ?int $topK = null;
|
||||
/**
|
||||
* @var list<string>|null Stop sequences.
|
||||
*/
|
||||
protected ?array $stopSequences = null;
|
||||
/**
|
||||
* @var float|null Presence penalty for reducing repetition.
|
||||
*/
|
||||
protected ?float $presencePenalty = null;
|
||||
/**
|
||||
* @var float|null Frequency penalty for reducing repetition.
|
||||
*/
|
||||
protected ?float $frequencyPenalty = null;
|
||||
/**
|
||||
* @var bool|null Whether to return log probabilities.
|
||||
*/
|
||||
protected ?bool $logprobs = null;
|
||||
/**
|
||||
* @var int|null Number of top log probabilities to return.
|
||||
*/
|
||||
protected ?int $topLogprobs = null;
|
||||
/**
|
||||
* @var list<FunctionDeclaration>|null Function declarations available to the model.
|
||||
*/
|
||||
protected ?array $functionDeclarations = null;
|
||||
/**
|
||||
* @var WebSearch|null Web search configuration for the model.
|
||||
*/
|
||||
protected ?WebSearch $webSearch = null;
|
||||
/**
|
||||
* @var FileTypeEnum|null Output file type.
|
||||
*/
|
||||
protected ?FileTypeEnum $outputFileType = null;
|
||||
/**
|
||||
* @var string|null Output MIME type.
|
||||
*/
|
||||
protected ?string $outputMimeType = null;
|
||||
/**
|
||||
* @var array<string, mixed>|null Output schema (JSON schema).
|
||||
*/
|
||||
protected ?array $outputSchema = null;
|
||||
/**
|
||||
* @var MediaOrientationEnum|null Output media orientation.
|
||||
*/
|
||||
protected ?MediaOrientationEnum $outputMediaOrientation = null;
|
||||
/**
|
||||
* @var string|null Output media aspect ratio (e.g. 3:2, 16:9).
|
||||
*/
|
||||
protected ?string $outputMediaAspectRatio = null;
|
||||
/**
|
||||
* @var string|null Output speech voice.
|
||||
*/
|
||||
protected ?string $outputSpeechVoice = null;
|
||||
/**
|
||||
* @var array<string, mixed> Custom provider-specific options.
|
||||
*/
|
||||
protected array $customOptions = [];
|
||||
/**
|
||||
* Creates a deep clone of this configuration.
|
||||
*
|
||||
* Clones nested objects (functionDeclarations, webSearch) to ensure
|
||||
* the cloned configuration is independent of the original.
|
||||
* Enum value objects (outputModalities, outputFileType, outputMediaOrientation)
|
||||
* are intentionally shared as they are immutable.
|
||||
*
|
||||
* @since 0.4.2
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
// Deep clone function declarations if set
|
||||
if ($this->functionDeclarations !== null) {
|
||||
$clonedDeclarations = [];
|
||||
foreach ($this->functionDeclarations as $declaration) {
|
||||
$clonedDeclarations[] = clone $declaration;
|
||||
}
|
||||
$this->functionDeclarations = $clonedDeclarations;
|
||||
}
|
||||
// Clone web search if set
|
||||
if ($this->webSearch !== null) {
|
||||
$this->webSearch = clone $this->webSearch;
|
||||
}
|
||||
// Note: Enum value objects (outputModalities, outputFileType, outputMediaOrientation)
|
||||
// are immutable and can be safely shared.
|
||||
}
|
||||
/**
|
||||
* Sets the output modalities.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param list<ModalityEnum> $outputModalities The output modalities.
|
||||
*
|
||||
* @throws InvalidArgumentException If the array is not a list.
|
||||
*/
|
||||
public function setOutputModalities(array $outputModalities): void
|
||||
{
|
||||
if (!array_is_list($outputModalities)) {
|
||||
throw new InvalidArgumentException('Output modalities must be a list array.');
|
||||
}
|
||||
$this->outputModalities = $outputModalities;
|
||||
}
|
||||
/**
|
||||
* Gets the output modalities.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return list<ModalityEnum>|null The output modalities.
|
||||
*/
|
||||
public function getOutputModalities(): ?array
|
||||
{
|
||||
return $this->outputModalities;
|
||||
}
|
||||
/**
|
||||
* Sets the system instruction.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $systemInstruction The system instruction.
|
||||
*/
|
||||
public function setSystemInstruction(string $systemInstruction): void
|
||||
{
|
||||
$this->systemInstruction = $systemInstruction;
|
||||
}
|
||||
/**
|
||||
* Gets the system instruction.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The system instruction.
|
||||
*/
|
||||
public function getSystemInstruction(): ?string
|
||||
{
|
||||
return $this->systemInstruction;
|
||||
}
|
||||
/**
|
||||
* Sets the candidate count.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param int $candidateCount The candidate count.
|
||||
*/
|
||||
public function setCandidateCount(int $candidateCount): void
|
||||
{
|
||||
$this->candidateCount = $candidateCount;
|
||||
}
|
||||
/**
|
||||
* Gets the candidate count.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return int|null The candidate count.
|
||||
*/
|
||||
public function getCandidateCount(): ?int
|
||||
{
|
||||
return $this->candidateCount;
|
||||
}
|
||||
/**
|
||||
* Sets the maximum tokens.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param int $maxTokens The maximum tokens.
|
||||
*/
|
||||
public function setMaxTokens(int $maxTokens): void
|
||||
{
|
||||
$this->maxTokens = $maxTokens;
|
||||
}
|
||||
/**
|
||||
* Gets the maximum tokens.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return int|null The maximum tokens.
|
||||
*/
|
||||
public function getMaxTokens(): ?int
|
||||
{
|
||||
return $this->maxTokens;
|
||||
}
|
||||
/**
|
||||
* Sets the temperature.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param float $temperature The temperature.
|
||||
*/
|
||||
public function setTemperature(float $temperature): void
|
||||
{
|
||||
$this->temperature = $temperature;
|
||||
}
|
||||
/**
|
||||
* Gets the temperature.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return float|null The temperature.
|
||||
*/
|
||||
public function getTemperature(): ?float
|
||||
{
|
||||
return $this->temperature;
|
||||
}
|
||||
/**
|
||||
* Sets the top-p parameter.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param float $topP The top-p parameter.
|
||||
*/
|
||||
public function setTopP(float $topP): void
|
||||
{
|
||||
$this->topP = $topP;
|
||||
}
|
||||
/**
|
||||
* Gets the top-p parameter.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return float|null The top-p parameter.
|
||||
*/
|
||||
public function getTopP(): ?float
|
||||
{
|
||||
return $this->topP;
|
||||
}
|
||||
/**
|
||||
* Sets the top-k parameter.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param int $topK The top-k parameter.
|
||||
*/
|
||||
public function setTopK(int $topK): void
|
||||
{
|
||||
$this->topK = $topK;
|
||||
}
|
||||
/**
|
||||
* Gets the top-k parameter.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return int|null The top-k parameter.
|
||||
*/
|
||||
public function getTopK(): ?int
|
||||
{
|
||||
return $this->topK;
|
||||
}
|
||||
/**
|
||||
* Sets the stop sequences.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param list<string> $stopSequences The stop sequences.
|
||||
*
|
||||
* @throws InvalidArgumentException If the array is not a list.
|
||||
*/
|
||||
public function setStopSequences(array $stopSequences): void
|
||||
{
|
||||
if (!array_is_list($stopSequences)) {
|
||||
throw new InvalidArgumentException('Stop sequences must be a list array.');
|
||||
}
|
||||
$this->stopSequences = $stopSequences;
|
||||
}
|
||||
/**
|
||||
* Gets the stop sequences.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return list<string>|null The stop sequences.
|
||||
*/
|
||||
public function getStopSequences(): ?array
|
||||
{
|
||||
return $this->stopSequences;
|
||||
}
|
||||
/**
|
||||
* Sets the presence penalty.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param float $presencePenalty The presence penalty.
|
||||
*/
|
||||
public function setPresencePenalty(float $presencePenalty): void
|
||||
{
|
||||
$this->presencePenalty = $presencePenalty;
|
||||
}
|
||||
/**
|
||||
* Gets the presence penalty.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return float|null The presence penalty.
|
||||
*/
|
||||
public function getPresencePenalty(): ?float
|
||||
{
|
||||
return $this->presencePenalty;
|
||||
}
|
||||
/**
|
||||
* Sets the frequency penalty.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param float $frequencyPenalty The frequency penalty.
|
||||
*/
|
||||
public function setFrequencyPenalty(float $frequencyPenalty): void
|
||||
{
|
||||
$this->frequencyPenalty = $frequencyPenalty;
|
||||
}
|
||||
/**
|
||||
* Gets the frequency penalty.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return float|null The frequency penalty.
|
||||
*/
|
||||
public function getFrequencyPenalty(): ?float
|
||||
{
|
||||
return $this->frequencyPenalty;
|
||||
}
|
||||
/**
|
||||
* Sets whether to return log probabilities.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param bool $logprobs Whether to return log probabilities.
|
||||
*/
|
||||
public function setLogprobs(bool $logprobs): void
|
||||
{
|
||||
$this->logprobs = $logprobs;
|
||||
}
|
||||
/**
|
||||
* Gets whether to return log probabilities.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return bool|null Whether to return log probabilities.
|
||||
*/
|
||||
public function getLogprobs(): ?bool
|
||||
{
|
||||
return $this->logprobs;
|
||||
}
|
||||
/**
|
||||
* Sets the number of top log probabilities to return.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param int $topLogprobs The number of top log probabilities.
|
||||
*/
|
||||
public function setTopLogprobs(int $topLogprobs): void
|
||||
{
|
||||
$this->topLogprobs = $topLogprobs;
|
||||
}
|
||||
/**
|
||||
* Gets the number of top log probabilities to return.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return int|null The number of top log probabilities.
|
||||
*/
|
||||
public function getTopLogprobs(): ?int
|
||||
{
|
||||
return $this->topLogprobs;
|
||||
}
|
||||
/**
|
||||
* Sets the function declarations.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param list<FunctionDeclaration> $functionDeclarations The function declarations.
|
||||
*
|
||||
* @throws InvalidArgumentException If the array is not a list.
|
||||
*/
|
||||
public function setFunctionDeclarations(array $functionDeclarations): void
|
||||
{
|
||||
if (!array_is_list($functionDeclarations)) {
|
||||
throw new InvalidArgumentException('Function declarations must be a list array.');
|
||||
}
|
||||
$this->functionDeclarations = $functionDeclarations;
|
||||
}
|
||||
/**
|
||||
* Gets the function declarations.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return list<FunctionDeclaration>|null The function declarations.
|
||||
*/
|
||||
public function getFunctionDeclarations(): ?array
|
||||
{
|
||||
return $this->functionDeclarations;
|
||||
}
|
||||
/**
|
||||
* Sets the web search configuration.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param WebSearch $webSearch The web search configuration.
|
||||
*/
|
||||
public function setWebSearch(WebSearch $webSearch): void
|
||||
{
|
||||
$this->webSearch = $webSearch;
|
||||
}
|
||||
/**
|
||||
* Gets the web search configuration.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return WebSearch|null The web search configuration.
|
||||
*/
|
||||
public function getWebSearch(): ?WebSearch
|
||||
{
|
||||
return $this->webSearch;
|
||||
}
|
||||
/**
|
||||
* Sets the output file type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param FileTypeEnum $outputFileType The output file type.
|
||||
*/
|
||||
public function setOutputFileType(FileTypeEnum $outputFileType): void
|
||||
{
|
||||
$this->outputFileType = $outputFileType;
|
||||
}
|
||||
/**
|
||||
* Gets the output file type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return FileTypeEnum|null The output file type.
|
||||
*/
|
||||
public function getOutputFileType(): ?FileTypeEnum
|
||||
{
|
||||
return $this->outputFileType;
|
||||
}
|
||||
/**
|
||||
* Sets the output MIME type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $outputMimeType The output MIME type.
|
||||
*/
|
||||
public function setOutputMimeType(string $outputMimeType): void
|
||||
{
|
||||
$this->outputMimeType = $outputMimeType;
|
||||
}
|
||||
/**
|
||||
* Gets the output MIME type.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The output MIME type.
|
||||
*/
|
||||
public function getOutputMimeType(): ?string
|
||||
{
|
||||
return $this->outputMimeType;
|
||||
}
|
||||
/**
|
||||
* Sets the output schema.
|
||||
*
|
||||
* When setting an output schema, this method automatically sets
|
||||
* the output MIME type to "application/json" if not already set.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array<string, mixed> $outputSchema The output schema (JSON schema).
|
||||
*/
|
||||
public function setOutputSchema(array $outputSchema): void
|
||||
{
|
||||
$this->outputSchema = $outputSchema;
|
||||
// Automatically set outputMimeType to application/json when schema is provided
|
||||
if ($this->outputMimeType === null) {
|
||||
$this->outputMimeType = 'application/json';
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Gets the output schema.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return array<string, mixed>|null The output schema.
|
||||
*/
|
||||
public function getOutputSchema(): ?array
|
||||
{
|
||||
return $this->outputSchema;
|
||||
}
|
||||
/**
|
||||
* Sets the output media orientation.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param MediaOrientationEnum $outputMediaOrientation The output media orientation.
|
||||
*/
|
||||
public function setOutputMediaOrientation(MediaOrientationEnum $outputMediaOrientation): void
|
||||
{
|
||||
if ($this->outputMediaAspectRatio) {
|
||||
$this->validateMediaOrientationAspectRatioCompatibility($outputMediaOrientation, $this->outputMediaAspectRatio);
|
||||
}
|
||||
$this->outputMediaOrientation = $outputMediaOrientation;
|
||||
}
|
||||
/**
|
||||
* Gets the output media orientation.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return MediaOrientationEnum|null The output media orientation.
|
||||
*/
|
||||
public function getOutputMediaOrientation(): ?MediaOrientationEnum
|
||||
{
|
||||
return $this->outputMediaOrientation;
|
||||
}
|
||||
/**
|
||||
* Sets the output media aspect ratio.
|
||||
*
|
||||
* If set, this supersedes the output media orientation, as it is a more specific configuration.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $outputMediaAspectRatio The output media aspect ratio (e.g. 3:2, 16:9).
|
||||
*/
|
||||
public function setOutputMediaAspectRatio(string $outputMediaAspectRatio): void
|
||||
{
|
||||
if (!preg_match('/^\d+:\d+$/', $outputMediaAspectRatio)) {
|
||||
throw new InvalidArgumentException('Output media aspect ratio must be in the format "width:height" (e.g. 3:2, 16:9).');
|
||||
}
|
||||
if ($this->outputMediaOrientation) {
|
||||
$this->validateMediaOrientationAspectRatioCompatibility($this->outputMediaOrientation, $outputMediaAspectRatio);
|
||||
}
|
||||
$this->outputMediaAspectRatio = $outputMediaAspectRatio;
|
||||
}
|
||||
/**
|
||||
* Gets the output media aspect ratio.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The output media aspect ratio (e.g. 3:2, 16:9).
|
||||
*/
|
||||
public function getOutputMediaAspectRatio(): ?string
|
||||
{
|
||||
return $this->outputMediaAspectRatio;
|
||||
}
|
||||
/**
|
||||
* Validates that the given media orientation and aspect ratio values do not conflict with each other.
|
||||
*
|
||||
* @since 0.4.0
|
||||
*
|
||||
* @param MediaOrientationEnum $orientation The desired media orientation.
|
||||
* @param string $aspectRatio The desired media aspect ratio.
|
||||
*/
|
||||
protected function validateMediaOrientationAspectRatioCompatibility(MediaOrientationEnum $orientation, string $aspectRatio): void
|
||||
{
|
||||
$aspectRatioParts = explode(':', $aspectRatio);
|
||||
if ($orientation->isSquare() && $aspectRatioParts[0] !== $aspectRatioParts[1]) {
|
||||
throw new InvalidArgumentException('The aspect ratio "' . $aspectRatio . '" is not compatible with the square orientation.');
|
||||
}
|
||||
if ($orientation->isLandscape() && $aspectRatioParts[0] <= $aspectRatioParts[1]) {
|
||||
throw new InvalidArgumentException('The aspect ratio "' . $aspectRatio . '" is not compatible with the landscape orientation.');
|
||||
}
|
||||
if ($orientation->isPortrait() && $aspectRatioParts[0] >= $aspectRatioParts[1]) {
|
||||
throw new InvalidArgumentException('The aspect ratio "' . $aspectRatio . '" is not compatible with the portrait orientation.');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Sets the output speech voice.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $outputSpeechVoice The output speech voice.
|
||||
*/
|
||||
public function setOutputSpeechVoice(string $outputSpeechVoice): void
|
||||
{
|
||||
$this->outputSpeechVoice = $outputSpeechVoice;
|
||||
}
|
||||
/**
|
||||
* Gets the output speech voice.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string|null The output speech voice.
|
||||
*/
|
||||
public function getOutputSpeechVoice(): ?string
|
||||
{
|
||||
return $this->outputSpeechVoice;
|
||||
}
|
||||
/**
|
||||
* Sets a single custom option.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $key The option key.
|
||||
* @param mixed $value The option value.
|
||||
*/
|
||||
public function setCustomOption(string $key, $value): void
|
||||
{
|
||||
$this->customOptions[$key] = $value;
|
||||
}
|
||||
/**
|
||||
* Sets the custom options.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param array<string, mixed> $customOptions The custom options.
|
||||
*/
|
||||
public function setCustomOptions(array $customOptions): void
|
||||
{
|
||||
$this->customOptions = $customOptions;
|
||||
}
|
||||
/**
|
||||
* Gets the custom options.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return array<string, mixed> The custom options.
|
||||
*/
|
||||
public function getCustomOptions(): array
|
||||
{
|
||||
return $this->customOptions;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function getJsonSchema(): array
|
||||
{
|
||||
return ['type' => 'object', 'properties' => [self::KEY_OUTPUT_MODALITIES => ['type' => 'array', 'items' => ['type' => 'string', 'enum' => ModalityEnum::getValues()], 'description' => 'Output modalities for the model.'], self::KEY_SYSTEM_INSTRUCTION => ['type' => 'string', 'description' => 'System instruction for the model.'], self::KEY_CANDIDATE_COUNT => ['type' => 'integer', 'minimum' => 1, 'description' => 'Number of response candidates to generate.'], self::KEY_MAX_TOKENS => ['type' => 'integer', 'minimum' => 1, 'description' => 'Maximum number of tokens to generate.'], self::KEY_TEMPERATURE => ['type' => 'number', 'minimum' => 0.0, 'maximum' => 2.0, 'description' => 'Temperature for randomness.'], self::KEY_TOP_P => ['type' => 'number', 'minimum' => 0.0, 'maximum' => 1.0, 'description' => 'Top-p nucleus sampling parameter.'], self::KEY_TOP_K => ['type' => 'integer', 'minimum' => 1, 'description' => 'Top-k sampling parameter.'], self::KEY_STOP_SEQUENCES => ['type' => 'array', 'items' => ['type' => 'string'], 'description' => 'Stop sequences.'], self::KEY_PRESENCE_PENALTY => ['type' => 'number', 'description' => 'Presence penalty for reducing repetition.'], self::KEY_FREQUENCY_PENALTY => ['type' => 'number', 'description' => 'Frequency penalty for reducing repetition.'], self::KEY_LOGPROBS => ['type' => 'boolean', 'description' => 'Whether to return log probabilities.'], self::KEY_TOP_LOGPROBS => ['type' => 'integer', 'minimum' => 1, 'description' => 'Number of top log probabilities to return.'], self::KEY_FUNCTION_DECLARATIONS => ['type' => 'array', 'items' => FunctionDeclaration::getJsonSchema(), 'description' => 'Function declarations available to the model.'], self::KEY_WEB_SEARCH => WebSearch::getJsonSchema(), self::KEY_OUTPUT_FILE_TYPE => ['type' => 'string', 'enum' => FileTypeEnum::getValues(), 'description' => 'Output file type.'], self::KEY_OUTPUT_MIME_TYPE => ['type' => 'string', 'description' => 'Output MIME type.'], self::KEY_OUTPUT_SCHEMA => ['type' => 'object', 'additionalProperties' => \true, 'description' => 'Output schema (JSON schema).'], self::KEY_OUTPUT_MEDIA_ORIENTATION => ['type' => 'string', 'enum' => MediaOrientationEnum::getValues(), 'description' => 'Output media orientation.'], self::KEY_OUTPUT_MEDIA_ASPECT_RATIO => ['type' => 'string', 'pattern' => '^\d+:\d+$', 'description' => 'Output media aspect ratio.'], self::KEY_OUTPUT_SPEECH_VOICE => ['type' => 'string', 'description' => 'Output speech voice.'], self::KEY_CUSTOM_OPTIONS => ['type' => 'object', 'additionalProperties' => \true, 'description' => 'Custom provider-specific options.']], 'additionalProperties' => \false];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return ModelConfigArrayShape
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$data = [];
|
||||
if ($this->outputModalities !== null) {
|
||||
$data[self::KEY_OUTPUT_MODALITIES] = array_map(static function (ModalityEnum $modality): string {
|
||||
return $modality->value;
|
||||
}, $this->outputModalities);
|
||||
}
|
||||
if ($this->systemInstruction !== null) {
|
||||
$data[self::KEY_SYSTEM_INSTRUCTION] = $this->systemInstruction;
|
||||
}
|
||||
if ($this->candidateCount !== null) {
|
||||
$data[self::KEY_CANDIDATE_COUNT] = $this->candidateCount;
|
||||
}
|
||||
if ($this->maxTokens !== null) {
|
||||
$data[self::KEY_MAX_TOKENS] = $this->maxTokens;
|
||||
}
|
||||
if ($this->temperature !== null) {
|
||||
$data[self::KEY_TEMPERATURE] = $this->temperature;
|
||||
}
|
||||
if ($this->topP !== null) {
|
||||
$data[self::KEY_TOP_P] = $this->topP;
|
||||
}
|
||||
if ($this->topK !== null) {
|
||||
$data[self::KEY_TOP_K] = $this->topK;
|
||||
}
|
||||
if ($this->stopSequences !== null) {
|
||||
$data[self::KEY_STOP_SEQUENCES] = $this->stopSequences;
|
||||
}
|
||||
if ($this->presencePenalty !== null) {
|
||||
$data[self::KEY_PRESENCE_PENALTY] = $this->presencePenalty;
|
||||
}
|
||||
if ($this->frequencyPenalty !== null) {
|
||||
$data[self::KEY_FREQUENCY_PENALTY] = $this->frequencyPenalty;
|
||||
}
|
||||
if ($this->logprobs !== null) {
|
||||
$data[self::KEY_LOGPROBS] = $this->logprobs;
|
||||
}
|
||||
if ($this->topLogprobs !== null) {
|
||||
$data[self::KEY_TOP_LOGPROBS] = $this->topLogprobs;
|
||||
}
|
||||
if ($this->functionDeclarations !== null) {
|
||||
$data[self::KEY_FUNCTION_DECLARATIONS] = array_map(static function (FunctionDeclaration $functionDeclaration): array {
|
||||
return $functionDeclaration->toArray();
|
||||
}, $this->functionDeclarations);
|
||||
}
|
||||
if ($this->webSearch !== null) {
|
||||
$data[self::KEY_WEB_SEARCH] = $this->webSearch->toArray();
|
||||
}
|
||||
if ($this->outputFileType !== null) {
|
||||
$data[self::KEY_OUTPUT_FILE_TYPE] = $this->outputFileType->value;
|
||||
}
|
||||
if ($this->outputMimeType !== null) {
|
||||
$data[self::KEY_OUTPUT_MIME_TYPE] = $this->outputMimeType;
|
||||
}
|
||||
if ($this->outputSchema !== null) {
|
||||
$data[self::KEY_OUTPUT_SCHEMA] = $this->outputSchema;
|
||||
}
|
||||
if ($this->outputMediaOrientation !== null) {
|
||||
$data[self::KEY_OUTPUT_MEDIA_ORIENTATION] = $this->outputMediaOrientation->value;
|
||||
}
|
||||
if ($this->outputMediaAspectRatio !== null) {
|
||||
$data[self::KEY_OUTPUT_MEDIA_ASPECT_RATIO] = $this->outputMediaAspectRatio;
|
||||
}
|
||||
if ($this->outputSpeechVoice !== null) {
|
||||
$data[self::KEY_OUTPUT_SPEECH_VOICE] = $this->outputSpeechVoice;
|
||||
}
|
||||
if (!empty($this->customOptions)) {
|
||||
$data[self::KEY_CUSTOM_OPTIONS] = $this->customOptions;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
$config = new self();
|
||||
if (isset($array[self::KEY_OUTPUT_MODALITIES])) {
|
||||
$config->setOutputModalities(array_map(static fn(string $modality): ModalityEnum => ModalityEnum::from($modality), $array[self::KEY_OUTPUT_MODALITIES]));
|
||||
}
|
||||
if (isset($array[self::KEY_SYSTEM_INSTRUCTION])) {
|
||||
$config->setSystemInstruction($array[self::KEY_SYSTEM_INSTRUCTION]);
|
||||
}
|
||||
if (isset($array[self::KEY_CANDIDATE_COUNT])) {
|
||||
$config->setCandidateCount($array[self::KEY_CANDIDATE_COUNT]);
|
||||
}
|
||||
if (isset($array[self::KEY_MAX_TOKENS])) {
|
||||
$config->setMaxTokens($array[self::KEY_MAX_TOKENS]);
|
||||
}
|
||||
if (isset($array[self::KEY_TEMPERATURE])) {
|
||||
$config->setTemperature($array[self::KEY_TEMPERATURE]);
|
||||
}
|
||||
if (isset($array[self::KEY_TOP_P])) {
|
||||
$config->setTopP($array[self::KEY_TOP_P]);
|
||||
}
|
||||
if (isset($array[self::KEY_TOP_K])) {
|
||||
$config->setTopK($array[self::KEY_TOP_K]);
|
||||
}
|
||||
if (isset($array[self::KEY_STOP_SEQUENCES])) {
|
||||
$config->setStopSequences($array[self::KEY_STOP_SEQUENCES]);
|
||||
}
|
||||
if (isset($array[self::KEY_PRESENCE_PENALTY])) {
|
||||
$config->setPresencePenalty($array[self::KEY_PRESENCE_PENALTY]);
|
||||
}
|
||||
if (isset($array[self::KEY_FREQUENCY_PENALTY])) {
|
||||
$config->setFrequencyPenalty($array[self::KEY_FREQUENCY_PENALTY]);
|
||||
}
|
||||
if (isset($array[self::KEY_LOGPROBS])) {
|
||||
$config->setLogprobs($array[self::KEY_LOGPROBS]);
|
||||
}
|
||||
if (isset($array[self::KEY_TOP_LOGPROBS])) {
|
||||
$config->setTopLogprobs($array[self::KEY_TOP_LOGPROBS]);
|
||||
}
|
||||
if (isset($array[self::KEY_FUNCTION_DECLARATIONS])) {
|
||||
$config->setFunctionDeclarations(array_map(static function (array $functionDeclarationData): FunctionDeclaration {
|
||||
return FunctionDeclaration::fromArray($functionDeclarationData);
|
||||
}, $array[self::KEY_FUNCTION_DECLARATIONS]));
|
||||
}
|
||||
if (isset($array[self::KEY_WEB_SEARCH])) {
|
||||
$config->setWebSearch(WebSearch::fromArray($array[self::KEY_WEB_SEARCH]));
|
||||
}
|
||||
if (isset($array[self::KEY_OUTPUT_FILE_TYPE])) {
|
||||
$config->setOutputFileType(FileTypeEnum::from($array[self::KEY_OUTPUT_FILE_TYPE]));
|
||||
}
|
||||
if (isset($array[self::KEY_OUTPUT_MIME_TYPE])) {
|
||||
$config->setOutputMimeType($array[self::KEY_OUTPUT_MIME_TYPE]);
|
||||
}
|
||||
if (isset($array[self::KEY_OUTPUT_SCHEMA])) {
|
||||
$config->setOutputSchema($array[self::KEY_OUTPUT_SCHEMA]);
|
||||
}
|
||||
if (isset($array[self::KEY_OUTPUT_MEDIA_ORIENTATION])) {
|
||||
$config->setOutputMediaOrientation(MediaOrientationEnum::from($array[self::KEY_OUTPUT_MEDIA_ORIENTATION]));
|
||||
}
|
||||
if (isset($array[self::KEY_OUTPUT_MEDIA_ASPECT_RATIO])) {
|
||||
$config->setOutputMediaAspectRatio($array[self::KEY_OUTPUT_MEDIA_ASPECT_RATIO]);
|
||||
}
|
||||
if (isset($array[self::KEY_OUTPUT_SPEECH_VOICE])) {
|
||||
$config->setOutputSpeechVoice($array[self::KEY_OUTPUT_SPEECH_VOICE]);
|
||||
}
|
||||
if (isset($array[self::KEY_CUSTOM_OPTIONS])) {
|
||||
$config->setCustomOptions($array[self::KEY_CUSTOM_OPTIONS]);
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Providers\Models\DTO;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractDataTransferObject;
|
||||
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
|
||||
use WordPress\AiClient\Providers\Models\Enums\CapabilityEnum;
|
||||
/**
|
||||
* Represents metadata about an AI model.
|
||||
*
|
||||
* This class contains information about a specific AI model, including
|
||||
* its identifier, display name, supported capabilities, and configuration options.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @phpstan-import-type SupportedOptionArrayShape from SupportedOption
|
||||
*
|
||||
* @phpstan-type ModelMetadataArrayShape array{
|
||||
* id: string,
|
||||
* name: string,
|
||||
* supportedCapabilities: list<string>,
|
||||
* supportedOptions: list<SupportedOptionArrayShape>
|
||||
* }
|
||||
*
|
||||
* @extends AbstractDataTransferObject<ModelMetadataArrayShape>
|
||||
*/
|
||||
class ModelMetadata extends AbstractDataTransferObject
|
||||
{
|
||||
public const KEY_ID = 'id';
|
||||
public const KEY_NAME = 'name';
|
||||
public const KEY_SUPPORTED_CAPABILITIES = 'supportedCapabilities';
|
||||
public const KEY_SUPPORTED_OPTIONS = 'supportedOptions';
|
||||
/**
|
||||
* @var string The model's unique identifier.
|
||||
*/
|
||||
protected string $id;
|
||||
/**
|
||||
* @var string The model's display name.
|
||||
*/
|
||||
protected string $name;
|
||||
/**
|
||||
* @var list<CapabilityEnum> The model's supported capabilities.
|
||||
*/
|
||||
protected array $supportedCapabilities;
|
||||
/**
|
||||
* @var list<SupportedOption> The model's supported configuration options.
|
||||
*/
|
||||
protected array $supportedOptions;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param string $id The model's unique identifier.
|
||||
* @param string $name The model's display name.
|
||||
* @param list<CapabilityEnum> $supportedCapabilities The model's supported capabilities.
|
||||
* @param list<SupportedOption> $supportedOptions The model's supported configuration options.
|
||||
*
|
||||
* @throws InvalidArgumentException If arrays are not lists.
|
||||
*/
|
||||
public function __construct(string $id, string $name, array $supportedCapabilities, array $supportedOptions)
|
||||
{
|
||||
if (!array_is_list($supportedCapabilities)) {
|
||||
throw new InvalidArgumentException('Supported capabilities must be a list array.');
|
||||
}
|
||||
if (!array_is_list($supportedOptions)) {
|
||||
throw new InvalidArgumentException('Supported options must be a list array.');
|
||||
}
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->supportedCapabilities = $supportedCapabilities;
|
||||
$this->supportedOptions = $supportedOptions;
|
||||
}
|
||||
/**
|
||||
* Gets the model's unique identifier.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string The model ID.
|
||||
*/
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
/**
|
||||
* Gets the model's display name.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return string The model name.
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
/**
|
||||
* Gets the model's supported capabilities.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return list<CapabilityEnum> The supported capabilities.
|
||||
*/
|
||||
public function getSupportedCapabilities(): array
|
||||
{
|
||||
return $this->supportedCapabilities;
|
||||
}
|
||||
/**
|
||||
* Gets the model's supported configuration options.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return list<SupportedOption> The supported options.
|
||||
*/
|
||||
public function getSupportedOptions(): array
|
||||
{
|
||||
return $this->supportedOptions;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function getJsonSchema(): array
|
||||
{
|
||||
return ['type' => 'object', 'properties' => [self::KEY_ID => ['type' => 'string', 'description' => 'The model\'s unique identifier.'], self::KEY_NAME => ['type' => 'string', 'description' => 'The model\'s display name.'], self::KEY_SUPPORTED_CAPABILITIES => ['type' => 'array', 'items' => ['type' => 'string', 'enum' => CapabilityEnum::getValues()], 'description' => 'The model\'s supported capabilities.'], self::KEY_SUPPORTED_OPTIONS => ['type' => 'array', 'items' => \WordPress\AiClient\Providers\Models\DTO\SupportedOption::getJsonSchema(), 'description' => 'The model\'s supported configuration options.']], 'required' => [self::KEY_ID, self::KEY_NAME, self::KEY_SUPPORTED_CAPABILITIES, self::KEY_SUPPORTED_OPTIONS]];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return ModelMetadataArrayShape
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [self::KEY_ID => $this->id, self::KEY_NAME => $this->name, self::KEY_SUPPORTED_CAPABILITIES => array_map(static fn(CapabilityEnum $capability): string => $capability->value, $this->supportedCapabilities), self::KEY_SUPPORTED_OPTIONS => array_map(static fn(\WordPress\AiClient\Providers\Models\DTO\SupportedOption $option): array => $option->toArray(), $this->supportedOptions)];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
static::validateFromArrayData($array, [self::KEY_ID, self::KEY_NAME, self::KEY_SUPPORTED_CAPABILITIES, self::KEY_SUPPORTED_OPTIONS]);
|
||||
return new self($array[self::KEY_ID], $array[self::KEY_NAME], array_map(static fn(string $capability): CapabilityEnum => CapabilityEnum::from($capability), $array[self::KEY_SUPPORTED_CAPABILITIES]), array_map(static fn(array $optionData): \WordPress\AiClient\Providers\Models\DTO\SupportedOption => \WordPress\AiClient\Providers\Models\DTO\SupportedOption::fromArray($optionData), $array[self::KEY_SUPPORTED_OPTIONS]));
|
||||
}
|
||||
/**
|
||||
* Performs a deep clone of the model metadata.
|
||||
*
|
||||
* This method ensures that supported option objects are cloned to prevent
|
||||
* modifications to the cloned metadata from affecting the original.
|
||||
*
|
||||
* @since 0.4.2
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
$clonedOptions = [];
|
||||
foreach ($this->supportedOptions as $option) {
|
||||
$clonedOptions[] = clone $option;
|
||||
}
|
||||
$this->supportedOptions = $clonedOptions;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Providers\Models\DTO;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractDataTransferObject;
|
||||
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
|
||||
use WordPress\AiClient\Messages\DTO\Message;
|
||||
use WordPress\AiClient\Messages\Enums\ModalityEnum;
|
||||
use WordPress\AiClient\Providers\Models\Enums\CapabilityEnum;
|
||||
use WordPress\AiClient\Providers\Models\Enums\OptionEnum;
|
||||
/**
|
||||
* Represents requirements that implementing code has for AI model selection.
|
||||
*
|
||||
* This class defines the capabilities and options that a model must support
|
||||
* in order to be considered suitable for the implementing code's needs.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @phpstan-import-type RequiredOptionArrayShape from RequiredOption
|
||||
*
|
||||
* @phpstan-type ModelRequirementsArrayShape array{
|
||||
* requiredCapabilities: list<string>,
|
||||
* requiredOptions: list<RequiredOptionArrayShape>
|
||||
* }
|
||||
*
|
||||
* @extends AbstractDataTransferObject<ModelRequirementsArrayShape>
|
||||
*/
|
||||
class ModelRequirements extends AbstractDataTransferObject
|
||||
{
|
||||
public const KEY_REQUIRED_CAPABILITIES = 'requiredCapabilities';
|
||||
public const KEY_REQUIRED_OPTIONS = 'requiredOptions';
|
||||
/**
|
||||
* @var list<CapabilityEnum> The capabilities that the model must support.
|
||||
*/
|
||||
protected array $requiredCapabilities;
|
||||
/**
|
||||
* @var list<RequiredOption> The options that the model must support with specific values.
|
||||
*/
|
||||
protected array $requiredOptions;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param list<CapabilityEnum> $requiredCapabilities The capabilities that the model must support.
|
||||
* @param list<RequiredOption> $requiredOptions The options that the model must support with specific values.
|
||||
*
|
||||
* @throws InvalidArgumentException If arrays are not lists.
|
||||
*/
|
||||
public function __construct(array $requiredCapabilities, array $requiredOptions)
|
||||
{
|
||||
if (!array_is_list($requiredCapabilities)) {
|
||||
throw new InvalidArgumentException('Required capabilities must be a list array.');
|
||||
}
|
||||
if (!array_is_list($requiredOptions)) {
|
||||
throw new InvalidArgumentException('Required options must be a list array.');
|
||||
}
|
||||
$this->requiredCapabilities = $requiredCapabilities;
|
||||
$this->requiredOptions = $requiredOptions;
|
||||
}
|
||||
/**
|
||||
* Gets the capabilities that the model must support.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return list<CapabilityEnum> The required capabilities.
|
||||
*/
|
||||
public function getRequiredCapabilities(): array
|
||||
{
|
||||
return $this->requiredCapabilities;
|
||||
}
|
||||
/**
|
||||
* Gets the options that the model must support with specific values.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return list<RequiredOption> The required options.
|
||||
*/
|
||||
public function getRequiredOptions(): array
|
||||
{
|
||||
return $this->requiredOptions;
|
||||
}
|
||||
/**
|
||||
* Checks whether the given model metadata meets these requirements.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*
|
||||
* @param ModelMetadata $metadata The model metadata to check against.
|
||||
* @return bool True if the model meets all requirements, false otherwise.
|
||||
*/
|
||||
public function areMetBy(\WordPress\AiClient\Providers\Models\DTO\ModelMetadata $metadata): bool
|
||||
{
|
||||
// Create lookup maps for better performance (instead of nested foreach loops)
|
||||
$capabilitiesMap = [];
|
||||
foreach ($metadata->getSupportedCapabilities() as $capability) {
|
||||
$capabilitiesMap[$capability->value] = $capability;
|
||||
}
|
||||
$optionsMap = [];
|
||||
foreach ($metadata->getSupportedOptions() as $option) {
|
||||
$optionsMap[$option->getName()->value] = $option;
|
||||
}
|
||||
// Check if all required capabilities are supported using map lookup
|
||||
foreach ($this->requiredCapabilities as $requiredCapability) {
|
||||
if (!isset($capabilitiesMap[$requiredCapability->value])) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
// Check if all required options are supported with the specified values
|
||||
foreach ($this->requiredOptions as $requiredOption) {
|
||||
// Use map lookup instead of linear search
|
||||
if (!isset($optionsMap[$requiredOption->getName()->value])) {
|
||||
return \false;
|
||||
}
|
||||
$supportedOption = $optionsMap[$requiredOption->getName()->value];
|
||||
// Check if the required value is supported by this option
|
||||
if (!$supportedOption->isSupportedValue($requiredOption->getValue())) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
/**
|
||||
* Creates ModelRequirements from prompt data and model configuration.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*
|
||||
* @param CapabilityEnum $capability The capability the model must support.
|
||||
* @param list<Message> $messages The messages in the conversation.
|
||||
* @param ModelConfig $modelConfig The model configuration.
|
||||
* @return self The created requirements.
|
||||
*/
|
||||
public static function fromPromptData(CapabilityEnum $capability, array $messages, \WordPress\AiClient\Providers\Models\DTO\ModelConfig $modelConfig): self
|
||||
{
|
||||
// Start with base capability
|
||||
$capabilities = [$capability];
|
||||
$inputModalities = [];
|
||||
// Check if we have chat history (multiple messages)
|
||||
if (count($messages) > 1) {
|
||||
$capabilities[] = CapabilityEnum::chatHistory();
|
||||
}
|
||||
// Analyze all messages to determine required input modalities
|
||||
$hasFunctionMessageParts = \false;
|
||||
foreach ($messages as $message) {
|
||||
foreach ($message->getParts() as $part) {
|
||||
// Check for text input
|
||||
if ($part->getType()->isText()) {
|
||||
$inputModalities[] = ModalityEnum::text();
|
||||
}
|
||||
// Check for file inputs
|
||||
if ($part->getType()->isFile()) {
|
||||
$file = $part->getFile();
|
||||
if ($file !== null) {
|
||||
if ($file->isImage()) {
|
||||
$inputModalities[] = ModalityEnum::image();
|
||||
} elseif ($file->isAudio()) {
|
||||
$inputModalities[] = ModalityEnum::audio();
|
||||
} elseif ($file->isVideo()) {
|
||||
$inputModalities[] = ModalityEnum::video();
|
||||
} elseif ($file->isDocument() || $file->isText()) {
|
||||
$inputModalities[] = ModalityEnum::document();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Check for function calls/responses (these might require special capabilities)
|
||||
if ($part->getType()->isFunctionCall() || $part->getType()->isFunctionResponse()) {
|
||||
$hasFunctionMessageParts = \true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Convert ModelConfig to RequiredOptions
|
||||
$requiredOptions = self::toRequiredOptions($modelConfig);
|
||||
// Add additional options based on message analysis
|
||||
if ($hasFunctionMessageParts) {
|
||||
$requiredOptions = self::includeInRequiredOptions($requiredOptions, new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::functionDeclarations(), \true));
|
||||
}
|
||||
// Add input modalities if we have any inputs
|
||||
if (!empty($inputModalities)) {
|
||||
// Remove duplicates
|
||||
$inputModalities = array_unique($inputModalities, \SORT_REGULAR);
|
||||
$requiredOptions = self::includeInRequiredOptions($requiredOptions, new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::inputModalities(), array_values($inputModalities)));
|
||||
}
|
||||
// Step 6: Return new ModelRequirements
|
||||
return new self($capabilities, $requiredOptions);
|
||||
}
|
||||
/**
|
||||
* Converts ModelConfig to an array of RequiredOptions.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*
|
||||
* @param ModelConfig $modelConfig The model configuration.
|
||||
* @return list<RequiredOption> The required options.
|
||||
*/
|
||||
private static function toRequiredOptions(\WordPress\AiClient\Providers\Models\DTO\ModelConfig $modelConfig): array
|
||||
{
|
||||
$requiredOptions = [];
|
||||
// Map properties that have corresponding OptionEnum values
|
||||
if ($modelConfig->getOutputModalities() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::outputModalities(), $modelConfig->getOutputModalities());
|
||||
}
|
||||
if ($modelConfig->getSystemInstruction() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::systemInstruction(), $modelConfig->getSystemInstruction());
|
||||
}
|
||||
if ($modelConfig->getCandidateCount() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::candidateCount(), $modelConfig->getCandidateCount());
|
||||
}
|
||||
if ($modelConfig->getMaxTokens() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::maxTokens(), $modelConfig->getMaxTokens());
|
||||
}
|
||||
if ($modelConfig->getTemperature() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::temperature(), $modelConfig->getTemperature());
|
||||
}
|
||||
if ($modelConfig->getTopP() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::topP(), $modelConfig->getTopP());
|
||||
}
|
||||
if ($modelConfig->getTopK() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::topK(), $modelConfig->getTopK());
|
||||
}
|
||||
if ($modelConfig->getOutputMimeType() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::outputMimeType(), $modelConfig->getOutputMimeType());
|
||||
}
|
||||
if ($modelConfig->getOutputSchema() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::outputSchema(), $modelConfig->getOutputSchema());
|
||||
}
|
||||
// Handle properties without OptionEnum values as custom options
|
||||
if ($modelConfig->getStopSequences() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::stopSequences(), $modelConfig->getStopSequences());
|
||||
}
|
||||
if ($modelConfig->getPresencePenalty() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::presencePenalty(), $modelConfig->getPresencePenalty());
|
||||
}
|
||||
if ($modelConfig->getFrequencyPenalty() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::frequencyPenalty(), $modelConfig->getFrequencyPenalty());
|
||||
}
|
||||
if ($modelConfig->getLogprobs() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::logprobs(), $modelConfig->getLogprobs());
|
||||
}
|
||||
if ($modelConfig->getTopLogprobs() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::topLogprobs(), $modelConfig->getTopLogprobs());
|
||||
}
|
||||
if ($modelConfig->getFunctionDeclarations() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::functionDeclarations(), \true);
|
||||
}
|
||||
if ($modelConfig->getWebSearch() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::webSearch(), \true);
|
||||
}
|
||||
if ($modelConfig->getOutputFileType() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::outputFileType(), $modelConfig->getOutputFileType());
|
||||
}
|
||||
if ($modelConfig->getOutputMediaOrientation() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::outputMediaOrientation(), $modelConfig->getOutputMediaOrientation());
|
||||
}
|
||||
if ($modelConfig->getOutputMediaAspectRatio() !== null) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::outputMediaAspectRatio(), $modelConfig->getOutputMediaAspectRatio());
|
||||
}
|
||||
// Add custom options as individual RequiredOptions
|
||||
foreach ($modelConfig->getCustomOptions() as $key => $value) {
|
||||
$requiredOptions[] = new \WordPress\AiClient\Providers\Models\DTO\RequiredOption(OptionEnum::customOptions(), [$key => $value]);
|
||||
}
|
||||
return $requiredOptions;
|
||||
}
|
||||
/**
|
||||
* Includes a RequiredOption in the array, ensuring no duplicates based on option name.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*
|
||||
* @param list<RequiredOption> $requiredOptions The existing required options.
|
||||
* @param RequiredOption $newOption The new option to include.
|
||||
* @return list<RequiredOption> The updated required options array.
|
||||
*/
|
||||
private static function includeInRequiredOptions(array $requiredOptions, \WordPress\AiClient\Providers\Models\DTO\RequiredOption $newOption): array
|
||||
{
|
||||
// Check if we already have this option name
|
||||
foreach ($requiredOptions as $index => $existingOption) {
|
||||
if ($existingOption->getName()->equals($newOption->getName())) {
|
||||
// Replace existing option with new one
|
||||
$requiredOptions[$index] = $newOption;
|
||||
return $requiredOptions;
|
||||
}
|
||||
}
|
||||
// Option not found, add it
|
||||
$requiredOptions[] = $newOption;
|
||||
return $requiredOptions;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function getJsonSchema(): array
|
||||
{
|
||||
return ['type' => 'object', 'properties' => [self::KEY_REQUIRED_CAPABILITIES => ['type' => 'array', 'items' => ['type' => 'string', 'enum' => CapabilityEnum::getValues()], 'description' => 'The capabilities that the model must support.'], self::KEY_REQUIRED_OPTIONS => ['type' => 'array', 'items' => \WordPress\AiClient\Providers\Models\DTO\RequiredOption::getJsonSchema(), 'description' => 'The options that the model must support with specific values.']], 'required' => [self::KEY_REQUIRED_CAPABILITIES, self::KEY_REQUIRED_OPTIONS]];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return ModelRequirementsArrayShape
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [self::KEY_REQUIRED_CAPABILITIES => array_map(static fn(CapabilityEnum $capability): string => $capability->value, $this->requiredCapabilities), self::KEY_REQUIRED_OPTIONS => array_map(static fn(\WordPress\AiClient\Providers\Models\DTO\RequiredOption $option): array => $option->toArray(), $this->requiredOptions)];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
static::validateFromArrayData($array, [self::KEY_REQUIRED_CAPABILITIES, self::KEY_REQUIRED_OPTIONS]);
|
||||
return new self(array_map(static fn(string $capability): CapabilityEnum => CapabilityEnum::from($capability), $array[self::KEY_REQUIRED_CAPABILITIES]), array_map(static fn(array $optionData): \WordPress\AiClient\Providers\Models\DTO\RequiredOption => \WordPress\AiClient\Providers\Models\DTO\RequiredOption::fromArray($optionData), $array[self::KEY_REQUIRED_OPTIONS]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Providers\Models\DTO;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractDataTransferObject;
|
||||
use WordPress\AiClient\Providers\Models\Enums\OptionEnum;
|
||||
/**
|
||||
* Represents an option that the implementing code requires the model to support.
|
||||
*
|
||||
* This class defines an option that the model must support with a specific value
|
||||
* for it to be considered suitable for the implementing code's requirements.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @phpstan-type RequiredOptionArrayShape array{
|
||||
* name: string,
|
||||
* value: mixed
|
||||
* }
|
||||
*
|
||||
* @extends AbstractDataTransferObject<RequiredOptionArrayShape>
|
||||
*/
|
||||
class RequiredOption extends AbstractDataTransferObject
|
||||
{
|
||||
public const KEY_NAME = 'name';
|
||||
public const KEY_VALUE = 'value';
|
||||
/**
|
||||
* @var OptionEnum The option name.
|
||||
*/
|
||||
protected OptionEnum $name;
|
||||
/**
|
||||
* @var mixed The value that the model must support for this option.
|
||||
*/
|
||||
protected $value;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param OptionEnum $name The option name.
|
||||
* @param mixed $value The value that the model must support for this option.
|
||||
*/
|
||||
public function __construct(OptionEnum $name, $value)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->value = $value;
|
||||
}
|
||||
/**
|
||||
* Gets the option name.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return OptionEnum The option name.
|
||||
*/
|
||||
public function getName(): OptionEnum
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
/**
|
||||
* Gets the value that the model must support for this option.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return mixed The value that the model must support.
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function getJsonSchema(): array
|
||||
{
|
||||
return ['type' => 'object', 'properties' => [self::KEY_NAME => ['type' => 'string', 'enum' => OptionEnum::getValues(), 'description' => 'The option name.'], self::KEY_VALUE => ['oneOf' => [['type' => 'string'], ['type' => 'number'], ['type' => 'boolean'], ['type' => 'null'], ['type' => 'array'], ['type' => 'object']], 'description' => 'The value that the model must support for this option.']], 'required' => [self::KEY_NAME, self::KEY_VALUE]];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return RequiredOptionArrayShape
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [self::KEY_NAME => $this->name->value, self::KEY_VALUE => $this->value];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
static::validateFromArrayData($array, [self::KEY_NAME, self::KEY_VALUE]);
|
||||
return new self(OptionEnum::from($array[self::KEY_NAME]), $array[self::KEY_VALUE]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace WordPress\AiClient\Providers\Models\DTO;
|
||||
|
||||
use WordPress\AiClient\Common\AbstractDataTransferObject;
|
||||
use WordPress\AiClient\Common\AbstractEnum;
|
||||
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
|
||||
use WordPress\AiClient\Providers\Models\Enums\OptionEnum;
|
||||
/**
|
||||
* Represents a supported configuration option for an AI model.
|
||||
*
|
||||
* This class defines an option that a model supports, including its name
|
||||
* and the values that are valid for that option.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @phpstan-type SupportedOptionArrayShape array{
|
||||
* name: string,
|
||||
* supportedValues?: list<mixed>
|
||||
* }
|
||||
*
|
||||
* @extends AbstractDataTransferObject<SupportedOptionArrayShape>
|
||||
*/
|
||||
class SupportedOption extends AbstractDataTransferObject
|
||||
{
|
||||
public const KEY_NAME = 'name';
|
||||
public const KEY_SUPPORTED_VALUES = 'supportedValues';
|
||||
/**
|
||||
* @var OptionEnum The option name.
|
||||
*/
|
||||
protected OptionEnum $name;
|
||||
/**
|
||||
* @var list<mixed>|null The supported values for this option.
|
||||
*/
|
||||
protected ?array $supportedValues;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param OptionEnum $name The option name.
|
||||
* @param list<mixed>|null $supportedValues The supported values for this option, or null if any value is supported.
|
||||
*
|
||||
* @throws InvalidArgumentException If supportedValues is not null and not a list.
|
||||
*/
|
||||
public function __construct(OptionEnum $name, ?array $supportedValues = null)
|
||||
{
|
||||
if ($supportedValues !== null && !array_is_list($supportedValues)) {
|
||||
throw new InvalidArgumentException('Supported values must be a list array.');
|
||||
}
|
||||
$this->name = $name;
|
||||
$this->supportedValues = $supportedValues;
|
||||
}
|
||||
/**
|
||||
* Gets the option name.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return OptionEnum The option name.
|
||||
*/
|
||||
public function getName(): OptionEnum
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
/**
|
||||
* Checks if a value is supported for this option.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @param mixed $value The value to check.
|
||||
* @return bool True if the value is supported, false otherwise.
|
||||
*/
|
||||
public function isSupportedValue($value): bool
|
||||
{
|
||||
// If supportedValues is null, any value is supported
|
||||
if ($this->supportedValues === null) {
|
||||
return \true;
|
||||
}
|
||||
// If the value is an array, consider it a set (i.e. order doesn't matter).
|
||||
if (is_array($value)) {
|
||||
$normalizedValue = self::normalizeArrayForComparison($value);
|
||||
foreach ($this->supportedValues as $supportedValue) {
|
||||
if (!is_array($supportedValue)) {
|
||||
continue;
|
||||
}
|
||||
$normalizedSupported = self::normalizeArrayForComparison($supportedValue);
|
||||
if ($normalizedValue === $normalizedSupported) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
$normalizedValue = self::normalizeValue($value);
|
||||
foreach ($this->supportedValues as $supportedValue) {
|
||||
if (self::normalizeValue($supportedValue) === $normalizedValue) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
/**
|
||||
* Normalizes an AbstractEnum instance to its string value.
|
||||
*
|
||||
* This ensures comparisons work correctly even after deserialization
|
||||
* (e.g. Redis/Memcached object cache), where AbstractEnum singletons
|
||||
* are reconstructed as separate instances.
|
||||
*
|
||||
* @since 1.2.1
|
||||
*
|
||||
* @param mixed $value The value to normalize.
|
||||
* @return mixed The normalized value.
|
||||
*/
|
||||
private static function normalizeValue($value)
|
||||
{
|
||||
if ($value instanceof AbstractEnum) {
|
||||
return $value->value;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
/**
|
||||
* Normalizes and sorts an array for comparison.
|
||||
*
|
||||
* Maps each element through normalizeValue() and sorts the result,
|
||||
* ensuring consistent comparison regardless of element order or
|
||||
* AbstractEnum instance identity.
|
||||
*
|
||||
* @since 1.2.1
|
||||
*
|
||||
* @param array<mixed> $items The array to normalize.
|
||||
* @return array<mixed> The normalized, sorted array.
|
||||
*/
|
||||
private static function normalizeArrayForComparison(array $items): array
|
||||
{
|
||||
$normalized = array_map([self::class, 'normalizeValue'], $items);
|
||||
sort($normalized);
|
||||
return $normalized;
|
||||
}
|
||||
/**
|
||||
* Gets the supported values for this option.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return list<mixed>|null The supported values, or null if any value is supported.
|
||||
*/
|
||||
public function getSupportedValues(): ?array
|
||||
{
|
||||
return $this->supportedValues;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function getJsonSchema(): array
|
||||
{
|
||||
return ['type' => 'object', 'properties' => [self::KEY_NAME => ['type' => 'string', 'enum' => OptionEnum::getValues(), 'description' => 'The option name.'], self::KEY_SUPPORTED_VALUES => ['type' => 'array', 'items' => ['oneOf' => [['type' => 'string'], ['type' => 'number'], ['type' => 'boolean'], ['type' => 'null'], ['type' => 'array'], ['type' => 'object']]], 'description' => 'The supported values for this option.']], 'required' => [self::KEY_NAME]];
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @return SupportedOptionArrayShape
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$data = [self::KEY_NAME => $this->name->value];
|
||||
if ($this->supportedValues !== null) {
|
||||
/** @var list<mixed> $supportedValues */
|
||||
$supportedValues = $this->supportedValues;
|
||||
$data[self::KEY_SUPPORTED_VALUES] = $supportedValues;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
static::validateFromArrayData($array, [self::KEY_NAME]);
|
||||
return new self(OptionEnum::from($array[self::KEY_NAME]), $array[self::KEY_SUPPORTED_VALUES] ?? null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user