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
|
1420 |
|
public function __construct($data = null) |
48
|
|
|
{ |
49
|
1420 |
|
$this->data = $this->validate($data); |
50
|
1324 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Validate the component string |
54
|
|
|
* |
55
|
|
|
* @param mixed $data |
56
|
|
|
* |
57
|
|
|
* @throws InvalidArgumentException if the component is no valid |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
1084 |
|
protected function validate($data) |
62
|
|
|
{ |
63
|
1084 |
|
if (null === $data) { |
64
|
762 |
|
return $data; |
65
|
|
|
} |
66
|
|
|
|
67
|
688 |
|
return $this->decodeComponent($this->validateString($data)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The component raw data |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
834 |
|
public function getContent() |
77
|
|
|
{ |
78
|
834 |
|
return $this->data; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the instance string representation; If the |
83
|
|
|
* instance is not defined an empty string is returned |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
1218 |
|
public function __toString() |
88
|
|
|
{ |
89
|
1218 |
|
return (string) $this->getContent(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the instance string representation |
94
|
|
|
* with its optional URI delimiters |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
183 |
|
public function getUriComponent() |
99
|
|
|
{ |
100
|
183 |
|
return $this->__toString(); |
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
|
435 |
|
public function modify($value) |
114
|
|
|
{ |
115
|
435 |
|
if ($value === $this->getContent()) { |
116
|
306 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
366 |
|
return new static($value); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|