|
1
|
|
|
<?php |
|
2
|
|
|
namespace Subreality\Dilmun\Anshar\Http\UriParts; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class Fragment |
|
6
|
|
|
* @package Subreality\Dilmun\Anshar\Http\UriParts |
|
7
|
|
|
*/ |
|
8
|
|
View Code Duplication |
class Fragment extends AbstractUriPart |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
protected $unencoded_characters = array("/", "?"); |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Fragment constructor. Accepts a string representing a URI fragment. Construction will throw an exception if the |
|
14
|
|
|
* fragment is not a string. |
|
15
|
|
|
* |
|
16
|
|
|
* Construction accepts strings that have been percent-encoded as well as strings that have not been percent-encoded |
|
17
|
|
|
* and will encode invalid characters. |
|
18
|
|
|
* |
|
19
|
|
|
* Construction with a string that includes both encoded and decoded characters will be assumed to be an encoded |
|
20
|
|
|
* string, resulting in double-encoding. |
|
21
|
|
|
* |
|
22
|
|
|
* fragment = *( pchar / "/" / "?" ) |
|
23
|
|
|
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@" |
|
24
|
|
|
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
|
25
|
|
|
* pct-encoded = "%" HEXDIG HEXDIG |
|
26
|
|
|
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" |
|
27
|
|
|
* |
|
28
|
|
|
* @see https://tools.ietf.org/html/rfc3986#appendix-A |
|
29
|
|
|
* |
|
30
|
|
|
* @throws \InvalidArgumentException |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $fragment A string representing a URI fragment |
|
33
|
|
|
*/ |
|
34
|
14 |
|
public function __construct($fragment) |
|
35
|
|
|
{ |
|
36
|
14 |
|
$this->compileValidPattern(); |
|
37
|
14 |
|
$this->compileUnencodedCharacters(); |
|
38
|
|
|
|
|
39
|
14 |
|
if (!is_string($fragment)) { |
|
40
|
6 |
|
throw new \InvalidArgumentException("Fragment must be a string"); |
|
41
|
8 |
|
} elseif (!self::isValid($fragment)) { |
|
42
|
5 |
|
$fragment = $this->encode($fragment); |
|
43
|
5 |
|
} |
|
44
|
|
|
|
|
45
|
8 |
|
$this->data = $fragment; |
|
46
|
8 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns a string representation of the fragment formatted so that it can be compiled into a complete URI string |
|
50
|
|
|
* per the Uri object's string specification. |
|
51
|
|
|
* |
|
52
|
|
|
* If the fragment is empty, toUriString returns an empty string; if the fragment is not empty, toUriString returns |
|
53
|
|
|
* the fragment prefixed with a octothorpe(#). |
|
54
|
|
|
* |
|
55
|
|
|
* @see Uri::__toString |
|
56
|
|
|
* |
|
57
|
|
|
* @return string A string representation of the fragment formatted for a complete URI string |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function toUriString() |
|
60
|
|
|
{ |
|
61
|
2 |
|
$uri_fragment = $this->data; |
|
62
|
|
|
|
|
63
|
2 |
|
if (!empty($uri_fragment)) { |
|
64
|
1 |
|
$uri_fragment = "#" . $uri_fragment; |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return $uri_fragment; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Compiles a regexp pattern based on predefined patterns that define allowed characters for a fragment. |
|
72
|
|
|
*/ |
|
73
|
14 |
|
protected function compileValidPattern() |
|
74
|
|
|
{ |
|
75
|
14 |
|
self::$valid_pattern = '/^([\/\?' . |
|
76
|
14 |
|
$this->unreserved_pattern . |
|
77
|
14 |
|
$this->sub_delims_pattern . |
|
78
|
14 |
|
$this->pchar_pattern . |
|
79
|
14 |
|
']|' . //predefined patterns or percent-encoding |
|
80
|
14 |
|
$this->pct_encoded_pattern . |
|
81
|
14 |
|
')*$/'; |
|
82
|
14 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Compiles an array of legal characters for a fragment based upon the RFC3986 specification: |
|
86
|
|
|
* |
|
87
|
|
|
* fragment = *( pchar / "/" / "?" ) |
|
88
|
|
|
*/ |
|
89
|
14 |
|
protected function compileUnencodedCharacters() |
|
90
|
|
|
{ |
|
91
|
14 |
|
$this->unencoded_characters = array_merge( |
|
92
|
14 |
|
$this->unencoded_characters, |
|
93
|
14 |
|
$this->unreserved_characters, |
|
94
|
14 |
|
$this->pchar_characters, |
|
95
|
14 |
|
$this->sub_delims_characters |
|
96
|
14 |
|
); |
|
97
|
14 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.