11<?php
22
3+ declare (strict_types=1 );
4+
5+ /**
6+ * This file is part of php-fast-forward/http-message.
7+ *
8+ * This source file is subject to the license bundled
9+ * with this source code in the file LICENSE.
10+ *
11+ * @link https://github.com/php-fast-forward/http-message
12+ * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <[email protected] > 13+ * @license https://opensource.org/licenses/MIT MIT License
14+ */
15+
316namespace FastForward \Http \Message ;
417
518use Nyholm \Psr7 \Stream ;
619
720/**
8- * Class JsonStream
21+ * Class JsonStream.
922 *
1023 * Extends Nyholm's PSR-7 Stream implementation to provide JSON-specific stream functionality.
1124 * This class SHALL encapsulate a JSON-encoded payload within a PHP stream, while retaining the original
@@ -26,8 +39,8 @@ final class JsonStream extends Stream implements JsonStreamInterface
2639 * The payload SHALL be JSON-encoded and written to an in-memory PHP stream. The original payload is retained
2740 * in decoded form for later retrieval via {@see getPayload()}.
2841 *
29- * @param mixed $payload The data to encode as JSON. MUST be JSON-encodable. Resources are explicitly prohibited.
30- * @param int $encodingOptions Optional JSON encoding flags as defined by {@see json_encode()}. Defaults to 0.
42+ * @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.
3144 */
3245 public function __construct (
3346 private mixed $ payload = [],
@@ -39,22 +52,33 @@ public function __construct(
3952 $ this ->rewind ();
4053 }
4154
55+ public function getPayload (): mixed
56+ {
57+ return $ this ->payload ;
58+ }
59+
60+ public function withPayload (mixed $ payload ): self
61+ {
62+ return new self ($ payload );
63+ }
64+
4265 /**
4366 * Encodes the given data as JSON, enforcing proper error handling.
4467 *
4568 * If the provided data is a resource, this method SHALL throw an {@see \InvalidArgumentException} as resources
4669 * cannot be represented in JSON format.
4770 *
48- * @param mixed $data The data to encode as JSON.
49- * @param int $encodingOptions JSON encoding options, combined with JSON_THROW_ON_ERROR.
50- * @return string The JSON-encoded string representation of the data.
71+ * @param mixed $data the data to encode as JSON
72+ * @param int $encodingOptions JSON encoding options, combined with JSON_THROW_ON_ERROR
5173 *
52- * @throws \InvalidArgumentException If the data contains a resource.
53- * @throws \JsonException If JSON encoding fails.
74+ * @return string the JSON-encoded string representation of the data
75+ *
76+ * @throws \InvalidArgumentException if the data contains a resource
77+ * @throws \JsonException if JSON encoding fails
5478 */
5579 private function jsonEncode (mixed $ data , int $ encodingOptions ): string
5680 {
57- if (is_resource ($ data )) {
81+ if (\ is_resource ($ data )) {
5882 throw new \InvalidArgumentException ('Cannot JSON encode resources ' );
5983 }
6084
@@ -63,14 +87,4 @@ private function jsonEncode(mixed $data, int $encodingOptions): string
6387
6488 return json_encode ($ data , $ encodingOptions | JSON_THROW_ON_ERROR );
6589 }
66-
67- public function getPayload (): mixed
68- {
69- return $ this ->payload ;
70- }
71-
72- public function withPayload (mixed $ payload ): self
73- {
74- return new self ($ payload );
75- }
7690}
0 commit comments