1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* League.Uri (http://uri.thephpleague.com) |
4
|
|
|
* |
5
|
|
|
* @package League.uri |
6
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* @copyright 2013-2015 Ignace Nyamagana Butera |
8
|
|
|
* @license https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License) |
9
|
|
|
* @version 4.2.0 |
10
|
|
|
* @link https://github.com/thephpleague/uri/ |
11
|
|
|
*/ |
12
|
|
|
namespace League\Uri\Components; |
13
|
|
|
|
14
|
|
|
use League\Uri\Interfaces\Fragment as FragmentInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Value object representing a URI fragment component. |
18
|
|
|
* |
19
|
|
|
* @package League.uri |
20
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class Fragment extends AbstractComponent implements FragmentInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
|
|
protected static $reservedCharactersRegex = "\!\$&'\(\)\*\+,;\=\:\/@\?"; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Preserve the delimiter |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $preserveDelimiter = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* new instance |
39
|
|
|
* |
40
|
|
|
* @param string|null $data the component value |
41
|
|
|
*/ |
42
|
625 |
|
public function __construct($data = null) |
43
|
|
|
{ |
44
|
625 |
|
if ($data !== null) { |
45
|
382 |
|
$this->preserveDelimiter = true; |
|
|
|
|
46
|
382 |
|
$this->init($data); |
47
|
246 |
|
} |
48
|
613 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
12 |
|
public static function __set_state(array $properties) |
54
|
|
|
{ |
55
|
12 |
|
$component = new static($properties['data']); |
56
|
12 |
|
$component->preserveDelimiter = $properties['preserveDelimiter']; |
57
|
|
|
|
58
|
12 |
|
return $component; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
2 |
|
public function __debugInfo() |
65
|
|
|
{ |
66
|
2 |
|
return ['fragment' => $this->__toString()]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the component literal value. The return type can be |
71
|
|
|
* <ul> |
72
|
|
|
* <li> null: If the component is not defined |
73
|
|
|
* <li> string: Otherwise |
74
|
|
|
* </ul> |
75
|
|
|
* |
76
|
|
|
* @return string|int|null |
77
|
|
|
*/ |
78
|
631 |
|
public function getComponent() |
79
|
|
|
{ |
80
|
631 |
|
if (null === $this->data && false === $this->preserveDelimiter) { |
81
|
348 |
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
370 |
|
return $this->encode($this->data); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the instance string representation |
89
|
|
|
* with its optional URI delimiters |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
629 |
|
public function getUriComponent() |
94
|
|
|
{ |
95
|
629 |
|
$component = $this->__toString(); |
96
|
629 |
|
if ($this->preserveDelimiter) { |
97
|
368 |
|
return FragmentInterface::DELIMITER.$component; |
98
|
|
|
} |
99
|
|
|
|
100
|
348 |
|
return $component; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns an instance with the specified string |
105
|
|
|
* |
106
|
|
|
* This method MUST retain the state of the current instance, and return |
107
|
|
|
* an instance that contains the modified data |
108
|
|
|
* |
109
|
|
|
* @param string $value |
110
|
|
|
* |
111
|
|
|
* @return static |
112
|
|
|
*/ |
113
|
111 |
|
public function modify($value) |
114
|
|
|
{ |
115
|
111 |
|
if (null === $value && $value === $this->getComponent()) { |
116
|
93 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
18 |
|
if ($value === $this->__toString()) { |
120
|
15 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
return new static($value); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.