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