ServerRequest::withUploadedFiles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Psr7;
6
7
use Psr\Http\Message\{ServerRequestInterface, StreamInterface, UploadedFileInterface, UriInterface};
8
9
/**
10
 * @author Michael Dowling and contributors to guzzlehttp/psr7
11
 * @author Tobias Nyholm <[email protected]>
12
 * @author Martijn van der Ven <[email protected]>
13
 */
14
final class ServerRequest implements ServerRequestInterface
15
{
16
    use MessageTrait;
17
    use RequestTrait;
18
19
    /** @var array */
20
    private $attributes = [];
21
22
    /** @var array */
23
    private $cookieParams = [];
24
25
    /** @var array|object|null */
26
    private $parsedBody;
27
28
    /** @var array */
29
    private $queryParams = [];
30
31
    /** @var array */
32
    private $serverParams;
33
34
    /** @var UploadedFileInterface[] */
35
    private $uploadedFiles = [];
36
37
    /**
38
     * @param string $method HTTP method
39
     * @param string|UriInterface $uri URI
40
     * @param array $headers Request headers
41
     * @param string|resource|StreamInterface|null $body Request body
42
     * @param string $version Protocol version
43
     * @param array $serverParams Typically the $_SERVER superglobal
44
     */
45 45 View Code Duplication
    public function __construct(string $method, $uri, array $headers = [], $body = null, string $version = '1.1', array $serverParams = [])
46
    {
47 45
        $this->serverParams = $serverParams;
48
49 45
        if (!($uri instanceof UriInterface)) {
50 39
            $uri = new Uri($uri);
51
        }
52
53 45
        $this->method = $method;
54 45
        $this->uri = $uri;
55 45
        $this->setHeaders($headers);
56 45
        $this->protocol = $version;
57
58 45
        if (!$this->hasHeader('Host')) {
59 45
            $this->updateHostFromUri();
60
        }
61
62
        // If we got no body, defer initialization of the stream until ServerRequest::getBody()
63 45
        if ('' !== $body && null !== $body) {
64
            $this->stream = Stream::create($body);
65
        }
66 45
    }
67
68 3
    public function getServerParams(): array
69
    {
70 3
        return $this->serverParams;
71
    }
72
73 3
    public function getUploadedFiles(): array
74
    {
75 3
        return $this->uploadedFiles;
76
    }
77
78 2
    public function withUploadedFiles(array $uploadedFiles)
79
    {
80 2
        $new = clone $this;
81 2
        $new->uploadedFiles = $uploadedFiles;
82
83 2
        return $new;
84
    }
85
86 4
    public function getCookieParams(): array
87
    {
88 4
        return $this->cookieParams;
89
    }
90
91 2
    public function withCookieParams(array $cookies)
92
    {
93 2
        $new = clone $this;
94 2
        $new->cookieParams = $cookies;
95
96 2
        return $new;
97
    }
98
99 3
    public function getQueryParams(): array
100
    {
101 3
        return $this->queryParams;
102
    }
103
104 2
    public function withQueryParams(array $query)
105
    {
106 2
        $new = clone $this;
107 2
        $new->queryParams = $query;
108
109 2
        return $new;
110
    }
111
112 5
    public function getParsedBody()
113
    {
114 5
        return $this->parsedBody;
115
    }
116
117 8
    public function withParsedBody($data)
118
    {
119 8
        if (!\is_array($data) && !\is_object($data) && null !== $data) {
120 4
            throw new \InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null');
121
        }
122
123 4
        $new = clone $this;
124 4
        $new->parsedBody = $data;
125
126 4
        return $new;
127
    }
128
129 3
    public function getAttributes(): array
130
    {
131 3
        return $this->attributes;
132
    }
133
134 4 View Code Duplication
    public function getAttribute($attribute, $default = null)
135
    {
136 4
        if (false === \array_key_exists($attribute, $this->attributes)) {
137 4
            return $default;
138
        }
139
140 4
        return $this->attributes[$attribute];
141
    }
142
143 5
    public function withAttribute($attribute, $value): self
144
    {
145 5
        $new = clone $this;
146 5
        $new->attributes[$attribute] = $value;
147
148 5
        return $new;
149
    }
150
151 3 View Code Duplication
    public function withoutAttribute($attribute): self
152
    {
153 3
        if (false === \array_key_exists($attribute, $this->attributes)) {
154 1
            return $this;
155
        }
156
157 3
        $new = clone $this;
158 3
        unset($new->attributes[$attribute]);
159
160 3
        return $new;
161
    }
162
}
163