1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_address_handler; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Forward |
8
|
|
|
* @package kalanis\kw_address_handler |
9
|
|
|
* Forwarding requests |
10
|
|
|
*/ |
11
|
|
|
class Forward |
12
|
|
|
{ |
13
|
|
|
public const KEY_FORWARD = 'fwd'; |
14
|
|
|
|
15
|
|
|
protected Handler $urlHandler; |
16
|
|
|
|
17
|
4 |
|
public function __construct() |
18
|
|
|
{ |
19
|
4 |
|
$this->urlHandler = new Handler(); |
20
|
4 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $link |
24
|
|
|
* @throws HandlerException |
25
|
|
|
* @return $this |
26
|
|
|
*/ |
27
|
4 |
|
public function setLink(string $link): self |
28
|
|
|
{ |
29
|
4 |
|
return $this->setSource(new Sources\Address($link)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @throws HandlerException |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
4 |
|
public function getLink(): string |
37
|
|
|
{ |
38
|
4 |
|
return strval($this->urlHandler->getAddress()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Sources\Sources $sources |
43
|
|
|
* @throws HandlerException |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
4 |
|
public function setSource(Sources\Sources $sources): self |
47
|
|
|
{ |
48
|
4 |
|
$this->urlHandler->setSource($sources); |
49
|
4 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
public function setForward(string $forward): self |
53
|
|
|
{ |
54
|
4 |
|
$urlVariable = new SingleVariable($this->urlHandler->getParams()); |
55
|
4 |
|
$urlVariable->setVariableName(static::KEY_FORWARD); |
56
|
4 |
|
$urlVariable->setVariableValue($forward); |
57
|
4 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param bool $extraRule |
62
|
|
|
* @codeCoverageIgnore access external call |
63
|
|
|
*/ |
64
|
|
|
public function forward(bool $extraRule = true): void |
65
|
|
|
{ |
66
|
|
|
if ($extraRule && $this->has()) { |
67
|
|
|
new Redirect($this->get()); // external |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
public function has(): bool |
72
|
|
|
{ |
73
|
4 |
|
return !empty($this->get()); |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
public function get(): string |
77
|
|
|
{ |
78
|
4 |
|
$urlVariable = new SingleVariable($this->urlHandler->getParams()); |
79
|
4 |
|
$urlVariable->setVariableName(static::KEY_FORWARD); |
80
|
4 |
|
return $urlVariable->getVariableValue(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|