Passed
Push — main ( 248da0...de635b )
by smiley
01:50
created

create_server_request_from_globals()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 2
nop 2
dl 0
loc 23
rs 9.5222
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A create_stream() 0 17 4
1
<?php
2
/**
3
 * @created      28.08.2018
4
 * @author       smiley <[email protected]>
5
 * @copyright    2018 smiley
6
 * @license      MIT
7
 */
8
9
namespace chillerlan\HTTP\Psr17;
10
11
use Psr\Http\Message\StreamInterface;
12
use chillerlan\HTTP\Psr7\Stream;
13
use InvalidArgumentException;
14
15
use function is_scalar, method_exists;
16
17
const CHILLERLAN_PSR17_INCLUDES = true;
18
19
const STREAM_MODES_READ_WRITE = [
20
	'a+'  => true,
21
	'c+'  => true,
22
	'c+b' => true,
23
	'c+t' => true,
24
	'r+'  => true,
25
	'r+b' => true,
26
	'r+t' => true,
27
	'w+'  => true,
28
	'w+b' => true,
29
	'w+t' => true,
30
	'x+'  => true,
31
	'x+b' => true,
32
	'x+t' => true,
33
];
34
35
const STREAM_MODES_READ = STREAM_MODES_READ_WRITE + [
36
	'r'   => true,
37
	'rb'  => true,
38
	'rt'  => true,
39
];
40
41
const STREAM_MODES_WRITE = STREAM_MODES_READ_WRITE + [
42
	'a'   => true,
43
	'rw'  => true,
44
	'w'   => true,
45
	'wb'  => true,
46
];
47
48
/**
49
 * Create a new writable stream from a string.
50
 *
51
 * The stream SHOULD be created with a temporary resource.
52
 *
53
 * @param string $content String content with which to populate the stream.
54
 * @param string $mode    one of \chillerlan\HTTP\Psr17\STREAM_MODES_WRITE
55
 * @param bool   $rewind  rewind the stream
56
 *
57
 * @return \Psr\Http\Message\StreamInterface
58
 */
59
function create_stream(string $content = '', string $mode = 'r+', bool $rewind = true):StreamInterface{
60
61
	if(!isset(STREAM_MODES_WRITE[$mode])){
62
		throw new InvalidArgumentException('invalid mode');
63
	}
64
65
	$stream = new Stream(fopen('php://temp', $mode));
66
67
	if($content !== ''){
68
		$stream->write($content);
69
	}
70
71
	if($rewind){
72
		$stream->rewind();
73
	}
74
75
	return $stream;
76
}
77
78
/**
79
 * @param mixed $in
80
 *
81
 * @return \Psr\Http\Message\StreamInterface
82
 */
83
function create_stream_from_input($in = null):StreamInterface{
84
	$in ??= '';
85
86
	// not sure about this one, it might cause:
87
	// a) trouble if the given string accidentally matches a file path, and
88
	// b) security implications because of the above.
89
	// use with caution and never with user input!
90
#	if(\is_string($in) && \is_file($in) && \is_readable($in)){
91
#		return new Stream(\fopen($in, 'r'));
92
#	}
93
94
	if(is_scalar($in)){
95
		return create_stream((string)$in);
96
	}
97
98
	$type = gettype($in);
99
100
	if($type === 'resource'){
101
		return new Stream($in);
102
	}
103
	elseif($type === 'object'){
104
105
		if($in instanceof StreamInterface){
106
			return $in;
107
		}
108
		elseif(method_exists($in, '__toString')){
109
			return create_stream((string)$in);
110
		}
111
112
	}
113
114
	throw new InvalidArgumentException('Invalid resource type: '.$type);
115
}
116