Passed
Push — master ( 47c941...a9f564 )
by Roberto
09:10 queued 11s
created

ReCaptchaBuilderV2::cleanAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * Copyright (c) 2017 - present
5
 * LaravelGoogleRecaptcha - ReCaptchaBuilderV2.php
6
 * author: Roberto Belotti - [email protected]
7
 * web : robertobelotti.com, github.com/biscolab
8
 * Initial version created on: 12/9/2018
9
 * MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
10
 */
11
12
namespace Biscolab\ReCaptcha;
13
14
use Biscolab\ReCaptcha\Exceptions\InvalidConfigurationException;
15
use Illuminate\Support\Arr;
16
17
/**
18
 * Class ReCaptchaBuilderV2
19
 * @package Biscolab\ReCaptcha
20
 */
21
class ReCaptchaBuilderV2 extends ReCaptchaBuilder
22
{
23
24
    protected static $allowed_data_attribute = [
25
        "theme",
26
        "size",
27
        "tabindex",
28
        "callback",
29
        "expired-callback",
30
        "error-callback",
31
    ];
32
33
    /**
34
     * ReCaptchaBuilderV2 constructor.
35
     *
36
     * @param string $api_site_key
37
     * @param string $api_secret_key
38
     */
39 34
    public function __construct(string $api_site_key, string $api_secret_key)
40
    {
41
42 34
        parent::__construct($api_site_key, $api_secret_key, 'v2');
43 34
    }
44
45
    /**
46
     * Write ReCAPTCHA HTML tag in your FORM
47
     * Insert before </form> tag
48
     * 
49
     * @param null|array $attributes
50
     * @return string
51
     */
52 5
    public function htmlFormSnippet(?array $attributes = []): string
53
    {
54
55 5
        $data_attributes = [];
56 5
        $config_data_attributes = array_merge($this->getTagAttributes(), self::cleanAttributes($attributes));
57 5
        ksort($config_data_attributes);
58 5
        foreach ($config_data_attributes as $k => $v) {
59 5
            if ($v) {
60 5
                $data_attributes[] = 'data-' . $k . '="' . $v . '"';
61
            }
62
        }
63
64 5
        $html = '<div class="g-recaptcha" ' . implode(" ", $data_attributes) . ' id="recaptcha-element"></div>';
65
66 5
        return $html;
67
    }
68
69
    /**
70
     * @return array
71
     * @throws InvalidConfigurationException
72
     */
73 10
    public function getTagAttributes(): array
74
    {
75
76
        $tag_attributes = [
77 10
            'sitekey' => $this->api_site_key
78
        ];
79
80 10
        $tag_attributes = array_merge($tag_attributes, config('recaptcha.tag_attributes', []));
81
82 10
        if (Arr::get($tag_attributes, 'callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) {
83 1
            throw new InvalidConfigurationException('Property "callback" ("data-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"');
84
        }
85
86 9
        if (Arr::get($tag_attributes, 'expired-callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) {
87
            throw new InvalidConfigurationException('Property "expired-callback" ("data-expired-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"');
88
        }
89
90 9
        if (Arr::get($tag_attributes, 'error-callback') === ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION) {
91
            throw new InvalidConfigurationException('Property "error-callback" ("data-error-callback") must be different from "' . ReCaptchaBuilder::DEFAULT_ONLOAD_JS_FUNCTION . '"');
92
        }
93
94 9
        return $tag_attributes;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 3
    public function getOnLoadCallback(): string
101
    {
102
103 3
        $attributes = $this->getTagAttributes();
104
105 2
        return "<script>var biscolabOnloadCallback = function() {grecaptcha.render('recaptcha-element', " . json_encode($attributes) . ");};</script>";
106
    }
107
108
    /**
109
     * Compare given attributes with allowed attributes
110
     *
111
     * @param array|null $attributes
112
     * @return array
113
     */
114 6
    public static function cleanAttributes(?array $attributes = []): array
115
    {
116
        return array_filter($attributes, function ($k) {
117 3
            return in_array($k, self::$allowed_data_attribute);
118 6
        }, ARRAY_FILTER_USE_KEY);
119
    }
120
}
121