zip-ai
/
lib
/
php-mcp-schema
/
src
/
Client
/
Elicitation
/
Factory
/
SingleSelectEnumSchemaFactory.php
SingleSelectEnumSchemaFactory.php in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.8, at lib/php-mcp-schema/src/Client/Elicitation/Factory/SingleSelectEnumSchemaFactory.php
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace WP\McpSchema\Client\Elicitation\Factory; |
| 6 | |
| 7 | use WP\McpSchema\Client\Elicitation\Union\SingleSelectEnumSchemaInterface; |
| 8 | use WP\McpSchema\Client\Elicitation\DTO\UntitledSingleSelectEnumSchema; |
| 9 | use WP\McpSchema\Client\Elicitation\DTO\TitledSingleSelectEnumSchema; |
| 10 | |
| 11 | /** |
| 12 | * Factory for creating SingleSelectEnumSchema union type instances. |
| 13 | * |
| 14 | * @mcp-domain Client |
| 15 | * @mcp-subdomain Elicitation |
| 16 | * @mcp-version 2025-11-25 |
| 17 | */ |
| 18 | final class SingleSelectEnumSchemaFactory |
| 19 | { |
| 20 | /** |
| 21 | * Registry mapping discriminator values to implementation classes. |
| 22 | * |
| 23 | * @var array<string, class-string<SingleSelectEnumSchemaInterface>> |
| 24 | */ |
| 25 | public const REGISTRY = [ |
| 26 | 'string' => UntitledSingleSelectEnumSchema::class, |
| 27 | ]; |
| 28 | |
| 29 | /** |
| 30 | * Creates an instance from an array. |
| 31 | * |
| 32 | * @param array<string, mixed> $data |
| 33 | * @return SingleSelectEnumSchemaInterface |
| 34 | * @throws \InvalidArgumentException |
| 35 | */ |
| 36 | public static function fromArray(array $data): SingleSelectEnumSchemaInterface |
| 37 | { |
| 38 | if (!isset($data['type'])) { |
| 39 | throw new \InvalidArgumentException('Missing discriminator field: type'); |
| 40 | } |
| 41 | |
| 42 | switch ($data['type']) { |
| 43 | case 'string': |
| 44 | if (isset($data['enum'])) { |
| 45 | return UntitledSingleSelectEnumSchema::fromArray($data); |
| 46 | } |
| 47 | elseif (isset($data['oneOf'])) { |
| 48 | return TitledSingleSelectEnumSchema::fromArray($data); |
| 49 | } |
| 50 | else { |
| 51 | return UntitledSingleSelectEnumSchema::fromArray($data); |
| 52 | } |
| 53 | default: |
| 54 | throw new \InvalidArgumentException(sprintf( |
| 55 | "Unknown type value '%s'. Valid values: %s", |
| 56 | is_scalar($data['type']) ? $data['type'] : gettype($data['type']), |
| 57 | implode(', ', array_keys(self::REGISTRY)) |
| 58 | )); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Checks if a type value is supported by this factory. |
| 64 | * |
| 65 | * @param string $type |
| 66 | * @return bool |
| 67 | */ |
| 68 | public static function supports(string $type): bool |
| 69 | { |
| 70 | return isset(self::REGISTRY[$type]); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns all supported type values. |
| 75 | * |
| 76 | * @return array<string> |
| 77 | */ |
| 78 | public static function types(): array |
| 79 | { |
| 80 | return array_keys(self::REGISTRY); |
| 81 | } |
| 82 | } |
| 83 |