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\Enum\LoggingLevel;
18: use Nexus\Mcp\Core\Schema\MetaObject;
19: use Nexus\Mcp\Core\Schema\NotificationParams;
20: use Nexus\Mcp\Core\Validation\EnumValueValidator;
21:
22: /**
23: * Parameters for a `notifications/message` notification.
24: *
25: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#loggingmessagenotificationparams
26: */
27: final readonly class LoggingMessageNotificationParams extends NotificationParams
28: {
29: public function __construct(
30: public LoggingLevel $level,
31: public mixed $data,
32: public ?string $logger = null,
33: MetaObject $meta = new MetaObject(),
34: ) {
35: parent::__construct($meta);
36: }
37:
38: /**
39: * @param array<string, mixed> $data
40: */
41: #[\Override]
42: public static function fromArray(array $data): static
43: {
44: Assert::that($data)->hasOffset('level', 'missing the required "level" key.');
45: $level = EnumValueValidator::parse(LoggingLevel::class, $data['level'], '"params.level"');
46:
47: Assert::that($data)->hasOffset('data', 'missing the required "data" key.');
48:
49: $logger = $data['logger'] ?? null;
50: Assert::that($logger)->nullOr()->isString('"params.logger" must be a string or null, {type} given.');
51:
52: $meta = new MetaObject();
53:
54: if (\array_key_exists('_meta', $data)) {
55: Assert::that($data['_meta'])
56: ->isArray('"params._meta" must be an object, {type} given.')
57: ->isMap('"params._meta" must be a string-keyed object.')
58: ;
59: $meta = MetaObject::fromArray($data['_meta']);
60: }
61:
62: return new self($level, $data['data'], $logger, $meta);
63: }
64:
65: #[\Override]
66: public function toArray(): array
67: {
68: $data = [
69: ...parent::toArray(),
70: 'level' => $this->level->value,
71: 'data' => $this->data,
72: ];
73:
74: if (null !== $this->logger) {
75: $data['logger'] = $this->logger;
76: }
77:
78: return $data;
79: }
80:
81: #[\Override]
82: public function jsonSerialize(): array
83: {
84: return $this->toArray();
85: }
86: }
87: