Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
5 / 5 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| StreamFactory | |
100.00% |
5 / 5 |
|
100.00% |
5 / 5 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createStream | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createStreamFromFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createStreamFromResource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createStreamFromPayload | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of php-fast-forward/http-factory. |
| 7 | * |
| 8 | * This source file is subject to the license bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @copyright Copyright (c) 2025-2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | * @license https://opensource.org/licenses/MIT MIT License |
| 13 | * |
| 14 | * @see https://github.com/php-fast-forward/http-factory |
| 15 | * @see https://github.com/php-fast-forward |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\Http\Message\Factory; |
| 20 | |
| 21 | use FastForward\Http\Message\JsonStream; |
| 22 | use FastForward\Http\Message\PayloadStreamInterface; |
| 23 | use Psr\Http\Message\StreamFactoryInterface as PsrStreamFactoryInterface; |
| 24 | use Psr\Http\Message\StreamInterface; |
| 25 | |
| 26 | /** |
| 27 | * Class StreamFactory. |
| 28 | * |
| 29 | * Decorates a PSR-17 StreamFactoryInterface to provide additional functionality for creating payload streams. |
| 30 | * Implements both standard PSR-17 stream creation methods and convenient payload-aware stream generation. |
| 31 | */ |
| 32 | class StreamFactory implements StreamFactoryInterface |
| 33 | { |
| 34 | /** |
| 35 | * Constructs the StreamFactory instance. |
| 36 | * |
| 37 | * @param PsrStreamFactoryInterface $streamFactory the underlying PSR-17 stream factory implementation |
| 38 | */ |
| 39 | public function __construct( |
| 40 | private PsrStreamFactoryInterface $streamFactory, |
| 41 | ) {} |
| 42 | |
| 43 | /** |
| 44 | * Creates a new stream containing the provided string content. |
| 45 | * |
| 46 | * @param string $content the string content for the stream |
| 47 | * |
| 48 | * @return StreamInterface the generated stream |
| 49 | */ |
| 50 | public function createStream(string $content = ''): StreamInterface |
| 51 | { |
| 52 | return $this->streamFactory->createStream($content); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Creates a new stream from the specified file. |
| 57 | * |
| 58 | * The file is opened with the given mode, and a stream is returned. |
| 59 | * |
| 60 | * @param string $filename the path to the file to open as a stream |
| 61 | * @param string $mode The file open mode. Defaults to 'r' (read mode). |
| 62 | * |
| 63 | * @return StreamInterface the generated stream |
| 64 | */ |
| 65 | public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
| 66 | { |
| 67 | return $this->streamFactory->createStreamFromFile($filename, $mode); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Creates a new stream from the provided PHP resource. |
| 72 | * |
| 73 | * @param resource $resource the PHP resource to wrap as a stream |
| 74 | * |
| 75 | * @return StreamInterface the generated stream |
| 76 | */ |
| 77 | public function createStreamFromResource($resource): StreamInterface |
| 78 | { |
| 79 | return $this->streamFactory->createStreamFromResource($resource); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Creates a new stream containing the JSON-encoded representation of the provided payload. |
| 84 | * |
| 85 | * The returned stream implements PayloadStreamInterface and contains the encoded payload. |
| 86 | * |
| 87 | * @param array $payload the payload to encode as JSON |
| 88 | * |
| 89 | * @return PayloadStreamInterface the generated payload stream |
| 90 | */ |
| 91 | public function createStreamFromPayload(array $payload): PayloadStreamInterface |
| 92 | { |
| 93 | return new JsonStream($payload); |
| 94 | } |
| 95 | } |