Completed
Push — master ( 102700...98b500 )
by Derek
02:15
created

AbstractUriPart::compileValidPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace Subreality\Dilmun\Anshar\Http\UriParts;
3
4
use Subreality\Dilmun\Anshar\Utils\StringHelper;
5
6
/**
7
 * Class AbstractUriPart
8
 * @package Subreality\Dilmun\Anshar\Http\UriParts
9
 */
10
abstract class AbstractUriPart implements UriPartInterface
11
{
12
    protected $unreserved_pattern  = '\w\-\.~';
13
    protected $pct_encoded_pattern = '%[A-Fa-f0-9]{2}';
14
    protected $sub_delims_pattern  = '\!\$&\'\(\)\*\+,;\=';
15
    protected $pchar_pattern       = '\:@';
16
17
    protected $unreserved_characters = array("_", "-", ".", "~");
18
    protected $pchar_characters      = array(":", "@");
19
20
    protected $sub_delims_characters = array(
21
        "!",
22
        "$",
23
        "&",
24
        "'",
25
        "(",
26
        ")",
27
        "*",
28
        "+",
29
        ",",
30
        ";",
31
        "=",
32
    );
33
34
    protected $data = "";
35
36
    protected $unencoded_characters = array();
37
38
    protected static $valid_pattern;
39
40
    /**
41
     * Abstract constructor for URI parts.
42
     *
43
     * @param string $part_data The data used to construct a URI part
44
     * @param string $part_name The name of the URI part for exception reporting
45
     * @param string $part_type [optional] The expected type of the URI part
46
     */
47 112
    public function __construct($part_data, $part_name, $part_type = "string")
48
    {
49 112
        $this->compileValidPattern();
50 112
        $this->compileUnencodedCharacters();
51
52 112
        if (gettype($part_data) != $part_type) {
53 18
            throw new \InvalidArgumentException("{$part_name} must be a {$part_type}");
54 94
        } elseif (!self::isValid($part_data)) {
55 19
            $part_data = $this->encode($part_data);
56 19
        }
57
58 94
        $this->data = $part_data;
59 94
    }
60
61
    /**
62
     * Returns a string representation of the URI component
63
     *
64
     * @return string   A string representation of the URI component
65
     */
66 39
    public function __toString()
67
    {
68 39
        return (string) $this->data;
69
    }
70
71
    /**
72
     * Determines whether a given string adheres to the RFC3986 specification for the extending URI part.
73
     *
74
     * Requires extending classes to define self::$valid_pattern.
75
     *
76
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
77
     *
78
     * @param string $string    The query to check for validity
79
     *
80
     * @return bool             Returns true if the provided query matches the RFC3986 specification
81
     *                          Returns false otherwise
82
     */
83 129
    public static function isValid($string)
84
    {
85 129
        return (bool) preg_match(self::$valid_pattern, $string);
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function toUriString()
92
    {
93
        return (string) $this;
94
    }
95
96
    /**
97
     * Percent-encodes invalid characters within a given string. Percent-encoding ignores valid as defined by the
98
     * component per RFC3986.
99
     *
100
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
101
     *
102
     * @param string $string     The query string to be percent-encoded
103
     *
104
     * @return string            The query string with invalid characters percent-encoded
105
     */
106 19
    protected function encode($string)
107
    {
108 19
        $encoded_query = $string;
109
110 19
        if (!empty($this->unencoded_characters)) {
111 19
            $string_helper = new StringHelper($string);
112
113 19
            $encoded_query = $string_helper->affectChunks("rawurlencode", ...$this->unencoded_characters);
114 19
        }
115
116 19
        return $encoded_query;
117
    }
118
119
    /**
120
     * @return void
121
     */
122
    protected function compileValidPattern()
123
    {
124
        self::$valid_pattern = "/.*/";
125
    }
126
127
    /**
128
     * @return void
129
     */
130
    protected function compileUnencodedCharacters()
131
    {
132
        $this->unencoded_characters = array_merge($this->unencoded_characters);
133
    }
134
}
135