Completed
Push — master ( aa8b89...6eacd0 )
by John
01:54
created

URLEncodedQueryStringRequestData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace LunixRESTBasics\APIRequest\RequestData;
3
4
class URLEncodedQueryStringRequestData implements RequestData
5
{
6
7
    protected $queryString;
8
9
    protected $parsedData = [];
10
11 7
    public function __construct($queryString)
12
    {
13 7
        $this->queryString = $queryString;
14 7
        parse_str($queryString, $this->parsedData);
15 7
    }
16
17
    /**
18
     * Returns the raw data that the requestData tried to parse
19
     * @return string
20
     */
21 1
    public function getRawData(): string
22
    {
23 1
        return $this->queryString;
24
    }
25
26
    /**
27
     * Attempts to get a specific key from the parsed data. Returns NULL if non-existant key (use has($key) if
28
     * there is a difference between a null value and a missing value)
29
     * WARNING: Parsed data is not sanitized, and should be treated as regular user data
30
     * @param string $key
31
     * @return mixed
32
     */
33 2
    public function get(string $key)
34
    {
35 2
        return $this->parsedData[$key] ?? null;
36
    }
37
38
    /**
39
     * Attempts to find a specific key in the parsed data. Returns true if found else false
40
     * @param string $key
41
     * @return mixed
42
     */
43 3
    public function has(string $key): bool
44
    {
45 3
        return isset($this->parsedData[$key]);
46
    }
47
48
    /**
49
     * Gets all parsed data as an associative array
50
     * WARNING: Parsed data is not sanitized, and should be treated as regular user data
51
     * @return array
52
     */
53 1
    public function getAllAsAssociativeArray(): array
54
    {
55 1
        return $this->parsedData;
56
    }
57
}
58