Completed
Push — master ( 40ab0c...a57043 )
by Changwan
04:15
created

HelperTrait::getPsrHeadersFromServerParams()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 5
nop 1
dl 0
loc 21
ccs 17
cts 17
cp 1
crap 7
rs 7.551
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Http\Factory;
3
4
use Wandu\Http\Psr\Stream;
5
use Wandu\Http\Psr\Uri;
6
7
trait HelperTrait
8
{
9
    /**
10
     * @param array $plainHeaders
11
     * @return array
12
     */
13 2
    protected function getPsrHeadersFromPlainHeader(array $plainHeaders)
14
    {
15 2
        $headers = [];
16 2
        foreach ($plainHeaders as $plainHeader) {
17 2
            list($key, $value) = array_map('trim', explode(':', $plainHeader, 2));
18 2
            $headers[strtolower($key)] = [$value];
19 2
        }
20 2
        return $headers;
21
    }
22
23
    /**
24
     * @param array $serverParams
25
     * @return array
26
     */
27 16
    protected function getPsrHeadersFromServerParams(array $serverParams)
28
    {
29 16
        $headers = array();
30 16
        foreach ($serverParams as $key => $value) {
31 16
            if ($value && strpos($key, 'HTTP_') === 0) {
32 15
                $name = strtr(substr($key, 5), '_', ' ');
33 15
                $name = strtr(ucwords(strtolower($name)), ' ', '-');
34 15
                $name = strtolower($name);
35 15
                $headers[$name] = $value;
36 15
                continue;
37
            }
38 13
            if ($value && strpos($key, 'CONTENT_') === 0) {
39 1
                $name = substr($key, 8); // Content-
40 1
                $name = 'Content-' . (($name == 'MD5') ? $name : ucfirst(strtolower($name)));
41 1
                $name = strtolower($name);
42 1
                $headers[$name] = $value;
43 1
                continue;
44
            }
45 16
        }
46 16
        return $headers;
47
    }
48
49
    /**
50
     * @param string $uri
51
     * @param array $headers
52
     * @return \Psr\Http\Message\UriInterface
53
     */
54 2
    protected function makeUriObjectWithPsrHeaders($uri = '/', array $headers = [])
55
    {
56 2
        $plainUri = isset($headers['host'][0]) ? 'http://' . $headers['host'][0] : '';
57 2
        if ($uri !== '/' || ($uri === '/' && $plainUri === '')) {
58 2
            $plainUri .= $uri;
59 2
        }
60 2
        return new Uri($plainUri);
61
    }
62
}
63