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

URLEncodedQueryStringRequestData   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 54
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0
ccs 12
cts 12
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRawData() 0 4 1
A get() 0 4 1
A has() 0 4 1
A getAllAsAssociativeArray() 0 4 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