1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Core\Schema\NotificationParams;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Arrayable;
18: use Nexus\Mcp\Core\Schema\MetaObject;
19: use Nexus\Mcp\Core\Schema\NotificationParams;
20: use Nexus\Mcp\Core\Schema\RequestId;
21:
22: /**
23: * Parameters for a `notifications/cancelled` notification.
24: *
25: * @extends NotificationParams<array{
26: * _meta?: template-type<MetaObject, Arrayable, 'T'>,
27: * requestId: int|non-empty-string,
28: * reason?: non-empty-string,
29: * }>
30: *
31: * @see https://modelcontextprotocol.io/specification/draft/schema#cancellednotificationparams
32: */
33: final readonly class CancelledNotificationParams extends NotificationParams
34: {
35: /**
36: * @var null|non-empty-string
37: */
38: public ?string $reason;
39:
40: public function __construct(
41: public RequestId $requestId,
42: ?string $reason = null,
43: MetaObject $meta = new MetaObject(),
44: ) {
45: Assert::that($reason)->nullOr()->isNonEmptyString('"params.reason" must be a non-empty string or null.');
46:
47: $this->reason = $reason;
48:
49: parent::__construct(meta: $meta);
50: }
51:
52: #[\Override]
53: public static function fromArray(array $data): static
54: {
55: Assert::that($data)->hasOffset('requestId', '"params" is missing the required "requestId" key.');
56: Assert::that($data['requestId'])->isArrayKey('"params.requestId" must be an int or string, {type} given.');
57: $requestId = new RequestId(id: $data['requestId']);
58:
59: $reason = $data['reason'] ?? null;
60: Assert::that($reason)->nullOr()->isString('"params.reason" must be a string or null, {type} given.');
61:
62: $meta = new MetaObject();
63:
64: if (\array_key_exists('_meta', $data)) {
65: Assert::that($data['_meta'])
66: ->isArray('"params._meta" must be an object, {type} given.')
67: ->isMap('"params._meta" must be a string-keyed object.')
68: ;
69: $meta = MetaObject::fromArray($data['_meta']);
70: }
71:
72: return new self(requestId: $requestId, reason: $reason, meta: $meta);
73: }
74:
75: #[\Override]
76: public function toArray(): array
77: {
78: $data = [];
79: $meta = $this->meta->toArray();
80:
81: if ([] !== $meta) {
82: $data['_meta'] = $meta;
83: }
84:
85: $data['requestId'] = $this->requestId->id;
86:
87: if (null !== $this->reason) {
88: $data['reason'] = $this->reason;
89: }
90:
91: return $data;
92: }
93:
94: #[\Override]
95: public function jsonSerialize(): array
96: {
97: return $this->toArray();
98: }
99: }
100: