Passed
Push — master ( 478a0c...9f2f7c )
by Zlatin
01:26
created

MessageTrait::withBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace DevOp\Core\Http\Traits;
3
4
use Psr\Http\Message\StreamInterface;
5
6
trait MessageTrait
7
{
8
9
    /**
10
     * @var StreamInterface
11
     */
12
    protected $body;
13
14
    /**
15
     * @var array
16
     */
17
    protected $headers = [];
18
19
    /**
20
     * @var array
21
     */
22
    protected $headersName = [];
23
24
    /**
25
     * @var string
26
     */
27
    protected $protocolVersion = 1.1;
28
29
    /**
30
     * @var StreamInterface
31
     */
32
    protected $stream;
33
34
    /**
35
     * @return StreamInterface
36
     */
37
    public function getBody()
38
    {
39
        return $this->body;
40
    }
41
42
    /**
43
     * @param StreamInterface $body
44
     * @return self
45
     */
46
    public function withBody(StreamInterface $body)
47
    {
48
        if ($body === $this->stream) {
49
            return $this;
50
        }
51
52
        $clone = clone $this;
53
        $clone->stream = $body;
54
55
        return $clone;
56
    }
57
58
    /**
59
     * @param string $name
60
     * @return boolean
61
     */
62
    public function hasHeader($name)
63
    {
64
        return isset($this->headersName[strtolower($name)]);
65
    }
66
67
    /**
68
     * @param string $name
69
     * @return mixed
70
     */
71
    public function getHeader($name)
72
    {
73
        if ($this->hasHeader($name)) {
74
            return [$this->headers[$this->headersName[strtolower($name)]]];
75
        }
76
        return [];
77
    }
78
79
    public function getHeaderLine($name)
80
    {
81
        return implode(', ', $this->getHeader($name));
82
    }
83
84
    /**
85
     * @return array)
86
     */
87
    public function getHeaders()
88
    {
89
        return $this->headers;
90
    }
91
92
    /**
93
     * @param string $name
94
     * @param mixed $value
95
     * @return self
96
     */
97
    public function withHeader($name, $value)
98
    {
99
        $clone = new $this;
100
101
        $normalize = strtolower($name);
102
103
        if ($clone->hasHeader($name)) {
104
            unset($clone->headers[$this->headersName[$normalize]]);
105
            unset($clone->headersName[$normalize]);
106
        }
107
108
        $clone->headersName[$normalize] = $name;
109
        $clone->headers[$normalize] = !is_array($value) ? [$value] : $value;
110
111
        return $clone;
112
    }
113
114
    /**
115
     * @param string $name
116
     * @param mixed $value
117
     * @return self
118
     */
119
    public function withAddedHeader($name, $value)
120
    {
121
        $clone = new $this;
122
123
        $normalize = strtolower($name);
124
125
        if ($clone->hasHeader($name)) {
126
            $clone->headers[$normalize] = array_merge($this->headers, (!is_array($value) ? [$value] : $value));
127
        } else {
128
            $clone->headersName[$normalize] = $name;
129
            $clone->headers[$normalize] = !is_array($value) ? [$value] : $value;
130
        }
131
132
        return $clone;
133
    }
134
135
    /**
136
     * @param string $name
137
     * @return self
138
     */
139
    public function withoutHeader($name)
140
    {
141
        $clone = new $this;
142
143
        $normalize = strtolower($name);
144
145
        if ($clone->hasHeader($name)) {
146
            unset($clone->headers[$this->headersName[$normalize]]);
147
            unset($clone->headersName[$normalize]);
148
        }
149
150
        return $clone;
151
    }
152
153
    /**
154
     * @return string
155
     */
156 4
    public function getProtocolVersion()
157
    {
158 4
        return $this->protocolVersion;
159
    }
160
161
    /**
162
     * @param string $version
163
     * @return self
164
     */
165 2
    public function withProtocolVersion($version)
166
    {
167 2
        if ($version === $this->protocolVersion) {
168
            return $this;
169
        }
170
171 2
        $clone = clone $this;
172 2
        $clone->protocolVersion = $version;
173
174 2
        return $clone;
175
    }
176
}
177