1
|
|
|
<?php namespace Arcanedev\NoCaptcha\Utilities; |
2
|
|
|
|
3
|
|
|
use Arcanedev\NoCaptcha\Contracts\Utilities\AttributesInterface; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Attributes |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\NoCaptcha\Utilities |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Attributes implements AttributesInterface |
12
|
|
|
{ |
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
14
|
|
|
| Properties |
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
16
|
|
|
*/ |
17
|
|
|
/** |
18
|
|
|
* Attribute collection. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $items = []; |
23
|
|
|
|
24
|
|
|
/* ------------------------------------------------------------------------------------------------ |
25
|
|
|
| Getters & Setters |
26
|
|
|
| ------------------------------------------------------------------------------------------------ |
27
|
|
|
*/ |
28
|
|
|
/** |
29
|
|
|
* Get all items. |
30
|
|
|
* |
31
|
|
|
* @param string $siteKey |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
132 |
|
protected function getItems($siteKey) |
36
|
|
|
{ |
37
|
132 |
|
return array_merge( |
38
|
132 |
|
$this->getDefaultAttributes($siteKey), |
39
|
132 |
|
$this->items |
40
|
99 |
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get Default attributes. |
45
|
|
|
* |
46
|
|
|
* @param string $siteKey |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
132 |
|
private function getDefaultAttributes($siteKey) |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
132 |
|
'class' => 'g-recaptcha', |
54
|
132 |
|
'data-sitekey' => $siteKey, |
55
|
99 |
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set items. |
60
|
|
|
* |
61
|
|
|
* @param array $items |
62
|
|
|
* |
63
|
|
|
* @return self |
64
|
|
|
*/ |
65
|
132 |
|
private function setItems(array $items) |
66
|
|
|
{ |
67
|
132 |
|
$this->items = $items; |
68
|
|
|
|
69
|
132 |
|
$this->checkAttributes(); |
70
|
|
|
|
71
|
132 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get an item value by name. |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
132 |
|
private function getItem($name) |
82
|
|
|
{ |
83
|
132 |
|
if ( ! $this->hasItem($name)) { |
84
|
132 |
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
84 |
|
return $this->items[$name]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set an item. |
92
|
|
|
* |
93
|
|
|
* @param string $name |
94
|
|
|
* @param string $value |
95
|
|
|
* |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
84 |
|
private function setItem($name, $value) |
99
|
|
|
{ |
100
|
84 |
|
$this->items[$name] = $value; |
101
|
|
|
|
102
|
84 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get image type attribute. |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
12 |
|
public function getImageAttribute() |
111
|
|
|
{ |
112
|
12 |
|
return [self::ATTR_TYPE => 'image']; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get audio type attribute. |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
12 |
|
public function getAudioAttribute() |
121
|
|
|
{ |
122
|
12 |
|
return [self::ATTR_TYPE => 'audio']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/* ------------------------------------------------------------------------------------------------ |
126
|
|
|
| Main functions |
127
|
|
|
| ------------------------------------------------------------------------------------------------ |
128
|
|
|
*/ |
129
|
|
|
/** |
130
|
|
|
* Build attributes. |
131
|
|
|
* |
132
|
|
|
* @param string $siteKey |
133
|
|
|
* @param array $items |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
132 |
|
public function build($siteKey, array $items = []) |
138
|
|
|
{ |
139
|
132 |
|
$this->setItems($items); |
140
|
|
|
|
141
|
132 |
|
$output = []; |
142
|
|
|
|
143
|
132 |
|
foreach ($this->getItems($siteKey) as $key => $value) { |
144
|
132 |
|
$output[] = trim($key) . '="' . trim($value) . '"'; |
145
|
99 |
|
} |
146
|
|
|
|
147
|
132 |
|
return implode(' ', $output); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/* ------------------------------------------------------------------------------------------------ |
151
|
|
|
| Check functions |
152
|
|
|
| ------------------------------------------------------------------------------------------------ |
153
|
|
|
*/ |
154
|
|
|
/** |
155
|
|
|
* Check attributes. |
156
|
|
|
*/ |
157
|
132 |
|
private function checkAttributes() |
158
|
|
|
{ |
159
|
132 |
|
$this->checkTypeAttribute(); |
160
|
132 |
|
$this->checkThemeAttribute(); |
161
|
132 |
|
$this->checkSizeAttribute(); |
162
|
132 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check type attribute. |
166
|
|
|
*/ |
167
|
132 |
|
private function checkTypeAttribute() |
168
|
|
|
{ |
169
|
132 |
|
$this->checkDataAttribute(self::ATTR_TYPE, 'image', ['image', 'audio']); |
170
|
132 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Check theme attribute. |
174
|
|
|
*/ |
175
|
132 |
|
private function checkThemeAttribute() |
176
|
|
|
{ |
177
|
132 |
|
$this->checkDataAttribute(self::ATTR_THEME, 'light', ['light', 'dark']); |
178
|
132 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Check size attribute. |
182
|
|
|
*/ |
183
|
132 |
|
private function checkSizeAttribute() |
184
|
|
|
{ |
185
|
132 |
|
$this->checkDataAttribute(self::ATTR_SIZE, 'normal', ['normal', 'compact']); |
186
|
132 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Check data Attribute. |
190
|
|
|
* |
191
|
|
|
* @param string $name |
192
|
|
|
* @param string $default |
193
|
|
|
* @param array $available |
194
|
|
|
*/ |
195
|
132 |
|
private function checkDataAttribute($name, $default, array $available) |
196
|
|
|
{ |
197
|
132 |
|
$item = $this->getItem($name); |
198
|
|
|
|
199
|
132 |
|
if ( ! is_null($item)) { |
200
|
84 |
|
$item = (is_string($item) and in_array($item, $available)) |
201
|
81 |
|
? strtolower(trim($item)) |
202
|
84 |
|
: $default; |
203
|
|
|
|
204
|
84 |
|
$this->setItem($name, $item); |
205
|
63 |
|
} |
206
|
132 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Check if has an item. |
210
|
|
|
* |
211
|
|
|
* @param string $name |
212
|
|
|
* |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
132 |
|
private function hasItem($name) |
216
|
|
|
{ |
217
|
132 |
|
return array_key_exists($name, $this->items); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|