1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2025 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\Schema\Tool\ToolSchema;
15:
16: /**
17: * The `number` type is used for validating integer and float values.
18: *
19: * @extends ToolSchema<float|int, array{
20: * type: 'number',
21: * description?: non-empty-string,
22: * title?: non-empty-string,
23: * minimum?: float|int,
24: * maximum?: float|int,
25: * default?: float|int,
26: * }>
27: */
28: final class NumberType extends ToolSchema
29: {
30: private null|float|int $minimum = null;
31: private null|float|int $maximum = null;
32: private null|float|int $multipleOf = null;
33:
34: public function minimum(float|int $minimum): self
35: {
36: $this->minimum = $minimum;
37:
38: return $this;
39: }
40:
41: public function maximum(float|int $maximum): self
42: {
43: $this->maximum = $maximum;
44:
45: return $this;
46: }
47:
48: public function multipleOf(float|int $multipleOf): self
49: {
50: if ($multipleOf <= 0) {
51: throw new \InvalidArgumentException('The multipleOf value must be greater than zero.');
52: }
53:
54: $this->multipleOf = $multipleOf;
55:
56: return $this;
57: }
58:
59: #[\Override]
60: public function toArray(): array
61: {
62: if (isset($this->minimum, $this->maximum) && $this->minimum > $this->maximum) {
63: throw new \LogicException('The minimum value cannot be greater than the maximum value.');
64: }
65:
66: if (isset($this->default, $this->minimum) && $this->default < $this->minimum) {
67: throw new \LogicException('The default value cannot be less than the minimum value.');
68: }
69:
70: if (isset($this->default, $this->maximum) && $this->default > $this->maximum) {
71: throw new \LogicException('The default value cannot be greater than the maximum value.');
72: }
73:
74: return array_filter([
75: 'type' => 'number',
76: 'description' => $this->description,
77: 'title' => $this->title,
78: 'minimum' => $this->minimum,
79: 'maximum' => $this->maximum,
80: 'multipleOf' => $this->multipleOf,
81: 'default' => $this->default,
82: ], static fn(mixed $value): bool => null !== $value);
83: }
84: }
85: