Completed
Push — master ( a68096...8596bf )
by Derek
02:01
created

Query::toUriString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
namespace Subreality\Dilmun\Anshar\Http\UriParts;
3
4
/**
5
 * Class Query
6
 * @package Subreality\Dilmun\Anshar\Http\UriParts
7
 */
8
class Query
9
{
10
    private $unreserved_pattern  = '\w\-\.~';
11
    private $pct_encoded_pattern = '%[A-Fa-f0-9]{2}';
12
    private $sub_delims_pattern  = '\!\$&\'\(\)\*\+,;\=';
13
    private $pchar_pattern       = '\:@';
14
15
    private static $valid_pattern;
16
17
    private $query = "";
18
19
    /**
20
     * Query constructor. Accepts a string representing a URI query. Construction will throw an exception if the
21
     * query is either not a string or does not conform to the RFC3986 URI query specification.
22
     *
23
     * query       = *( pchar / "/" / "?" )
24
     * pchar       = unreserved / pct-encoded / sub-delims / ":" / "@"
25
     * unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
26
     * pct-encoded = "%" HEXDIG HEXDIG
27
     * sub-delims  = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
28
     *
29
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
30
     *
31
     * @throws \InvalidArgumentException
32
     *
33
     * @param string $query     A string representing a URI query
34
     */
35 12
    public function __construct($query)
36
    {
37 12
        $this->compileValidPattern();
38
39 12
        if (!is_string($query)) {
40 6
            throw new \InvalidArgumentException("Query must be a string");
41 6
        } elseif (!self::isValid($query)) {
42 3
            throw new \InvalidArgumentException("Query must conform to RFC3986 specification");
43
        }
44
45 3
        $this->query = $query;
46 3
    }
47
48
    /**
49
     * Returns a string representation of the query
50
     *
51
     * @return string   A string representation of the query
52
     */
53 3
    public function __toString()
54
    {
55 3
        return $this->query;
56
    }
57
58
    /**
59
     * Determines whether a given query adheres to the RFC3986 specification:
60
     *
61
     * query       = *( pchar / "/" / "?" )
62
     * pchar       = unreserved / pct-encoded / sub-delims / ":" / "@"
63
     * unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
64
     * pct-encoded = "%" HEXDIG HEXDIG
65
     * sub-delims  = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
66
     *
67
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
68
     * @see Query::$valid_pattern
69
     *
70
     * @param string $query     The query to check for validity
71
     * @return bool             Returns true if the provided query matches the RFC3986 specification
72
     *                          Returns false otherwise
73
     */
74 16
    public static function isValid($query)
75
    {
76 16
        return (bool) preg_match(self::$valid_pattern, $query);
77
    }
78
79
    /**
80
     * Returns a string representation of the query formatted so that it can be compiled into a complete URI string
81
     * per the Uri object's string specification.
82
     *
83
     * If the query is empty, toUriString returns an empty string; if the query is not empty, toUriString returns the
84
     * query prefixed with a question mark.
85
     *
86
     * @see Uri::__toString
87
     *
88
     * @return string   A string representation of the query formatted for a complete URI string
89
     */
90 2
    public function toUriString()
91
    {
92 2
        $uri_query = $this->query;
93
94 2
        if (!empty($uri_query)) {
95 1
            $uri_query = "?" . $uri_query;
96 1
        }
97
98 2
        return $uri_query;
99
    }
100
101
    /**
102
     * Compiles a regexp pattern based on predefined patterns that define allowed characters for a query. Note that the
103
     * pipe indicates that a query can either contain all defined characters or contain percent encoded characters.
104
     */
105 12
    private function compileValidPattern()
106
    {
107 12
        self::$valid_pattern = '/^([\/\?' .
108 12
            $this->unreserved_pattern .
109 12
            $this->sub_delims_pattern .
110 12
            $this->pchar_pattern .
111 12
            ']|' .
112 12
            $this->pct_encoded_pattern .
113 12
            ')*$/';
114 12
    }
115
}
116