-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMathApiProvider.php
More file actions
143 lines (131 loc) · 5.14 KB
/
MathApiProvider.php
File metadata and controls
143 lines (131 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/**
* Copyright 2026 The Horde Project (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*/
namespace Horde\Rpc\JsonRpc\Dispatch;
use Horde\Rpc\JsonRpc\Exception\InvalidParamsException;
/**
* Illustrational API provider with basic math operations.
*
* Registers four methods: math.add, math.subtract, math.multiply, math.divide.
* Serves as documentation-by-code for how to build an ApiProvider.
*
* Example usage:
*
* $provider = MathApiProvider::create();
* $dispatcher = new Dispatcher($provider, $provider);
*/
final class MathApiProvider implements ApiProviderInterface, MethodInvokerInterface
{
private readonly CallableMapProvider $inner;
private function __construct()
{
$this->inner = new CallableMapProvider(
[
'math.add' => fn(float $a, float $b): float => $a + $b,
'math.subtract' => fn(float $a, float $b): float => $a - $b,
'math.multiply' => fn(float $a, float $b): float => $a * $b,
'math.divide' => static function (float $a, float $b): float {
if ($b == 0.0) {
throw new InvalidParamsException('Division by zero');
}
return $a / $b;
},
],
[
'math.add' => new MethodDescriptor(
'math.add',
'Add two numbers',
[
['name' => 'a', 'type' => 'float', 'required' => true],
['name' => 'b', 'type' => 'float', 'required' => true],
],
'float',
inputSchema: [
'type' => 'object',
'properties' => [
'a' => ['type' => 'number', 'description' => 'First number'],
'b' => ['type' => 'number', 'description' => 'Second number'],
],
'required' => ['a', 'b'],
],
),
'math.subtract' => new MethodDescriptor(
'math.subtract',
'Subtract b from a',
[
['name' => 'a', 'type' => 'float', 'required' => true],
['name' => 'b', 'type' => 'float', 'required' => true],
],
'float',
inputSchema: [
'type' => 'object',
'properties' => [
'a' => ['type' => 'number', 'description' => 'Minuend'],
'b' => ['type' => 'number', 'description' => 'Subtrahend'],
],
'required' => ['a', 'b'],
],
),
'math.multiply' => new MethodDescriptor(
'math.multiply',
'Multiply two numbers',
[
['name' => 'a', 'type' => 'float', 'required' => true],
['name' => 'b', 'type' => 'float', 'required' => true],
],
'float',
inputSchema: [
'type' => 'object',
'properties' => [
'a' => ['type' => 'number', 'description' => 'First factor'],
'b' => ['type' => 'number', 'description' => 'Second factor'],
],
'required' => ['a', 'b'],
],
),
'math.divide' => new MethodDescriptor(
'math.divide',
'Divide a by b (throws InvalidParamsException on division by zero)',
[
['name' => 'a', 'type' => 'float', 'required' => true],
['name' => 'b', 'type' => 'float', 'required' => true],
],
'float',
inputSchema: [
'type' => 'object',
'properties' => [
'a' => ['type' => 'number', 'description' => 'Dividend'],
'b' => ['type' => 'number', 'description' => 'Divisor (must not be zero)'],
],
'required' => ['a', 'b'],
],
),
],
);
}
public static function create(): self
{
return new self();
}
public function hasMethod(string $method): bool
{
return $this->inner->hasMethod($method);
}
public function getMethodDescriptor(string $method): ?MethodDescriptor
{
return $this->inner->getMethodDescriptor($method);
}
public function listMethods(): array
{
return $this->inner->listMethods();
}
public function invoke(string $method, array $params): Result
{
return $this->inner->invoke($method, $params);
}
}