PluginProbe
ZIP AI – AI Website Builder & AI Agent (Beta) / 0.0.8
ZIP AI – AI Website Builder & AI Agent (Beta) v0.0.8
0.0.10 0.0.9 trunk 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8
zip-ai / lib / php-mcp-schema / src / Common / Protocol / Factory / ClientNotificationFactory.php

ClientNotificationFactory.php in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.8, at lib/php-mcp-schema/src/Common/Protocol/Factory/ClientNotificationFactory.php

84 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace WP\McpSchema\Common\Protocol\Factory;
6
7 use WP\McpSchema\Common\Protocol\Union\ClientNotificationInterface;
8 use WP\McpSchema\Common\Protocol\DTO\CancelledNotification;
9 use WP\McpSchema\Common\Protocol\DTO\ProgressNotification;
10 use WP\McpSchema\Common\Protocol\DTO\InitializedNotification;
11 use WP\McpSchema\Client\Roots\DTO\RootsListChangedNotification;
12 use WP\McpSchema\Common\Tasks\DTO\TaskStatusNotification;
13
14 /**
15 * Factory for creating ClientNotification union type instances.
16 *
17 * @mcp-domain Common
18 * @mcp-subdomain Protocol
19 * @mcp-version 2025-11-25
20 */
21 final class ClientNotificationFactory
22 {
23 /**
24 * Registry mapping discriminator values to implementation classes.
25 *
26 * @var array<string, class-string<ClientNotificationInterface>>
27 */
28 public const REGISTRY = [
29 'notifications/cancelled' => CancelledNotification::class,
30 'notifications/progress' => ProgressNotification::class,
31 'notifications/initialized' => InitializedNotification::class,
32 'notifications/roots/list_changed' => RootsListChangedNotification::class,
33 'notifications/tasks/status' => TaskStatusNotification::class,
34 ];
35
36 /**
37 * Creates an instance from an array.
38 *
39 * @param array<string, mixed> $data
40 * @return ClientNotificationInterface
41 * @throws \InvalidArgumentException
42 */
43 public static function fromArray(array $data): ClientNotificationInterface
44 {
45 if (!isset($data['method'])) {
46 throw new \InvalidArgumentException('Missing discriminator field: method');
47 }
48
49 /** @var string $method */
50 $method = $data['method'];
51 if (!isset(self::REGISTRY[$method])) {
52 throw new \InvalidArgumentException(sprintf(
53 "Unknown method value '%s'. Valid values: %s",
54 $method,
55 implode(', ', array_keys(self::REGISTRY))
56 ));
57 }
58
59 $class = self::REGISTRY[$method];
60 return $class::fromArray($data);
61 }
62
63 /**
64 * Checks if a method value is supported by this factory.
65 *
66 * @param string $method
67 * @return bool
68 */
69 public static function supports(string $method): bool
70 {
71 return isset(self::REGISTRY[$method]);
72 }
73
74 /**
75 * Returns all supported method values.
76 *
77 * @return array<string>
78 */
79 public static function methods(): array
80 {
81 return array_keys(self::REGISTRY);
82 }
83 }
84