| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Common\Protocol\Factory; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\Protocol\Union\ClientRequestInterface; |
| 8 |
use WP\McpSchema\Common\Protocol\DTO\PingRequest; |
| 9 |
use WP\McpSchema\Common\Protocol\DTO\InitializeRequest; |
| 10 |
use WP\McpSchema\Server\Core\DTO\CompleteRequest; |
| 11 |
use WP\McpSchema\Server\Logging\DTO\SetLevelRequest; |
| 12 |
use WP\McpSchema\Server\Prompts\DTO\GetPromptRequest; |
| 13 |
use WP\McpSchema\Server\Prompts\DTO\ListPromptsRequest; |
| 14 |
use WP\McpSchema\Server\Resources\DTO\ListResourcesRequest; |
| 15 |
use WP\McpSchema\Server\Resources\DTO\ListResourceTemplatesRequest; |
| 16 |
use WP\McpSchema\Server\Resources\DTO\ReadResourceRequest; |
| 17 |
use WP\McpSchema\Server\Resources\DTO\SubscribeRequest; |
| 18 |
use WP\McpSchema\Server\Resources\DTO\UnsubscribeRequest; |
| 19 |
use WP\McpSchema\Server\Tools\DTO\CallToolRequest; |
| 20 |
use WP\McpSchema\Server\Tools\DTO\ListToolsRequest; |
| 21 |
use WP\McpSchema\Common\Tasks\DTO\GetTaskRequest; |
| 22 |
use WP\McpSchema\Common\Protocol\DTO\GetTaskPayloadRequest; |
| 23 |
use WP\McpSchema\Common\Tasks\DTO\ListTasksRequest; |
| 24 |
use WP\McpSchema\Common\Tasks\DTO\CancelTaskRequest; |
| 25 |
|
| 26 |
/** |
| 27 |
* Factory for creating ClientRequest union type instances. |
| 28 |
* |
| 29 |
* @mcp-domain Common |
| 30 |
* @mcp-subdomain Protocol |
| 31 |
* @mcp-version 2025-11-25 |
| 32 |
*/ |
| 33 |
final class ClientRequestFactory |
| 34 |
{ |
| 35 |
/** |
| 36 |
* Registry mapping discriminator values to implementation classes. |
| 37 |
* |
| 38 |
* @var array<string, class-string<ClientRequestInterface>> |
| 39 |
*/ |
| 40 |
public const REGISTRY = [ |
| 41 |
'ping' => PingRequest::class, |
| 42 |
'initialize' => InitializeRequest::class, |
| 43 |
'completion/complete' => CompleteRequest::class, |
| 44 |
'logging/setLevel' => SetLevelRequest::class, |
| 45 |
'prompts/get' => GetPromptRequest::class, |
| 46 |
'prompts/list' => ListPromptsRequest::class, |
| 47 |
'resources/list' => ListResourcesRequest::class, |
| 48 |
'resources/templates/list' => ListResourceTemplatesRequest::class, |
| 49 |
'resources/read' => ReadResourceRequest::class, |
| 50 |
'resources/subscribe' => SubscribeRequest::class, |
| 51 |
'resources/unsubscribe' => UnsubscribeRequest::class, |
| 52 |
'tools/call' => CallToolRequest::class, |
| 53 |
'tools/list' => ListToolsRequest::class, |
| 54 |
'tasks/get' => GetTaskRequest::class, |
| 55 |
'tasks/result' => GetTaskPayloadRequest::class, |
| 56 |
'tasks/list' => ListTasksRequest::class, |
| 57 |
'tasks/cancel' => CancelTaskRequest::class, |
| 58 |
]; |
| 59 |
|
| 60 |
/** |
| 61 |
* Creates an instance from an array. |
| 62 |
* |
| 63 |
* @param array<string, mixed> $data |
| 64 |
* @return ClientRequestInterface |
| 65 |
* @throws \InvalidArgumentException |
| 66 |
*/ |
| 67 |
public static function fromArray(array $data): ClientRequestInterface |
| 68 |
{ |
| 69 |
if (!isset($data['method'])) { |
| 70 |
throw new \InvalidArgumentException('Missing discriminator field: method'); |
| 71 |
} |
| 72 |
|
| 73 |
/** @var string $method */ |
| 74 |
$method = $data['method']; |
| 75 |
if (!isset(self::REGISTRY[$method])) { |
| 76 |
throw new \InvalidArgumentException(sprintf( |
| 77 |
"Unknown method value '%s'. Valid values: %s", |
| 78 |
$method, |
| 79 |
implode(', ', array_keys(self::REGISTRY)) |
| 80 |
)); |
| 81 |
} |
| 82 |
|
| 83 |
$class = self::REGISTRY[$method]; |
| 84 |
return $class::fromArray($data); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Checks if a method value is supported by this factory. |
| 89 |
* |
| 90 |
* @param string $method |
| 91 |
* @return bool |
| 92 |
*/ |
| 93 |
public static function supports(string $method): bool |
| 94 |
{ |
| 95 |
return isset(self::REGISTRY[$method]); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns all supported method values. |
| 100 |
* |
| 101 |
* @return array<string> |
| 102 |
*/ |
| 103 |
public static function methods(): array |
| 104 |
{ |
| 105 |
return array_keys(self::REGISTRY); |
| 106 |
} |
| 107 |
} |
| 108 |
|