1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Igni\Network\Http; |
4
|
|
|
|
5
|
|
|
use Igni\Exception\InvalidArgumentException; |
6
|
|
|
use Psr\Http\Message\StreamInterface; |
7
|
|
|
use Zend\Diactoros\Stream as BaseStream; |
8
|
|
|
|
9
|
|
|
use function strpos; |
10
|
|
|
use function is_string; |
11
|
|
|
use function is_resource; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @package Igni\Http |
15
|
|
|
*/ |
16
|
|
|
class Stream extends BaseStream |
17
|
|
|
{ |
18
|
24 |
|
public static function fromString(string $content): self |
19
|
|
|
{ |
20
|
24 |
|
$stream = new self('php://temp', 'wb+'); |
21
|
24 |
|
$stream->write($content); |
22
|
24 |
|
$stream->rewind(); |
23
|
|
|
|
24
|
24 |
|
return $stream; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function fromFile(string $path, string $mode = 'r'): self |
28
|
|
|
{ |
29
|
|
|
if ($path === 'php://input') { |
30
|
|
|
return new self($path, 'r'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return new self($path, $mode); |
34
|
|
|
} |
35
|
|
|
|
36
|
24 |
|
public static function create($stream, string $mode = 'r'): self |
37
|
|
|
{ |
38
|
24 |
|
if (!is_string($stream) && !is_resource($stream) && !$stream instanceof StreamInterface) { |
39
|
|
|
throw new InvalidArgumentException( |
40
|
|
|
'Stream must be a string stream resource identifier, ' |
41
|
|
|
. 'an actual stream resource, ' |
42
|
|
|
. 'or a Psr\Http\Message\StreamInterface implementation' |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
24 |
|
if (is_string($stream) && (empty($stream) || 0 !== strpos('php://', $stream))) { |
47
|
24 |
|
return self::fromString($stream); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $stream instanceof StreamInterface |
51
|
|
|
? $stream |
52
|
|
|
: self::fromFile($stream, $mode); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.