-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCaptcha.php
52 lines (42 loc) · 1.44 KB
/
Captcha.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace romanzipp\Turnstile;
use Illuminate\Support\HtmlString;
class Captcha
{
public static function getScript(bool $async = true, bool $defer = true, string $callback = null): HtmlString
{
$attributes = [];
if ($async) {
$attributes[] = 'async';
}
if ($defer) {
$attributes[] = 'defer';
}
if ( ! empty($callback)) {
$attributes[] = 'data-callback="' . $callback . '"';
}
return new HtmlString(
sprintf('<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" %s></script>', implode(' ', $attributes))
);
}
public static function getChallenge(string $theme = null, string $action = null, string $cData = null): HtmlString
{
$parameters = [
'class' => 'cf-turnstile',
'data-sitekey' => config('turnstile.site_key'),
'data-theme' => $theme,
'data-action' => $action,
'data-cdata' => $cData,
];
$parameters = array_filter($parameters, fn ($value, string $key) => ! empty($value), ARRAY_FILTER_USE_BOTH);
return new HtmlString(
sprintf(
'<div %s></div>',
implode(
' ',
array_map(fn (string $key, $value) => "{$key}=\"{$value}\"", array_keys($parameters), array_values($parameters))
)
)
);
}
}