Completed
Push — master ( b974b4...6333bd )
by Derek
02:08
created

AbstractUriPart::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 3
crap 3
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 101
    public function __construct($part_data, $part_name, $part_type = "string")
48
    {
49 101
        $this->compileValidPattern();
50 101
        $this->compileUnencodedCharacters();
51
52 101
        if (gettype($part_data) != $part_type) {
53 12
            throw new \InvalidArgumentException("{$part_name} must be a {$part_type}");
54 89
        } elseif (!self::isValid($part_data)) {
55 14
            $part_data = $this->encode($part_data);
56 14
        }
57
58 89
        $this->data = $part_data;
59 89
    }
60
61
    /**
62
     * Returns a string representation of the URI component
63
     *
64
     * @return string   A string representation of the URI component
65
     */
66 33
    public function __toString()
67
    {
68 33
        return $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 125
    public static function isValid($string)
84
    {
85 125
        return (bool) preg_match(self::$valid_pattern, $string);
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    abstract public function toUriString();
92
93
    /**
94
     * Percent-encodes invalid characters within a given string. Percent-encoding ignores valid as defined by the
95
     * component per RFC3986.
96
     *
97
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
98
     *
99
     * @param string $string     The query string to be percent-encoded
100
     *
101
     * @return string            The query string with invalid characters percent-encoded
102
     */
103 14
    protected function encode($string)
104
    {
105 14
        $encoded_query = $string;
106
107 14
        if (!empty($this->unencoded_characters)) {
108 14
            $string_helper = new StringHelper($string);
109
110 14
            $encoded_query = $string_helper->affectChunks("rawurlencode", ...$this->unencoded_characters);
111 14
        }
112
113 14
        return $encoded_query;
114
    }
115
116
    /**
117
     * @return void
118
     */
119
    abstract protected function compileValidPattern();
120
121
    /**
122
     * @return void
123
     */
124
    abstract protected function compileUnencodedCharacters();
125
}
126