Completed
Push — master ( b91c46...cf06f8 )
by ARCANEDEV
7s
created

Attributes::prepareNameAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php namespace Arcanedev\NoCaptcha\Utilities;
2
3
use Arcanedev\NoCaptcha\Contracts\Utilities\AttributesInterface;
4
use Arcanedev\NoCaptcha\Exceptions\InvalidArgumentException;
5
use Arcanedev\NoCaptcha\NoCaptcha;
6
7
/**
8
 * Class     Attributes
9
 *
10
 * @package  Arcanedev\NoCaptcha\Utilities
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class Attributes implements AttributesInterface
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Attribute collection.
21
     *
22
     * @var array
23
     */
24
    protected $items    = [];
25
26
    /**
27
     * Defaults attributes.
28
     *
29
     * @var array
30
     */
31
    protected $defaults = [];
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Constructor
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * Attributes constructor.
39
     *
40
     * @param  array  $defaults
41
     */
42 396
    public function __construct(array $defaults = [])
43
    {
44 396
        $this->defaults = array_filter($defaults);
45 396
    }
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Getters & Setters
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * Get all items.
53
     *
54
     * @param  string  $siteKey
55
     *
56
     * @return array
57
     */
58 168
    protected function getItems($siteKey)
59
    {
60 168
        return array_merge(
61 168
            $this->getDefaultAttributes($siteKey),
62 168
            $this->items
63 126
        );
64
    }
65
66
    /**
67
     * Get Default attributes.
68
     *
69
     * @param  string  $siteKey
70
     *
71
     * @return array
72
     */
73 168
    private function getDefaultAttributes($siteKey)
74
    {
75
        return [
76 168
            'class'        => 'g-recaptcha',
77 168
            'data-sitekey' => $siteKey,
78 126
        ];
79
    }
80
81
    /**
82
     * Set items.
83
     *
84
     * @param  array  $items
85
     *
86
     * @return self
87
     */
88 168
    private function setItems(array $items)
89
    {
90 168
        $this->items = array_merge($this->defaults, $items);
91
92 168
        $this->checkAttributes();
93
94 168
        return $this;
95
    }
96
97
    /**
98
     * Get an item value by name.
99
     *
100
     * @param  string  $name
101
     *
102
     * @return string
103
     */
104 168
    private function getItem($name)
105
    {
106 168
        if ( ! $this->hasItem($name)) {
107 168
            return null;
108
        }
109
110 120
        return $this->items[$name];
111
    }
112
113
    /**
114
     * Set an item.
115
     *
116
     * @param  string  $name
117
     * @param  string  $value
118
     *
119
     * @return self
120
     */
121 120
    private function setItem($name, $value)
122
    {
123 120
        $this->items[$name] = $value;
124
125 120
        return $this;
126
    }
127
128
    /**
129
     * Get image type attribute.
130
     *
131
     * @return array
132
     */
133 36
    public function getImageAttribute()
134
    {
135 36
        return [self::ATTR_TYPE => 'image'];
136
    }
137
138
    /**
139
     * Get audio type attribute.
140
     *
141
     * @return array
142
     */
143 36
    public function getAudioAttribute()
144
    {
145 36
        return [self::ATTR_TYPE => 'audio'];
146
    }
147
148
    /* ------------------------------------------------------------------------------------------------
149
     |  Main functions
150
     | ------------------------------------------------------------------------------------------------
151
     */
152
    /**
153
     * Build attributes.
154
     *
155
     * @param  string  $siteKey
156
     * @param  array   $items
157
     *
158
     * @return string
159
     */
160 168
    public function build($siteKey, array $items = [])
161
    {
162 168
        $this->setItems($items);
163
164 168
        $output = [];
165
166 168
        foreach ($this->getItems($siteKey) as $key => $value) {
167 168
            $output[] = trim($key) . '="' . trim($value) . '"';
168 126
        }
169
170 168
        return implode(' ', $output);
171
    }
172
173
    /**
174
     * Prepare the name and id attributes.
175
     *
176
     * @param  string|null  $name
177
     *
178
     * @return array
179
     *
180
     * @throws \Arcanedev\NoCaptcha\Exceptions\InvalidArgumentException
181
     */
182 132
    public function prepareNameAttribute($name)
183
    {
184 132
        if (is_null($name)) return [];
185
186 120
        if ($name === NoCaptcha::CAPTCHA_NAME) {
187 36
            throw new InvalidArgumentException(
188 36
                'The captcha name must be different from "' . NoCaptcha::CAPTCHA_NAME . '".'
189 27
            );
190
        }
191
192 84
        return array_combine(['id', 'name'], [$name, $name]);
193
    }
194
195
    /* ------------------------------------------------------------------------------------------------
196
     |  Check functions
197
     | ------------------------------------------------------------------------------------------------
198
     */
199
    /**
200
     * Check attributes.
201
     */
202 168
    private function checkAttributes()
203
    {
204 168
        $this->checkTypeAttribute();
205 168
        $this->checkThemeAttribute();
206 168
        $this->checkSizeAttribute();
207 168
    }
208
209
    /**
210
     * Check type attribute.
211
     */
212 168
    private function checkTypeAttribute()
213
    {
214 168
        $this->checkDataAttribute(self::ATTR_TYPE, 'image', ['image', 'audio']);
215 168
    }
216
217
    /**
218
     * Check theme attribute.
219
     */
220 168
    private function checkThemeAttribute()
221
    {
222 168
        $this->checkDataAttribute(self::ATTR_THEME, 'light', ['light', 'dark']);
223 168
    }
224
225
    /**
226
     * Check size attribute.
227
     */
228 168
    private function checkSizeAttribute()
229
    {
230 168
        $this->checkDataAttribute(self::ATTR_SIZE, 'normal', ['normal', 'compact']);
231 168
    }
232
233
    /**
234
     * Check data Attribute.
235
     *
236
     * @param  string  $name
237
     * @param  string  $default
238
     * @param  array   $available
239
     */
240 168
    private function checkDataAttribute($name, $default, array $available)
241
    {
242 168
        $item = $this->getItem($name);
243
244 168
        if ( ! is_null($item)) {
245 120
            $item = (is_string($item) and in_array($item, $available))
246 117
                ? strtolower(trim($item))
247 120
                : $default;
248
249 120
            $this->setItem($name, $item);
250 90
        }
251 168
    }
252
253
    /**
254
     * Check if has an item.
255
     *
256
     * @param  string  $name
257
     *
258
     * @return bool
259
     */
260 168
    private function hasItem($name)
261
    {
262 168
        return array_key_exists($name, $this->items);
263
    }
264
}
265