Skip to content

Commit f039465

Browse files
committed
Multiple Changes
Composer: - Bump php dep to 8.2 - Add ext-json as requirement Response: - move error package describing error member to response package Server: - Split notification and request middleware Documentation: - add links and test from json-RPC 2.0 spec to phpdocs Client: - add client Interface
1 parent 5611738 commit f039465

20 files changed

+224
-37
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
]
3434
},
3535
"require": {
36-
"php": "^8.1"
36+
"php": "^8.2",
37+
"ext-json": "*"
3738
}
3839
}

composer.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ClientIface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Haeckel\JsonRpcServerContract;
6+
7+
use Haeckel\JsonRpcServerContract\{Message, Response};
8+
9+
interface ClientIface
10+
{
11+
public function sendRequest(
12+
Message\RequestIface $request,
13+
): Response\SuccessIface|Response\ErrorIface;
14+
15+
public function sendBatch(
16+
Message\BatchRequestIface $batchRequest,
17+
): null|Response\BatchIface|Response\ErrorIface;
18+
19+
public function sendNotification(Message\NotificationIface $notification): void;
20+
}

src/Exception/JsonRpcErrorIface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
namespace Haeckel\JsonRpcServerContract\Exception;
2424

25-
use Haeckel\JsonRpcServerContract\Message;
25+
use Haeckel\JsonRpcServerContract\{Message, Response};
2626

2727
interface JsonRpcErrorIface extends \Throwable
2828
{
29-
public function getErrorObject(): Message\ErrorObjectIface;
29+
public function getErrorObject(): Response\Error\ErrObjectIface;
3030
public function getRequest(): ?Message\RequestIface;
3131
}

src/Message/BatchRequestIface.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525
use Haeckel\JsonRpcServerContract\{DataStruct, Response};
2626

