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
|
|
|
use ReflectionClass; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* An abstract class to ease component manipulation |
19
|
|
|
* |
20
|
|
|
* @package League.uri |
21
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
22
|
|
|
* @since 4.0.0 |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractComponent |
25
|
|
|
{ |
26
|
|
|
use ImmutableComponentTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The component data |
30
|
|
|
* |
31
|
|
|
* @var int|string |
32
|
|
|
*/ |
33
|
|
|
protected $data; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
12 |
|
public static function __set_state(array $properties) |
39
|
|
|
{ |
40
|
12 |
|
return new static($properties['data']); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* new instance |
45
|
|
|
* |
46
|
|
|
* @param string|null $data the component value |
47
|
|
|
*/ |
48
|
1262 |
|
public function __construct($data = null) |
49
|
|
|
{ |
50
|
1262 |
|
$this->data = $this->validate($data); |
51
|
1178 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Validate the component string |
55
|
|
|
* |
56
|
|
|
* @param mixed $data |
57
|
|
|
* |
58
|
|
|
* @throws InvalidArgumentException if the component is no valid |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
1018 |
|
protected function validate($data) |
63
|
|
|
{ |
64
|
1018 |
|
if (null === $data) { |
65
|
705 |
|
return $data; |
66
|
|
|
} |
67
|
|
|
|
68
|
667 |
|
return $this->decodeComponent($this->validateString($data)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The component raw data |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
768 |
|
public function getContent() |
78
|
|
|
{ |
79
|
768 |
|
return $this->data; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
10 |
|
public function __debugInfo() |
86
|
|
|
{ |
87
|
10 |
|
$component = strtolower((new ReflectionClass($this))->getShortName()); |
88
|
|
|
|
89
|
10 |
|
return [$component => $this->getContent()]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the instance string representation; If the |
94
|
|
|
* instance is not defined an empty string is returned |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
1137 |
|
public function __toString() |
99
|
|
|
{ |
100
|
1137 |
|
return (string) $this->getContent(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the instance string representation |
105
|
|
|
* with its optional URI delimiters |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
183 |
|
public function getUriComponent() |
110
|
|
|
{ |
111
|
183 |
|
return $this->__toString(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns an instance with the specified string |
116
|
|
|
* |
117
|
|
|
* This method MUST retain the state of the current instance, and return |
118
|
|
|
* an instance that contains the modified data |
119
|
|
|
* |
120
|
|
|
* @param string $value |
121
|
|
|
* |
122
|
|
|
* @return static |
123
|
|
|
*/ |
124
|
387 |
|
public function modify($value) |
125
|
|
|
{ |
126
|
387 |
|
if ($value === $this->getContent()) { |
127
|
258 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
339 |
|
return new static($value); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|