| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Server\Lifecycle\Factory; |
| 6 |
|
| 7 |
use WP\McpSchema\Server\Lifecycle\Union\ServerNotificationInterface; |
| 8 |
use WP\McpSchema\Common\Protocol\DTO\CancelledNotification; |
| 9 |
use WP\McpSchema\Common\Protocol\DTO\ProgressNotification; |
| 10 |
use WP\McpSchema\Server\Logging\DTO\LoggingMessageNotification; |
| 11 |
use WP\McpSchema\Server\Resources\DTO\ResourceUpdatedNotification; |
| 12 |
use WP\McpSchema\Server\Resources\DTO\ResourceListChangedNotification; |
| 13 |
use WP\McpSchema\Server\Tools\DTO\ToolListChangedNotification; |
| 14 |
use WP\McpSchema\Server\Prompts\DTO\PromptListChangedNotification; |
| 15 |
use WP\McpSchema\Client\Elicitation\DTO\ElicitationCompleteNotification; |
| 16 |
use WP\McpSchema\Common\Tasks\DTO\TaskStatusNotification; |
| 17 |
|
| 18 |
/** |
| 19 |
* Factory for creating ServerNotification union type instances. |
| 20 |
* |
| 21 |
* @mcp-domain Server |
| 22 |
* @mcp-subdomain Lifecycle |
| 23 |
* @mcp-version 2025-11-25 |
| 24 |
*/ |
| 25 |
final class ServerNotificationFactory |
| 26 |
{ |
| 27 |
/** |
| 28 |
* Registry mapping discriminator values to implementation classes. |
| 29 |
* |
| 30 |
* @var array<string, class-string<ServerNotificationInterface>> |
| 31 |
*/ |
| 32 |
public const REGISTRY = [ |
| 33 |
'notifications/cancelled' => CancelledNotification::class, |
| 34 |
'notifications/progress' => ProgressNotification::class, |
| 35 |
'notifications/message' => LoggingMessageNotification::class, |
| 36 |
'notifications/resources/updated' => ResourceUpdatedNotification::class, |
| 37 |
'notifications/resources/list_changed' => ResourceListChangedNotification::class, |
| 38 |
'notifications/tools/list_changed' => ToolListChangedNotification::class, |
| 39 |
'notifications/prompts/list_changed' => PromptListChangedNotification::class, |
| 40 |
'notifications/elicitation/complete' => ElicitationCompleteNotification::class, |
| 41 |
'notifications/tasks/status' => TaskStatusNotification::class, |
| 42 |
]; |
| 43 |
|
| 44 |
/** |
| 45 |
* Creates an instance from an array. |
| 46 |
* |
| 47 |
* @param array<string, mixed> $data |
| 48 |
* @return ServerNotificationInterface |
| 49 |
* @throws \InvalidArgumentException |
| 50 |
*/ |
| 51 |
public static function fromArray(array $data): ServerNotificationInterface |
| 52 |
{ |
| 53 |
if (!isset($data['method'])) { |
| 54 |
throw new \InvalidArgumentException('Missing discriminator field: method'); |
| 55 |
} |
| 56 |
|
| 57 |
/** @var string $method */ |
| 58 |
$method = $data['method']; |
| 59 |
if (!isset(self::REGISTRY[$method])) { |
| 60 |
throw new \InvalidArgumentException(sprintf( |
| 61 |
"Unknown method value '%s'. Valid values: %s", |
| 62 |
$method, |
| 63 |
implode(', ', array_keys(self::REGISTRY)) |
| 64 |
)); |
| 65 |
} |
| 66 |
|
| 67 |
$class = self::REGISTRY[$method]; |
| 68 |
return $class::fromArray($data); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Checks if a method value is supported by this factory. |
| 73 |
* |
| 74 |
* @param string $method |
| 75 |
* @return bool |
| 76 |
*/ |
| 77 |
public static function supports(string $method): bool |
| 78 |
{ |
| 79 |
return isset(self::REGISTRY[$method]); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns all supported method values. |
| 84 |
* |
| 85 |
* @return array<string> |
| 86 |
*/ |
| 87 |
public static function methods(): array |
| 88 |
{ |
| 89 |
return array_keys(self::REGISTRY); |
| 90 |
} |
| 91 |
} |
| 92 |
|