Completed
Push — master ( 7c2bcc...cd5c4f )
by ARCANEDEV
11s
created

Attributes   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 261
c 0
b 0
f 0
wmc 24
lcom 1
cbo 1
ccs 70
cts 70
cp 1
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getItems() 0 7 1
A getDefaultAttributes() 0 7 1
A setItems() 0 8 1
A getItem() 0 8 2
A setItem() 0 6 1
A getImageAttribute() 0 4 1
A getAudioAttribute() 0 4 1
A build() 0 12 2
A prepareNameAttribute() 0 12 3
A checkAttributes() 0 7 1
A checkTypeAttribute() 0 4 1
A checkThemeAttribute() 0 4 1
A checkSizeAttribute() 0 4 1
A checkBadgeAttribute() 0 4 1
A checkDataAttribute() 0 12 4
A hasItem() 0 4 1
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 102
    public function __construct(array $defaults = [])
43
    {
44 102
        $this->defaults = array_filter($defaults);
45 102
    }
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Getters & Setters
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * Get all items.
53
     *
54
     * @param  string  $siteKey
55
     *
56
     * @return array
57
     */
58 45
    protected function getItems($siteKey)
59
    {
60 45
        return array_merge(
61 45
            $this->getDefaultAttributes($siteKey),
62 45
            $this->items
63 15
        );
64
    }
65
66
    /**
67
     * Get Default attributes.
68
     *
69
     * @param  string  $siteKey
70
     *
71
     * @return array
72
     */
73 45
    private function getDefaultAttributes($siteKey)
74
    {
75
        return [
76 45
            'class'        => 'g-recaptcha',
77 45
            'data-sitekey' => $siteKey,
78 15
        ];
79
    }
80
81
    /**
82
     * Set items.
83
     *
84
     * @param  array  $items
85
     *
86
     * @return self
87
     */
88 45
    private function setItems(array $items)
89
    {
90 45
        $this->items = array_merge($this->defaults, $items);
91
92 45
        $this->checkAttributes();
93
94 45
        return $this;
95
    }
96
97
    /**
98
     * Get an item value by name.
99
     *
100
     * @param  string  $name
101
     *
102
     * @return string
103
     */
104 45
    private function getItem($name)
105
    {
106 45
        if ( ! $this->hasItem($name)) {
107 45
            return null;
108
        }
109
110 33
        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 33
    private function setItem($name, $value)
122
    {
123 33
        $this->items[$name] = $value;
124
125 33
        return $this;
126
    }
127
128
    /**
129
     * Get image type attribute.
130
     *
131
     * @return array
132
     */
133 9
    public function getImageAttribute()
134
    {
135 9
        return [self::ATTR_TYPE => 'image'];
136
    }
137
138
    /**
139
     * Get audio type attribute.
140
     *
141
     * @return array
142
     */
143 9
    public function getAudioAttribute()
144
    {
145 9
        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 45
    public function build($siteKey, array $items = [])
161
    {
162 45
        $this->setItems($items);
163
164 45
        $output = [];
165
166 45
        foreach ($this->getItems($siteKey) as $key => $value) {
167 45
            $output[] = trim($key) . '="' . trim($value) . '"';
168 15
        }
169
170 45
        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 33
    public function prepareNameAttribute($name)
183
    {
184 33
        if (is_null($name)) return [];
185
186 30
        if ($name === NoCaptcha::CAPTCHA_NAME) {
187 9
            throw new InvalidArgumentException(
188 9
                'The captcha name must be different from "' . NoCaptcha::CAPTCHA_NAME . '".'
189 3
            );
190
        }
191
192 21
        return array_combine(['id', 'name'], [$name, $name]);
193
    }
194
195
    /* ------------------------------------------------------------------------------------------------
196
     |  Check functions
197
     | ------------------------------------------------------------------------------------------------
198
     */
199
    /**
200
     * Check attributes.
201
     */
202 45
    private function checkAttributes()
203
    {
204 45
        $this->checkTypeAttribute();
205 45
        $this->checkThemeAttribute();
206 45
        $this->checkSizeAttribute();
207 45
        $this->checkBadgeAttribute();
208 45
    }
209
210
    /**
211
     * Check type attribute.
212
     */
213 45
    private function checkTypeAttribute()
214
    {
215 45
        $this->checkDataAttribute(self::ATTR_TYPE, 'image', ['image', 'audio']);
216 45
    }
217
218
    /**
219
     * Check theme attribute.
220
     */
221 45
    private function checkThemeAttribute()
222
    {
223 45
        $this->checkDataAttribute(self::ATTR_THEME, 'light', ['light', 'dark']);
224 45
    }
225
226
    /**
227
     * Check size attribute.
228
     */
229 45
    private function checkSizeAttribute()
230
    {
231 45
        $this->checkDataAttribute(self::ATTR_SIZE, 'normal', ['normal', 'compact', 'invisible']);
232 45
    }
233
234
    /**
235
     * Check badge attribute.
236
     */
237 45
    private function checkBadgeAttribute()
238
    {
239 45
        $this->checkDataAttribute(self::ATTR_BADGE, 'bottomright', ['bottomright', 'bottomleft', 'inline']);
240 45
    }
241
242
    /**
243
     * Check data Attribute.
244
     *
245
     * @param  string  $name
246
     * @param  string  $default
247
     * @param  array   $available
248
     */
249 45
    private function checkDataAttribute($name, $default, array $available)
250
    {
251 45
        $item = $this->getItem($name);
252
253 45
        if ( ! is_null($item)) {
254 33
            $item = (is_string($item) and in_array($item, $available))
255 31
                ? strtolower(trim($item))
256 33
                : $default;
257
258 33
            $this->setItem($name, $item);
259 11
        }
260 45
    }
261
262
    /**
263
     * Check if has an item.
264
     *
265
     * @param  string  $name
266
     *
267
     * @return bool
268
     */
269 45
    private function hasItem($name)
270
    {
271 45
        return array_key_exists($name, $this->items);
272
    }
273
}
274