1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\web; |
4
|
|
|
|
5
|
|
|
use luya\helpers\Url; |
6
|
|
|
use yii\base\InvalidConfigException; |
7
|
|
|
use luya\helpers\StringHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Generate External Link object. |
11
|
|
|
* |
12
|
|
|
* When href is provided without a protocol, for example `//go/there` the slashes are replaced |
13
|
|
|
* by the current base absolute base URL. |
14
|
|
|
* |
15
|
|
|
* @property string $href The external href link will be http ensured on set. |
16
|
|
|
* @property string $target Returns the link target. |
17
|
|
|
* |
18
|
|
|
* @author Basil Suter <[email protected]> |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
class WebsiteLink extends BaseLink |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string The default value which is used for website links is `_blank` you can override this property in order to change the default value. |
25
|
|
|
*/ |
26
|
|
|
public $defaultTarget = '_blank'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function init() |
32
|
|
|
{ |
33
|
|
|
parent::init(); |
34
|
|
|
|
35
|
|
|
if ($this->href === null) { |
36
|
|
|
throw new InvalidConfigException('The href attribute can not be empty and must be set trough configuration array.'); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private $_href; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set the href value for an external link resource. |
44
|
|
|
* |
45
|
|
|
* @param string $href The external link href value, the http protcol will be ensured. |
46
|
|
|
*/ |
47
|
|
|
public function setHref($href) |
48
|
|
|
{ |
49
|
|
|
if (StringHelper::startsWith($href, '//')) { |
50
|
|
|
$this->_href = Url::base(true) . str_replace('//', '/', $href); |
51
|
|
|
} else { |
52
|
|
|
$this->_href = Url::ensureHttp($href); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function getHref() |
60
|
|
|
{ |
61
|
|
|
return $this->_href; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private $_target; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Setter method for the link target. |
68
|
|
|
* |
69
|
|
|
* @param string $target A valid html link target attribute value. |
70
|
|
|
*/ |
71
|
|
|
public function setTarget($target) |
72
|
|
|
{ |
73
|
|
|
$this->_target = $target; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function getTarget() |
80
|
|
|
{ |
81
|
|
|
return empty($this->_target) ? $this->defaultTarget : $this->_target; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|