Skip to content

Commit 5611738

Browse files
committed
Restructure message package
Move response classes to own package Split ResponseIface into Err and Success respopnse ifaces
1 parent 629d934 commit 5611738

File tree

9 files changed

+83
-74
lines changed

9 files changed

+83
-74
lines changed

src/Message/BatchRequestIface.php

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

2323
namespace Haeckel\JsonRpcServerContract\Message;
2424

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

2727
/**
28-
* @extends CollectionIface<RequestIface|NotificationIface>
28+
* @extends DataStruct\CollectionIface<RequestIface|NotificationIface>
2929
*
3030
* @link https://www.jsonrpc.org/specification#batch
3131
*/
32-
interface BatchRequestIface extends CollectionIface
32+
interface BatchRequestIface extends DataStruct\CollectionIface
3333
{
3434
/** @no-named-arguments */
3535
public function add(RequestIface|NotificationIface ...$elements): void;
@@ -43,10 +43,10 @@ public function remove(RequestIface|NotificationIface ...$elements): void;
4343
* if any request of a batch is invalid or hast invalid json, add the error response here
4444
* @no-named-arguments
4545
*/
46-
public function addResponseForInvalidReq(ResponseIface ...$response): void;
46+
public function addResponseForInvalidReq(Response\ErrorIface ...$response): void;
4747

4848
/**
49-
* @return list<ResponseIface>
49+
* @return list<Response\ErrorIface>
5050
*/
5151
public function getResponsesForInvalidRequests(): array;
5252
}

src/Message/ResponseIface.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Response/BaseIface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Haeckel\JsonRpcServerContract\Response;
6+
7+
interface BaseIface extends \JsonSerializable
8+
{
9+
public function getId(): null|int|string;
10+
public function withId(null|int|string $id): static;
11+
12+
public function getJsonrpc(): string;
13+
public function withJsonrpc(string $jsonrpc): static;
14+
}

src/Message/BatchResponseIface.php renamed to src/Response/BatchIface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@
2020

2121
declare(strict_types=1);
2222

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

2525
use Haeckel\JsonRpcServerContract\DataStruct\CollectionIface;
2626

2727
/**
28-
* @extends CollectionIface<ResponseIface>
28+
* @extends CollectionIface<ErrorIface|SuccessIface>
2929
*
3030
* @link https://www.jsonrpc.org/specification#batch
3131
*/
32-
interface BatchResponseIface extends CollectionIface
32+
interface BatchIface extends CollectionIface
3333
{
3434
/** @no-named-arguments */
35-
public function add(ResponseIface ...$elements): void;
35+
public function add(ErrorIface|SuccessIface ...$elements): void;
3636

37-
public function current(): ?ResponseIface;
37+
public function current(): null|ErrorIface|SuccessIface;
3838

3939
/** @no-named-arguments */
40-
public function remove(ResponseIface ...$elements): void;
40+
public function remove(ErrorIface|SuccessIface ...$elements): void;
4141
}

src/Response/ErrorIface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Haeckel\JsonRpcServerContract\Response;
6+
7+
use Haeckel\JsonRpcServerContract\Message;
8+
9+
interface ErrorIface extends BaseIface
10+
{
11+
public function getError(): null|Message\ErrorObjectIface;
12+
public function withError(null|Message\ErrorObjectIface $error): static;
13+
}

src/Response/SuccessIface.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
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+
21+
declare(strict_types=1);
22+
23+
namespace Haeckel\JsonRpcServerContract\Response;
24+
25+
/** @link https://www.jsonrpc.org/specification#response_object */
26+
interface SuccessIface extends BaseIface
27+
{
28+
/** @return array<mixed>|bool|float|int|string|\stdClass|\JsonSerializable */
29+
public function getResult(): array|bool|float|int|string|\stdClass|\JsonSerializable;
30+
31+
/** @param array<mixed>|bool|float|int|string|\stdClass|\JsonSerializable $result */
32+
public function withResult(
33+
array|bool|float|int|string|\stdClass|\JsonSerializable $result,
34+
): static;
35+
}

src/Server/EmitterIface.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222

2323
namespace Haeckel\JsonRpcServerContract\Server;
2424

25-
use Haeckel\JsonRpcServerContract\Message;
25+
use Haeckel\JsonRpcServerContract\Response;
2626

2727
/** serialize the response and output it */
2828
interface EmitterIface
2929
{
30-
/** @throws \Throwable */
31-
public function emit(Message\ResponseIface|Message\BatchResponseIface $response): void;
30+
public function emit(
31+
Response\BatchIface|Response\ErrorIface|Response\SuccessIface $response,
32+
): void;
3233
}

src/Server/MiddlewareIface.php

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

2323
namespace Haeckel\JsonRpcServerContract\Server;
2424

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

2727
/**
2828
* can be used compose classes the offer general services,
@@ -33,5 +33,5 @@ interface MiddlewareIface
3333
public function process(
3434
Message\NotificationIface|Message\RequestIface $request,
3535
RequestHandlerIface|NotificationHandlerIface $handler,
36-
): ?Message\ResponseIface;
36+
): null|Response\ErrorIface|Response\SuccessIface;
3737
}

src/Server/RequestHandlerIface.php

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

2323
namespace Haeckel\JsonRpcServerContract\Server;
2424

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

2727
interface RequestHandlerIface
2828
{
29-
/** @throws Exception\JsonRpcErrorIface */
30-
public function handle(Message\RequestIface $request): Message\ResponseIface;
29+
public function handle(
30+
Message\RequestIface $request,
31+
): Response\ErrorIface|Response\SuccessIface;
3132
}

0 commit comments

Comments
 (0)