Completed
Push — master ( 74e06a...1990c3 )
by Derek
02:06
created

Query::compileValidPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
crap 1
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 $valid_pattern;
16
17
    /**
18
     * Query constructor. Accepts a string representing a URI query. Construction will throw an exception if the
19
     * query is either not a string or does not conform to the RFC3986 URI query specification.
20
     *
21
     * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
22
     *
23
     * @param $query
24
     */
25 10
    public function __construct($query)
26
    {
27 10
        $this->compileValidPattern();
28
29 10
        if (!is_string($query)) {
30 6
            throw new \InvalidArgumentException("Query must be a string");
31 4
        } elseif (!preg_match($this->valid_pattern, $query)) {
32 3
            throw new \InvalidArgumentException("Query must conform to RFC3986 specification");
33
        }
34 1
    }
35
36
    /**
37
     * Compiles a regexp pattern based on predefined patterns that define allowed characters for a query. Note that the
38
     * pipe indicates that a query can either contain all defined characters or contain percent encoded characters.
39
     */
40 10
    private function compileValidPattern()
41
    {
42 10
        $this->valid_pattern = '/^([\/\?' .
43 10
            $this->unreserved_pattern .
44 10
            $this->sub_delims_pattern .
45 10
            $this->pchar_pattern .
46 10
            ']|' .
47 10
            $this->pct_encoded_pattern .
48 10
            ')*$/';
49 10
    }
50
}
51