Skip to content

Commit 7501cad

Browse files
committed
Update PHPDoc
1 parent 3a435ad commit 7501cad

File tree

4 files changed

+108
-32
lines changed

4 files changed

+108
-32
lines changed

src/JsonResponse.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,25 @@
1818
use Nyholm\Psr7\Response;
1919

2020
/**
21-
* @method getBody(): JsonStreamInterface
21+
* Class JsonResponse.
22+
*
23+
* Provides a JSON-specific HTTP response implementation that complies with PSR-7 Response interfaces.
24+
* This class MUST be used when returning JSON payloads over HTTP responses.
25+
* It automatically sets the 'Content-Type' header to 'application/json' with the specified charset.
26+
*
27+
* @method JsonStreamInterface getBody() Retrieves the response body as a JSON stream.
2228
*/
2329
final class JsonResponse extends Response implements JsonResponseInterface
2430
{
31+
/**
32+
* Constructs a new JsonResponse instance with an optional payload and charset.
33+
*
34+
* This constructor SHALL initialize the response body with a JsonStream containing the provided payload.
35+
* The 'Content-Type' header MUST be set to 'application/json' with the specified charset.
36+
*
37+
* @param mixed $payload The JSON-serializable payload to send in the response body. Defaults to an empty array.
38+
* @param string $charset The character encoding to use in the 'Content-Type' header. Defaults to 'utf-8'.
39+
*/
2540
public function __construct(
2641
mixed $payload = [],
2742
string $charset = 'utf-8'
@@ -32,11 +47,29 @@ public function __construct(
3247
);
3348
}
3449

50+
/**
51+
* Retrieves the payload contained in the response body.
52+
*
53+
* This method MUST return the same payload provided during construction or via withPayload().
54+
*
55+
* @return mixed the decoded JSON payload
56+
*/
3557
public function getPayload(): mixed
3658
{
3759
return $this->getBody()->getPayload();
3860
}
3961

62+
/**
63+
* Returns an instance with the specified payload.
64+
*
65+
* This method SHALL return a new instance of the response with the body replaced by a new JsonStream
66+
* containing the provided payload. The original instance MUST remain unchanged, ensuring immutability
67+
* as required by PSR-7.
68+
*
69+
* @param mixed $payload the new JSON-serializable payload
70+
*
71+
* @return self a new JsonResponse instance with the updated payload
72+
*/
4073
public function withPayload(mixed $payload): self
4174
{
4275
return $this->withBody($this->getBody()->withPayload($payload));

src/JsonResponseInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@
1717

1818
use Psr\Http\Message\ResponseInterface;
1919

20+
/**
21+
* Interface JsonResponseInterface.
22+
*
23+
* Defines the contract for JSON-specific HTTP response implementations.
24+
* Implementations of this interface MUST provide access to a JSON payload and comply with PSR-7 ResponseInterface.
25+
*
26+
* This interface SHALL be used to identify responses intended to transmit JSON-encoded payloads with proper headers.
27+
*/
2028
interface JsonResponseInterface extends ResponseInterface, PayloadAwareInterface {}

src/JsonStream.php

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,79 @@
2020
/**
2121
* Class JsonStream.
2222
*
23-
* Extends Nyholm's PSR-7 Stream implementation to provide JSON-specific stream functionality.
24-
* This class SHALL encapsulate a JSON-encoded payload within a PHP stream, while retaining the original
25-
* payload in a decoded form for convenient access.
23+
* Provides a JSON-specific stream implementation, extending Nyholm's PSR-7 Stream.
24+
* This class SHALL encapsulate a JSON-encoded payload within an in-memory PHP stream,
25+
* while retaining the original decoded payload for convenient retrieval.
2626
*
27-
* Implementations MUST properly handle JSON encoding errors and enforce the prohibition of resource types
28-
* within JSON payloads.
27+
* Implementations of this class MUST properly handle JSON encoding errors and SHALL explicitly
28+
* prohibit the inclusion of resource types within the JSON payload.
2929
*
3030
* @package FastForward\Http\Message
3131
*/
3232
final class JsonStream extends Stream implements JsonStreamInterface
3333
{
34+
/**
35+
* JSON encoding flags to be applied by default.
36+
*
37+
* The options JSON_THROW_ON_ERROR, JSON_UNESCAPED_SLASHES, and JSON_UNESCAPED_UNICODE
38+
* SHALL be applied to enforce strict error handling and produce readable JSON output.
39+
*
40+
* @var int
41+
*/
3442
public const ENCODING_OPTIONS = JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
3543

44+
/**
45+
* @var mixed The decoded payload provided to the stream. This MUST be JSON-encodable and MUST NOT contain resources.
46+
*/
47+
private mixed $payload = [];
48+
49+
/**
50+
* @var int The JSON encoding options to be applied. Defaults to self::ENCODING_OPTIONS.
51+
*/
52+
private int $encodingOptions = self::ENCODING_OPTIONS;
53+
3654
/**
3755
* Constructs a new JsonStream instance with the provided payload.
3856
*
39-
* The payload SHALL be JSON-encoded and written to an in-memory PHP stream. The original payload is retained
40-
* in decoded form for later retrieval via {@see getPayload()}.
57+
* The payload SHALL be JSON-encoded and written to an in-memory stream. The original payload is retained
58+
* in its decoded form for later access via getPayload().
4159
*
4260
* @param mixed $payload The data to encode as JSON. MUST be JSON-encodable. Resources are explicitly prohibited.
43-
* @param int $encodingOptions Optional JSON encoding flags as defined by {@see json_encode()}. Defaults to 0.
61+
* @param int $encodingOptions Optional JSON encoding flags. If omitted, ENCODING_OPTIONS will be applied.
4462
*/
45-
public function __construct(
46-
private mixed $payload = [],
47-
private int $encodingOptions = self::ENCODING_OPTIONS
48-
) {
63+
public function __construct(mixed $payload = [], int $encodingOptions = self::ENCODING_OPTIONS)
64+
{
65+
$this->payload = $payload;
66+
$this->encodingOptions = $encodingOptions;
67+
4968
parent::__construct(fopen('php://temp', 'wb+'));
5069

5170
$this->write($this->jsonEncode($this->payload, $this->encodingOptions));
5271
$this->rewind();
5372
}
5473

74+
/**
75+
* Retrieves the decoded payload associated with the stream.
76+
*
77+
* This method SHALL return the original JSON-encodable payload provided during construction or via withPayload().
78+
*
79+
* @return mixed the decoded payload
80+
*/
5581
public function getPayload(): mixed
5682
{
5783
return $this->payload;
5884
}
5985

86+
/**
87+
* Returns a new instance of the stream with the specified payload.
88+
*
89+
* This method MUST return a new JsonStream instance with the body replaced by a stream
90+
* containing the JSON-encoded form of the new payload. The current instance SHALL remain unchanged.
91+
*
92+
* @param mixed $payload the new JSON-encodable payload
93+
*
94+
* @return self a new JsonStream instance containing the updated payload
95+
*/
6096
public function withPayload(mixed $payload): self
6197
{
6298
return new self($payload);
@@ -65,11 +101,11 @@ public function withPayload(mixed $payload): self
65101
/**
66102
* Encodes the given data as JSON, enforcing proper error handling.
67103
*
68-
* If the provided data is a resource, this method SHALL throw an {@see \InvalidArgumentException} as resources
69-
* cannot be represented in JSON format.
104+
* If the provided data is a resource, this method SHALL throw an \InvalidArgumentException,
105+
* as resource types are not supported by JSON.
70106
*
71107
* @param mixed $data the data to encode as JSON
72-
* @param int $encodingOptions JSON encoding options, combined with JSON_THROW_ON_ERROR
108+
* @param int $encodingOptions JSON encoding options to apply. JSON_THROW_ON_ERROR will always be enforced.
73109
*
74110
* @return string the JSON-encoded string representation of the data
75111
*
@@ -79,10 +115,10 @@ public function withPayload(mixed $payload): self
79115
private function jsonEncode(mixed $data, int $encodingOptions): string
80116
{
81117
if (\is_resource($data)) {
82-
throw new \InvalidArgumentException('Cannot JSON encode resources');
118+
throw new \InvalidArgumentException('Cannot JSON encode resources.');
83119
}
84120

85-
// Clear json_last_error()
121+
// Reset potential previous errors
86122
json_encode(null);
87123

88124
return json_encode($data, $encodingOptions | JSON_THROW_ON_ERROR);

src/PayloadAwareInterface.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,34 @@
1818
/**
1919
* Interface PayloadAwareInterface.
2020
*
21-
* Provide functionality for JSON payload handling.
21+
* Defines functionality for objects that encapsulate and manage a payload.
22+
* Implementations of this interface MUST provide immutable methods for accessing and replacing the payload.
23+
* The payload MAY be of any type supported by the implementation, including arrays, objects, scalars, or null.
2224
*
2325
* @package FastForward\Http\Message
2426
*/
2527
interface PayloadAwareInterface
2628
{
2729
/**
28-
* Retrieves the decoded JSON payload from the stream.
30+
* Retrieves the payload contained within the object.
2931
*
30-
* This method MUST return the decoded JSON payload as a native PHP type. The returned type MAY vary depending on
31-
* the structure of the JSON content (e.g., array, object, int, float, string, bool, or null).
32+
* This method MUST return the payload as originally provided or modified.
33+
* The returned type MAY vary depending on the structure of the payload (e.g., array, object, scalar, or null).
3234
*
33-
* @return mixed the decoded JSON payload, which MAY be of any type, including array, object, scalar, or null
35+
* @return mixed the payload, which MAY be of any type including array, object, scalar, or null
3436
*/
3537
public function getPayload(): mixed;
3638

3739
/**
38-
* Returns a new instance with the provided payload encoded as JSON.
40+
* Returns a new instance with the specified payload.
3941
*
40-
* This method MUST NOT modify the existing instance; instead, it SHALL return a new instance with the updated
41-
* JSON payload written to the underlying stream.
42+
* This method MUST NOT modify the current instance. It SHALL return a new instance with the updated payload.
43+
* The payload MAY be of any type supported by the implementation. Implementations MAY throw exceptions if
44+
* constraints on the payload type are violated.
4245
*
43-
* The provided data MUST be JSON-encodable. If encoding fails, the method MAY throw an exception as defined
44-
* by the implementation.
46+
* @param mixed $payload the new payload to set in the instance
4547
*
46-
* @param mixed $payload The data to encode as JSON and set as the stream's payload. This MAY be of any type
47-
* supported by json_encode.
48-
*
49-
* @return self a new instance with the updated JSON payload
48+
* @return self a new instance with the updated payload
5049
*/
5150
public function withPayload(mixed $payload): self;
5251
}

0 commit comments

Comments
 (0)