2727
/**
28-
* @extends DataStruct\CollectionIface<RequestIface|NotificationIface>
28+
* To send several Request objects at the same time,
29+
* the Client MAY send an Array filled with Request objects.
2930
*
3031
* @link https://www.jsonrpc.org/specification#batch
32+
*
33+
* @extends DataStruct\CollectionIface<RequestIface|NotificationIface>
34+
*
3135
*/
3236
interface BatchRequestIface extends DataStruct\CollectionIface
3337
{

src/Message/NotificationIface.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,52 @@
2222

2323
namespace Haeckel\JsonRpcServerContract\Message;
2424

25-
/** @link https://www.jsonrpc.org/specification#notification */
25+
/**
26+
* A Notification is a Request object without an "id" member.
27+
* A Request object that is a Notification signifies the Client's lack of interest in the
28+
* corresponding Response object, and as such no Response object needs to be returned to the client.
29+
* The Server MUST NOT reply to a Notification, including those that are within a batch request.
30+
*
31+
* Notifications are not confirmable by definition,
32+
* since they do not have a Response object to be returned.
33+
* As such, the Client would not be aware of any errors
34+
* (like e.g. "Invalid params","Internal error").
35+
* @link https://www.jsonrpc.org/specification#notification
36+
*/
2637
interface NotificationIface extends \JsonSerializable
2738
{
2839
public function getJsonRpc(): string;
29-
public function withJsonRpc(string $jsonRpc): static;
40+
/**
41+
* @param string $jsonrpc
42+
* A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
43+
*/
44+
public function withJsonRpc(string $jsonrpc): static;
3045

3146
public function getMethod(): string;
47+
/**
48+
* @param string $method
49+
* A String containing the name of the method to be invoked.
50+
* Method names that begin with the word rpc followed by a period character (U+002E or ASCII 46)
51+
* are reserved for rpc-internal methods and extensions and MUST NOT be used for anything else.
52+
*/
3253
public function withMethod(string $method): static;
3354

34-
/** @return null|object|array<mixed> */
55+
/** @return null|object|list<mixed> */
3556
public function getParams(): null|array|object;
36-
/** @param null|object|array<mixed> $params */
57+
/**
58+
* @param null|object|list<mixed> $params
59+
* A Structured value that holds the parameter values to be used during the invocation of the
60+
* method. This member MAY be omitted (in this case with null).
61+
*
62+
* If present, parameters for the rpc call MUST be provided as a Structured value.
63+
* Either by-position through an Array or by-name through an Object.
64+
*
65+
* by-position: params MUST be an Array, containing the values in the Server expected order.
66+
* by-name: params MUST be an Object, with member names that match the Server expected parameter
67+
* names. The absence of expected names MAY result in an error being generated.
68+
* The names MUST match exactly, including case, to the method's expected parameters.
69+
*
70+
* @link https://www.jsonrpc.org/specification#parameter_structures
71+
*/
3772
public function withParams(null|array|object $params): static;
3873
}

src/Message/RequestIface.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,23 @@
2222

2323
namespace Haeckel\JsonRpcServerContract\Message;
2424

25-
/** @link https://www.jsonrpc.org/specification#request_object */
25+
/**
26+
* A rpc call is represented by sending a Request object to a Server.
27+
*
28+
* The Server MUST reply with the same value in the Response object if included.
29+
* This member is used to correlate the context between the two objects.
30+
* @link https://www.jsonrpc.org/specification#request_object
31+
*/
2632
interface RequestIface extends NotificationIface
2733
{
2834
public function getId(): int|string;
35+
/**
36+
* @param int|string $id
37+
* An identifier established by the Client that MUST contain a String, Number, or NULL value.
38+
* The value SHOULD normally not be Null [1] and Numbers SHOULD NOT contain fractional parts [2]
39+
*
40+
* ATTENTION: this implementation is strict with the with the interpretation of allowable types
41+
* and only allows string and int as it's type.
42+
*/
2943
public function withId(int|string $id): static;
3044
}

src/Response/BaseIface.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
<?php
22

3+
/**
4+
* This file is part of haeckel/json-rpc-server-contracts.
5+
*
6+
* haeckel/json-rpc-server-contracts is free software:
7+
* you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
8+
* as published by the Free Software Foundation, either version 3 of the License,
9+
* or (at your option) any later version.
10+
*
11+
* haeckel/json-rpc-server-contracts is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY;
13+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License along with
17+
* haeckel/json-rpc-server-contracts.
18+
* If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
321
declare(strict_types=1);
422

523
namespace Haeckel\JsonRpcServerContract\Response;
624

25+
/**
26+
* When a rpc call is made, the Server MUST reply with a Response,
27+
* except for in the case of Notifications.
28+
* The Response is expressed as a single JSON Object
29+
*
30+
* @link https://www.jsonrpc.org/specification#response_object
31+
*/
732
interface BaseIface extends \JsonSerializable
833
{
934
public function getId(): null|int|string;
35+
/**
36+
* @param null|int|string $id
37+
* It MUST be the same as the value of the id member in the Request Object.
38+
* If there was an error in detecting the id in the Request object
39+
* (e.g. Parse error/Invalid Request), it MUST be Null.
40+
*/
1041
public function withId(null|int|string $id): static;
1142

1243
public function getJsonrpc(): string;
44+
/**
45+
* @param string $jsonrpc
46+
* A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
47+
*/
1348
public function withJsonrpc(string $jsonrpc): static;
1449
}

src/Response/BatchIface.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,25 @@
2525
use Haeckel\JsonRpcServerContract\DataStruct\CollectionIface;
2626

2727
/**
28-
* @extends CollectionIface<ErrorIface|SuccessIface>
28+
* The Server should respond with an Array containing the corresponding Response objects,
29+
* after all of the batch Request objects have been processed.
30+
* A Response object SHOULD exist for each Request object,
31+
* except that there SHOULD NOT be any Response objects for notifications.
32+
* The Server MAY process a batch rpc call as a set of concurrent tasks,
33+
* processing them in any order and with any width of parallelism.
34+
*
35+
* The Response objects being returned from a batch call MAY be returned in any order within the
36+
* Array. The Client SHOULD match contexts between the set of Request objects and the resulting set
37+
* of Response objects based on the id member within each Object.
38+
*
39+
* If the batch rpc call itself fails to be recognized as an valid JSON or as an Array with at least
40+
* one value, the response from the Server MUST be a single Response object.
41+
* If there are no Response objects contained within the Response array as it is to be sent to the
42+
* client, the server MUST NOT return an empty Array and should return nothing at all.
2943
*
3044
* @link https://www.jsonrpc.org/specification#batch
45+
*
46+
* @extends CollectionIface<ErrorIface|SuccessIface>
3147
*/
3248
interface BatchIface extends CollectionIface
3349
{

src/Message/ErrObj/ErrCodeIface.php renamed to src/Response/Error/ErrCodeIface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
declare(strict_types=1);
2222

23-
namespace Haeckel\JsonRpcServerContract\Message\ErrObj;
23+
namespace Haeckel\JsonRpcServerContract\Response\Error;
2424

2525
/** @link @link https://www.jsonrpc.org/specification#error_object */
2626
interface ErrCodeIface

0 commit comments

Comments
 (0)