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\MetaObject;
18: use Nexus\Mcp\Core\Schema\MetaObject\NotificationMetaObject;
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<NotificationMetaObject, MetaObject, 'T'>,
27: * requestId: int|non-empty-string,
28: * reason?: non-empty-string,
29: * }>
30: *
31: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#cancellednotificationparams
32: */
33: final readonly class CancelledNotificationParams extends NotificationParams
34: {
35: /**
36: * @param null|non-empty-string $reason
37: */
38: public function __construct(
39: public RequestId $requestId,
40: public ?string $reason = null,
41: NotificationMetaObject $meta = new NotificationMetaObject(),
42: ) {
43: Assert::that($reason)->nullOr()->isNonEmptyString('"params.reason" must be a non-empty string or null.');
44:
45: parent::__construct(meta: $meta);
46: }
47:
48: #[\Override]
49: public static function fromArray(array $data): static
50: {
51: Assert::that($data)->hasOffset('requestId', '"params" is missing the required "requestId" key.');
52: Assert::that($data['requestId'])->isIntOrNonEmptyString('"params.requestId" must be an int or non-empty string, {type} given.');
53: $requestId = new RequestId(id: $data['requestId']);
54:
55: $reason = $data['reason'] ?? null;
56: Assert::that($reason)->nullOr()->isNonEmptyString('"params.reason" must be a non-empty string or null, {type} given.');
57:
58: $meta = new NotificationMetaObject();
59:
60: if (\array_key_exists('_meta', $data)) {
61: Assert::that($data['_meta'])
62: ->isArray('"params._meta" must be an object, {type} given.')
63: ->not()->isNonEmptyList('"params._meta" must be a string-keyed object.')
64: ;
65: $meta = NotificationMetaObject::fromArray($data['_meta']);
66: }
67:
68: return new self(requestId: $requestId, reason: $reason, meta: $meta);
69: }
70:
71: #[\Override]
72: public function toArray(): array
73: {
74: $data = [];
75: $meta = $this->meta->toArray();
76:
77: if ([] !== $meta) {
78: $data['_meta'] = $meta;
79: }
80:
81: $data['requestId'] = $this->requestId->id;
82:
83: if (null !== $this->reason) {
84: $data['reason'] = $this->reason;
85: }
86:
87: return $data;
88: }
89:
90: #[\Override]
91: public function jsonSerialize(): array
92: {
93: return $this->toArray();
94: }
95: }
96: