zip-ai
/
lib
/
php-mcp-schema
/
src
/
Client
/
Elicitation
/
Factory
/
PrimitiveSchemaDefinitionFactory.php
PrimitiveSchemaDefinitionFactory.php in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.8, at lib/php-mcp-schema/src/Client/Elicitation/Factory/PrimitiveSchemaDefinitionFactory.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\PrimitiveSchemaDefinitionInterface; |
| 8 | use WP\McpSchema\Client\Elicitation\DTO\StringSchema; |
| 9 | use WP\McpSchema\Client\Elicitation\DTO\NumberSchema; |
| 10 | use WP\McpSchema\Client\Elicitation\DTO\BooleanSchema; |
| 11 | use WP\McpSchema\Client\Elicitation\Factory\EnumSchemaFactory; |
| 12 | |
| 13 | /** |
| 14 | * Factory for creating PrimitiveSchemaDefinition union type instances. |
| 15 | * |
| 16 | * Restricted schema definitions that only allow primitive types |
| 17 | * without nested objects or arrays. |
| 18 | * |
| 19 | * @mcp-domain Client |
| 20 | * @mcp-subdomain Elicitation |
| 21 | * @mcp-version 2025-11-25 |
| 22 | */ |
| 23 | final class PrimitiveSchemaDefinitionFactory |
| 24 | { |
| 25 | /** |
| 26 | * Registry mapping discriminator values to implementation classes. |
| 27 | * Note: Some values route to other factories for nested union resolution. |
| 28 | * |
| 29 | * @var array<string, class-string> |
| 30 | */ |
| 31 | public const REGISTRY = [ |
| 32 | 'number' => NumberSchema::class, |
| 33 | 'integer' => NumberSchema::class, |
| 34 | 'boolean' => BooleanSchema::class, |
| 35 | 'array' => EnumSchemaFactory::class, |
| 36 | 'string' => StringSchema::class, |
| 37 | ]; |
| 38 | |
| 39 | /** |
| 40 | * Creates an instance from an array. |
| 41 | * |
| 42 | * @param array<string, mixed> $data |
| 43 | * @return PrimitiveSchemaDefinitionInterface |
| 44 | * @throws \InvalidArgumentException |
| 45 | */ |
| 46 | public static function fromArray(array $data): PrimitiveSchemaDefinitionInterface |
| 47 | { |
| 48 | if (!isset($data['type'])) { |
| 49 | throw new \InvalidArgumentException('Missing discriminator field: type'); |
| 50 | } |
| 51 | |
| 52 | switch ($data['type']) { |
| 53 | case 'number': |
| 54 | return NumberSchema::fromArray($data); |
| 55 | case 'integer': |
| 56 | return NumberSchema::fromArray($data); |
| 57 | case 'boolean': |
| 58 | return BooleanSchema::fromArray($data); |
| 59 | case 'array': |
| 60 | return EnumSchemaFactory::fromArray($data); |
| 61 | case 'string': |
| 62 | if (isset($data['minLength'])) { |
| 63 | return StringSchema::fromArray($data); |
| 64 | } |
| 65 | elseif (isset($data['enum'])) { |
| 66 | return EnumSchemaFactory::fromArray($data); |
| 67 | } |
| 68 | else { |
| 69 | return StringSchema::fromArray($data); |
| 70 | } |
| 71 | default: |
| 72 | throw new \InvalidArgumentException(sprintf( |
| 73 | "Unknown type value '%s'. Valid values: %s", |
| 74 | is_scalar($data['type']) ? $data['type'] : gettype($data['type']), |
| 75 | implode(', ', array_keys(self::REGISTRY)) |
| 76 | )); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Checks if a type value is supported by this factory. |
| 82 | * |
| 83 | * @param string $type |
| 84 | * @return bool |
| 85 | */ |
| 86 | public static function supports(string $type): bool |
| 87 | { |
| 88 | return isset(self::REGISTRY[$type]); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns all supported type values. |
| 93 | * |
| 94 | * @return array<string> |
| 95 | */ |
| 96 | public static function types(): array |
| 97 | { |
| 98 | return array_keys(self::REGISTRY); |
| 99 | } |
| 100 | } |
| 101 |