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\Types\ImmutableComponentTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* An abstract class to ease component manipulation |
18
|
|
|
* |
19
|
|
|
* @package League.uri |
20
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
21
|
|
|
* @since 4.0.0 |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractComponent |
24
|
|
|
{ |
25
|
|
|
use ImmutableComponentTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The component data |
29
|
|
|
* |
30
|
|
|
* @var int|string |
31
|
|
|
*/ |
32
|
|
|
protected $data; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
12 |
|
public static function __set_state(array $properties) |
38
|
|
|
{ |
39
|
12 |
|
return new static($properties['data']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* new instance |
44
|
|
|
* |
45
|
|
|
* @param string|null $data the component value |
46
|
|
|
*/ |
47
|
1070 |
|
public function __construct($data = null) |
48
|
|
|
{ |
49
|
1070 |
|
if ($data !== null) { |
50
|
1028 |
|
$this->init($data); |
51
|
613 |
|
} |
52
|
965 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set data. |
56
|
|
|
* |
57
|
|
|
* @param mixed $data The data to set. |
58
|
|
|
*/ |
59
|
1013 |
|
protected function init($data) |
60
|
|
|
{ |
61
|
1013 |
|
$data = $this->validateString($data); |
62
|
965 |
|
$this->data = $this->validate($data); |
63
|
923 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* validate the incoming data |
67
|
|
|
* |
68
|
|
|
* @param string $data |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
520 |
|
protected function validate($data) |
73
|
|
|
{ |
74
|
520 |
|
$data = filter_var($data, FILTER_UNSAFE_RAW, ['flags' => FILTER_FLAG_STRIP_LOW]); |
75
|
520 |
|
$this->assertValidComponent($data); |
76
|
|
|
|
77
|
493 |
|
return rawurldecode(trim($data)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the component literal value. The return type can be |
82
|
|
|
* <ul> |
83
|
|
|
* <li> null: If the component is not defined |
84
|
|
|
* <li> int: If the component is a defined port |
85
|
|
|
* <li> string: Otherwise |
86
|
|
|
* </ul> |
87
|
|
|
* |
88
|
|
|
* @return string|int|null |
89
|
|
|
*/ |
90
|
724 |
|
public function getComponent() |
91
|
|
|
{ |
92
|
724 |
|
if (null === $this->data) { |
93
|
516 |
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
673 |
|
return $this->encode($this->data); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns the instance string representation; If the |
101
|
|
|
* instance is not defined an empty string is returned |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
966 |
|
public function __toString() |
106
|
|
|
{ |
107
|
966 |
|
return (string) $this->getComponent(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the instance string representation |
112
|
|
|
* with its optional URI delimiters |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
135 |
|
public function getUriComponent() |
117
|
|
|
{ |
118
|
135 |
|
return $this->__toString(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns an instance with the specified string |
123
|
|
|
* |
124
|
|
|
* This method MUST retain the state of the current instance, and return |
125
|
|
|
* an instance that contains the modified data |
126
|
|
|
* |
127
|
|
|
* @param string $value |
128
|
|
|
* |
129
|
|
|
* @return static |
130
|
|
|
*/ |
131
|
291 |
|
public function modify($value) |
132
|
|
|
{ |
133
|
291 |
|
if ($value === $this->getComponent()) { |
134
|
165 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
237 |
|
return new static($value); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|