Completed
Push — master ( 25e2a3...2fc870 )
by ARCANEDEV
8s
created

Attributes::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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