1 | <?php |
||
10 | abstract class AbstractUriPart implements UriPartInterface |
||
11 | { |
||
12 | protected static $unreserved_pattern = '\w\-\.~'; |
||
13 | protected static $pct_encoded_pattern = '%[A-Fa-f0-9]{2}'; |
||
14 | protected static $sub_delims_pattern = '\!\$&\'\(\)\*\+,;\='; |
||
15 | protected static $pchar_pattern = '\:@'; |
||
16 | |||
17 | protected static $unreserved_characters = array("_", "-", ".", "~"); |
||
18 | protected static $pchar_characters = array(":", "@"); |
||
19 | |||
20 | protected static $sub_delims_characters = array( |
||
21 | "!", |
||
22 | "$", |
||
23 | "&", |
||
24 | "'", |
||
25 | "(", |
||
26 | ")", |
||
27 | "*", |
||
28 | "+", |
||
29 | ",", |
||
30 | ";", |
||
31 | "=", |
||
32 | ); |
||
33 | |||
34 | protected $data = ""; |
||
35 | |||
36 | protected static $part_pattern; |
||
37 | |||
38 | /** |
||
39 | * Provides a common composition of allowed characters for validation and encoding. |
||
40 | * |
||
41 | * @see AbstractUriPart::compileUnencodedCharacters() |
||
42 | * @see AbstractUriPart::compileValidPattern() |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected static $compositions = array( |
||
47 | "unreserved_characters", |
||
48 | "pchar_characters", |
||
49 | "sub_delims_characters", |
||
50 | ); |
||
51 | |||
52 | protected static $unencoded_characters = array(); |
||
53 | |||
54 | protected static $valid_pattern; |
||
55 | |||
56 | /** |
||
57 | * Constructor for URI parts. |
||
58 | * |
||
59 | * Note that, if overriding the abstract class's constructor, care should be taken to compile unencoded characters |
||
60 | * prior to compiling the part's valid pattern, as the default behavior of the abstract class is to base the part's |
||
61 | * validation pattern on compiled unencoded characters. |
||
62 | * |
||
63 | * @see AbstractUriPart::compileUnencodedCharacters() |
||
64 | * @see AbstractUriPart::compileValidPattern() |
||
65 | * |
||
66 | * @param string $part_data The data used to construct a URI part |
||
67 | * @param string $part_name The name of the URI part for exception reporting |
||
68 | * @param string $part_type [optional] The expected type of the URI part |
||
69 | */ |
||
70 | 224 | public function __construct($part_data, $part_name, $part_type = "string") |
|
71 | { |
||
72 | 224 | if (gettype($part_data) != $part_type) { |
|
73 | 42 | throw new \InvalidArgumentException("{$part_name} must be a {$part_type}"); |
|
74 | 182 | } elseif (!static::isValid($part_data)) { |
|
75 | 33 | $part_data = $this->encode($part_data); |
|
76 | 33 | } |
|
77 | |||
78 | 182 | $this->data = $part_data; |
|
79 | 182 | } |
|
80 | |||
81 | /** |
||
82 | * Returns a string representation of the URI component |
||
83 | * |
||
84 | * @return string A string representation of the URI component |
||
85 | */ |
||
86 | 231 | public function __toString() |
|
90 | |||
91 | /** |
||
92 | * Returns a string representing a valid pattern for the URI component |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 22 | public static function getValidPattern() |
|
102 | |||
103 | /** |
||
104 | * Determines whether a given string adheres to the RFC3986 specification for the extending URI part. |
||
105 | * |
||
106 | * Requires extending classes to define self::$valid_pattern. |
||
107 | * |
||
108 | * @see https://tools.ietf.org/html/rfc3986#appendix-A |
||
109 | * |
||
110 | * @param mixed $data The data to check for validity |
||
111 | * |
||
112 | * @return bool Returns true if the provided query matches the RFC3986 specification |
||
113 | * Returns false otherwise |
||
114 | */ |
||
115 | 276 | public static function isValid($data) |
|
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | 98 | public function toUriString() |
|
131 | |||
132 | /** |
||
133 | * Percent-encodes invalid characters within a given string. Percent-encoding ignores valid as defined by the |
||
134 | * component per RFC3986. |
||
135 | * |
||
136 | * @see https://tools.ietf.org/html/rfc3986#appendix-A |
||
137 | * |
||
138 | * @param string $string The query string to be percent-encoded |
||
139 | * |
||
140 | * @return string The query string with invalid characters percent-encoded |
||
141 | */ |
||
142 | 33 | protected function encode($string) |
|
154 | |||
155 | /** |
||
156 | * Creates a validation pattern based upon a URI part's unencoded characters, allowing for empty strings (^$), |
||
157 | * word characters (\w), and percent encoding by default. |
||
158 | * |
||
159 | * @see AbstractUriPart::compileUnencodedCharacters() |
||
160 | * |
||
161 | * @return void |
||
162 | */ |
||
163 | 240 | protected static function compileValidPattern() |
|
184 | |||
185 | /** |
||
186 | * Compiles unencoded characters provided a URI part's composition, as defined by its compositions property. |
||
187 | * Note that unencoded characters form the basis for the URI part's validation pattern. |
||
188 | * |
||
189 | * @see AbstractUriPart::compileValidPattern() |
||
190 | * |
||
191 | * @return void |
||
192 | */ |
||
193 | 263 | protected static function compileUnencodedCharacters() |
|
205 | } |
||
206 |