|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HumanDirect\Socially; |
|
4
|
|
|
|
|
5
|
|
|
use LayerShifter\TLDExtract\ResultInterface as TldResultInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Result. |
|
9
|
|
|
*/ |
|
10
|
|
|
class Result implements \ArrayAccess, ResultInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var TldResultInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
private $tldResult; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor of class. |
|
19
|
|
|
* |
|
20
|
|
|
* @param TldResultInterface $result |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(TldResultInterface $result) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->tldResult = $result; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @param null|string $subdomain |
|
31
|
|
|
* @param null|string $hostname |
|
32
|
|
|
* @param null|string $suffix |
|
33
|
|
|
* |
|
34
|
|
|
* @return ResultInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function create(?string $subdomain, ?string $hostname, ?string $suffix): ResultInterface |
|
37
|
|
|
{ |
|
38
|
|
|
return new self(Factory::createTldResult($subdomain, $hostname, $suffix)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Create instance from URL. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $url |
|
45
|
|
|
* |
|
46
|
|
|
* @throws SociallyException |
|
47
|
|
|
* |
|
48
|
|
|
* @return ResultInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function createFromUrl(string $url): ResultInterface |
|
51
|
|
|
{ |
|
52
|
|
|
try { |
|
53
|
|
|
$extractor = Factory::createTldExtractor(); |
|
54
|
|
|
} catch (\Exception $e) { |
|
55
|
|
|
throw new SociallyException($e->getMessage(), $e->getCode(), $e); |
|
56
|
|
|
} |
|
57
|
|
|
$result = $extractor->parse($url); |
|
58
|
|
|
|
|
59
|
|
|
return new self(Factory::createTldResult( |
|
60
|
|
|
$result->getSubdomain(), |
|
61
|
|
|
$result->getHostname(), |
|
62
|
|
|
$result->getSuffix() |
|
63
|
|
|
)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns subdomain if it exists. |
|
68
|
|
|
* |
|
69
|
|
|
* @return null|string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getSubdomain(): ?string |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->tldResult->getSubdomain(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Return subdomains if they exist, example subdomain is "www.news", method will return array ['www', 'news']. |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getSubdomains(): array |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->tldResult->getSubdomains(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns hostname if it exists. |
|
88
|
|
|
* |
|
89
|
|
|
* @return null|string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getHostname(): ?string |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->tldResult->getHostname(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns suffix if it exists. |
|
98
|
|
|
* |
|
99
|
|
|
* @return null|string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getSuffix(): ?string |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->tldResult->getSuffix(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Method that returns full host record. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getFullHost(): string |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->tldResult->getFullHost(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns registrable domain or null. |
|
118
|
|
|
* |
|
119
|
|
|
* @return null|string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getRegistrableDomain(): ?string |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->tldResult->getRegistrableDomain(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Returns true if domain is valid. |
|
128
|
|
|
* |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
|
|
public function isValidDomain(): bool |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->tldResult->isValidDomain(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Magic method for run isset on private params. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $name Field name |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function __isset($name) |
|
144
|
|
|
{ |
|
145
|
|
|
return property_exists($this->tldResult, $name) || property_exists($this, $name); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Converts class fields to string. |
|
150
|
|
|
* |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
public function __toString(): string |
|
154
|
|
|
{ |
|
155
|
|
|
return (string) $this->tldResult; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Whether or not an offset exists. |
|
160
|
|
|
* |
|
161
|
|
|
* @param mixed $offset An offset to check for |
|
162
|
|
|
* |
|
163
|
|
|
* @return bool |
|
164
|
|
|
*/ |
|
165
|
|
|
public function offsetExists($offset): bool |
|
166
|
|
|
{ |
|
167
|
|
|
return property_exists($this->tldResult, $offset) || property_exists($this, $offset); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Returns the value at specified offset. |
|
172
|
|
|
* |
|
173
|
|
|
* @param mixed $offset The offset to retrieve. |
|
174
|
|
|
* |
|
175
|
|
|
* @throws \OutOfRangeException |
|
176
|
|
|
* |
|
177
|
|
|
* @return mixed |
|
178
|
|
|
*/ |
|
179
|
|
|
public function offsetGet($offset) |
|
180
|
|
|
{ |
|
181
|
|
|
if (property_exists($this->tldResult, $offset)) { |
|
182
|
|
|
return $this->tldResult->{$offset}; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (property_exists($this, $offset)) { |
|
186
|
|
|
return $this->{$offset}; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
throw new \OutOfRangeException(sprintf('Offset "%s" does not exist.', $offset)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Magic method, controls access to private params. |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $name Name of params to retrieve |
|
196
|
|
|
* |
|
197
|
|
|
* @throws \OutOfRangeException |
|
198
|
|
|
* |
|
199
|
|
|
* @return mixed |
|
200
|
|
|
*/ |
|
201
|
|
|
public function __get($name) |
|
202
|
|
|
{ |
|
203
|
|
|
if (property_exists($this->tldResult, $name)) { |
|
204
|
|
|
return $this->tldResult->{$name}; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
if (property_exists($this, $name)) { |
|
208
|
|
|
return $this->{$name}; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
throw new \OutOfRangeException(sprintf('Unknown field "%s"', $name)); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Magic method, makes params read-only. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $name Name of params to retrieve |
|
218
|
|
|
* @param mixed $value Value to set |
|
219
|
|
|
* |
|
220
|
|
|
* @throws \LogicException |
|
221
|
|
|
* |
|
222
|
|
|
* @return void |
|
223
|
|
|
*/ |
|
224
|
|
|
public function __set($name, $value) |
|
225
|
|
|
{ |
|
226
|
|
|
throw new \LogicException("Can't modify an immutable object."); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Disables assigns a value to the specified offset. |
|
231
|
|
|
* |
|
232
|
|
|
* @param mixed $offset The offset to assign the value to |
|
233
|
|
|
* @param mixed $value Value to set |
|
234
|
|
|
* |
|
235
|
|
|
* @throws \LogicException |
|
236
|
|
|
* |
|
237
|
|
|
* @return void |
|
238
|
|
|
*/ |
|
239
|
|
|
public function offsetSet($offset, $value): void |
|
240
|
|
|
{ |
|
241
|
|
|
throw new \LogicException( |
|
242
|
|
|
sprintf("Can't modify an immutable object. You tried to set value '%s' to field '%s'.", $value, $offset) |
|
243
|
|
|
); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Disables unset of an offset. |
|
248
|
|
|
* |
|
249
|
|
|
* @param mixed $offset The offset for unset |
|
250
|
|
|
* |
|
251
|
|
|
* @throws \LogicException |
|
252
|
|
|
* |
|
253
|
|
|
* @return void |
|
254
|
|
|
*/ |
|
255
|
|
|
public function offsetUnset($offset): void |
|
256
|
|
|
{ |
|
257
|
|
|
throw new \LogicException(sprintf("Can't modify an immutable object. You tried to unset '%s.'", $offset)); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Get the domain name components as a native PHP array. The returned array will contain these keys: 'subdomain', |
|
262
|
|
|
* 'domain' and 'tld'. |
|
263
|
|
|
* |
|
264
|
|
|
* @return array |
|
265
|
|
|
*/ |
|
266
|
|
|
public function toArray(): array |
|
267
|
|
|
{ |
|
268
|
|
|
return [ |
|
269
|
|
|
'subdomain' => $this->getSubdomain(), |
|
270
|
|
|
'hostname' => $this->getHostname(), |
|
271
|
|
|
'suffix' => $this->getSuffix(), |
|
272
|
|
|
]; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Get the domain name components as a JSON. |
|
277
|
|
|
* |
|
278
|
|
|
* @return string |
|
279
|
|
|
*/ |
|
280
|
|
|
public function toJson(): string |
|
281
|
|
|
{ |
|
282
|
|
|
return json_encode($this->toArray()); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|