1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://raw.githubusercontent.com/flipboxfactory/craft-link/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-link |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\link\types; |
10
|
|
|
|
11
|
|
|
use craft\helpers\ArrayHelper; |
12
|
|
|
use craft\helpers\StringHelper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Flipbox Factory <[email protected]> |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
*/ |
18
|
|
|
trait BaseTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
public $allowText = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
public $requireText = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
public $showTarget = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $target = "_self"; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string|null |
42
|
|
|
*/ |
43
|
|
|
public $overrideText; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $label; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $identifier; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
abstract public function getUrl(): string; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string|null |
62
|
|
|
*/ |
63
|
|
|
public function getText() |
64
|
|
|
{ |
65
|
|
|
if ($this->allowText && $this->overrideText !== null) { |
66
|
|
|
return $this->overrideText; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->getUrl(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getIdentifier(): string |
76
|
|
|
{ |
77
|
|
|
if ($this->identifier === null) { |
78
|
|
|
$this->identifier = StringHelper::randomString(8); |
79
|
|
|
} |
80
|
|
|
return $this->identifier; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $identifier |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function setIdentifier(string $identifier): string |
88
|
|
|
{ |
89
|
|
|
$this->identifier = $identifier; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function properties(): array |
97
|
|
|
{ |
98
|
|
|
return array_diff($this->attributes(), $this->settings()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getProperties(): array |
105
|
|
|
{ |
106
|
|
|
$properties = []; |
107
|
|
|
|
108
|
|
|
foreach ($this->properties() as $property) { |
109
|
|
|
$properties[$property] = $this->$property; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $properties; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
|
|
public function attributes() |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
|
|
'identifier', |
122
|
|
|
'overrideText', |
123
|
|
|
'target' |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritdoc |
129
|
|
|
*/ |
130
|
|
|
public function settings(): array |
131
|
|
|
{ |
132
|
|
|
return [ |
133
|
|
|
'label', |
134
|
|
|
'showTarget', |
135
|
|
|
'allowText', |
136
|
|
|
'requireText' |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritdoc |
142
|
|
|
*/ |
143
|
|
|
public function getSettings(): array |
144
|
|
|
{ |
145
|
|
|
$settings = []; |
146
|
|
|
|
147
|
|
|
foreach ($this->settings() as $attribute) { |
148
|
|
|
$settings[$attribute] = $this->$attribute; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $settings; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param array $attributes |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getHtml(array $attributes = []): string |
159
|
|
|
{ |
160
|
|
|
$defaults = [ |
161
|
|
|
'href' => $this->getUrl(), |
162
|
|
|
'title' => $this->getText(), |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
if ($this->showTarget && $this->target) { |
166
|
|
|
$defaults['target'] = $this->target; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$text = ArrayHelper::remove($attributes, 'text', $this->getText()); |
170
|
|
|
|
171
|
|
|
$properties = array_filter( |
172
|
|
|
array_merge( |
173
|
|
|
$defaults, |
174
|
|
|
$attributes |
175
|
|
|
) |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
array_walk( |
179
|
|
|
$properties, |
180
|
|
|
function (&$v, $k) { |
181
|
|
|
$v = $k . '="' . $v . '"'; |
182
|
|
|
} |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
return '<a ' . implode(' ', $properties) . '>' . $text . '</a>'; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function __toString() |
192
|
|
|
{ |
193
|
|
|
return $this->getHtml(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|