Completed
Push — master ( fd6900...98364f )
by Bjørn
03:07
created

HttpResponse::hasHeaderValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
namespace HtaccessCapabilityTester;
4
5
/**
6
 * Class for holding properties of a HttpResponse
7
 *
8
 * @package    HtaccessCapabilityTester
9
 * @author     Bjørn Rosell <[email protected]>
10
 * @since      Class available since 0.7
11
 */
12
class HttpResponse
13
{
14
15
    /* @var string  the body of the response */
16
    public $body;
17
18
    /* @var string  the status code of the response */
19
    public $statusCode;
20
21
    /* @var array  the response headers keyed by lowercased field name */
22
    public $headersMapLowerCase;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param  string  $body
28
     * @param  string  $statusCode
29
     * @param  array   $headersMap     Map of headers, keyed by field name.
30
     *                                 There is only one value (string) for each key.
31
     *                                 If there are multiple values, they must be separated by comma
32
     *
33
     * @return void
34
     */
35 84
    public function __construct($body, $statusCode, $headersMap)
36
    {
37 84
        $this->body = $body;
38 84
        $this->statusCode = $statusCode;
39 84
        $this->headersMapLowerCase = array_change_key_case($headersMap, CASE_LOWER);
40 84
    }
41
42
    /**
43
     *  Check if the response has a header
44
     *
45
     *  @param  string $fieldName
46
     *  @return bool
47
     */
48 24
    public function hasHeader($fieldName)
49
    {
50 24
        $fieldName = strtolower($fieldName);
51 24
        return (isset($this->headersMapLowerCase[$fieldName]));
52
    }
53
54
    /**
55
     *  Check if the response has a header with a given value
56
     *
57
     *  @param  string $fieldName
58
     *  @param  string $fieldValue
59
     *  @return bool
60
     */
61 24
    public function hasHeaderValue($fieldName, $fieldValue)
62
    {
63 24
        $fieldName = strtolower($fieldName);
64 24
        if (!isset($this->headersMapLowerCase[$fieldName])) {
65 17
            return false;
66
        }
67 10
        $values = explode(',', $this->headersMapLowerCase[$fieldName]);
68 10
        foreach ($values as $value) {
69 10
            if (trim($value) == $fieldValue) {
70 10
                return true;
71
            }
72
        }
73 2
        return false;
74
    }
75
76
}
77