Completed
Push — development ( eb9524...db4517 )
by Andrij
28:49 queued 02:09
created

BannerEffects::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace xbanners\src\Entities;
4
5
/**
6
 * @author cray
7
 */
8
class BannerEffects implements \ArrayAccess
9
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
10
11
    protected $effects = [
12
        //@var boolean
13
        'autoplay' => 0,
14
        //@var int
15
        'autoplaySpeed' => 0,
16
        //@var boolean
17
        'arrows' => 0,
18
        //@var boolean
19
        'centerMode' => 0,
20
        //@var boolean
21
        'dots' => 0,
22
        //@var boolean
23
        'draggable' => 0,
24
        //@var boolean
25
        'fade' => 0,
26
        //@var string
27
        'easing' => '',
28
        //@var boolean
29
        'infinite' => 0,
30
        //@var boolean
31
        'pauseOnHover' => 0,
32
        //@var boolean
33
        'pauseOnDotsHover' => 0,
34
        //@var int
35
        'speed' => 0,
36
        //@var boolean
37
        'swipe' => 0,
38
        //@var boolean
39
        'touchMove' => 0,
40
        //@var boolean
41
        'vertical' => 0,
42
        //@var boolean
43
        'rtl' => 0,
44
        //@var string
45
        'scrollSpeed' => 0,
46
    ];
47
48
    protected $effectsTypes = [
49
        //@var boolean
50
        'autoplay' => 'integer',
51
        //@var int
52
        'autoplaySpeed' => 'integer',
53
        //@var boolean
54
        'arrows' => 'integer',
55
        //@var boolean
56
        'centerMode' => 'integer',
57
        //@var boolean
58
        'dots' => 'integer',
59
        //@var boolean
60
        'draggable' => 'integer',
61
        //@var boolean
62
        'fade' => 'integer',
63
        //@var string
64
        'easing' => 'string',
65
        //@var boolean
66
        'infinite' => 'integer',
67
        //@var boolean
68
        'pauseOnHover' => 'integer',
69
        //@var boolean
70
        'pauseOnDotsHover' => 'integer',
71
        //@var int
72
        'speed' => 'integer',
73
        //@var boolean
74
        'swipe' => 'integer',
75
        //@var boolean
76
        'touchMove' => 'integer',
77
        //@var boolean
78
        'vertical' => 'integer',
79
        //@var boolean
80
        'rtl' => 'integer',
81
        //@var string
82
        'scrollSpeed' => 'string',
83
    ];
84
85
    /**
86
     * @param jsonString|array $data
0 ignored issues
show
Documentation introduced by
Should the type for parameter $data not be jsonString|array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
87
     */
88
    public function __construct($data = null, $mergeWithDefault = false) {
89
        if (null === $data) {
90
            $data = $this->getDefaultEffects();
91
        }
92
        if (is_string($data)) {
93
            $data = json_decode($data, true);
94
        }
95
        if (is_array($data)) {
96
            $this->fromArray($data, $mergeWithDefault);
97
        }
98
    }
99
100
    /**
101
     * @param array $effects
102
     */
103
    protected function fromArray(array $effects, $mergeWithDefault = false) {
104
        if (true === $mergeWithDefault) {
105
            $this->effects = $this->getDefaultEffects();
106
        }
107
108
        foreach ($effects as $property => $value) {
109
            $value = (('on' === $value) ? 1 : $value);
110
            if (array_key_exists($property, $this->effects)) {
111
112
                if ($this->effectsTypes[$property]) {
113
                    settype($value, $this->effectsTypes[$property]);
114
                }
115
                $this->effects[$property] = $value;
116
            }
117
        }
118
    }
119
120
    public function toArray() {
121
        return $this->effects;
122
    }
123
124
    /**
125
     * -------------------------------------------------------------------------
126
     *               ArrayAccess implementation
127
     * -------------------------------------------------------------------------
128
     */
129
    public function offsetExists($offset) {
130
        return array_key_exists($offset, $this->effects);
131
    }
132
133
    public function offsetGet($offset) {
134
        return $this->effects[$offset];
135
    }
136
137
    public function offsetSet($offset, $value) {
138
        return $this->effects[$offset] = $value;
139
    }
140
141
    public function offsetUnset($offset) {
142
        unset($this->effects[$offset]);
143
    }
144
145
    /**
146
     * -------------------------------------------------------------------------
147
     *                      MAGIC
148
     * -------------------------------------------------------------------------
149
     */
150
    public function __toString() {
151
        return json_encode($this->effects);
152
    }
153
154
    /**
155
     * -------------------------------------------------------------------------
156
     *                      DATA
157
     * -------------------------------------------------------------------------
158
     */
159
    public function getDefaultEffects() {
160
        return [
161
            'autoplay' => false,
162
            'autoplaySpeed' => 3,
163
            'arrows' => true,
164
            'centerMode' => false,
165
            'dots' => false,
166
            'draggable' => true,
167
            'fade' => false,
168
            'easing' => 'linear',
169
            'infinite' => true,
170
            'pauseOnHover' => true,
171
            'pauseOnDotsHover' => false,
172
            'speed' => 1,
173
            'swipe' => true,
174
            'touchMove' => true,
175
            'vertical' => false,
176
            'rtl' => false,
177
            'scrollSpeed' => 1,
178
        ];
179
    }
180
181
    /**
182
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
183
     */
184
    public function getEasingTypes() {
185
        return [
186
            'linear',
187
            'easeInSine',
188
            'easeOutSine',
189
            'easeInOutSine',
190
            'easeInQuad',
191
            'easeOutQuad',
192
            'easeInOutQuad',
193
            'easeInCubic',
194
            'easeOutCubic',
195
            'easeInOutCubic',
196
            'easeInQuart',
197
            'easeOutQuart',
198
            'easeInOutQuart',
199
            'easeInQuint',
200
            'easeOutQuint',
201
            'easeInOutQuint',
202
            'easeInExpo',
203
            'easeOutExpo',
204
            'easeInOutExpo',
205
            'easeInCirc',
206
            'easeOutCirc',
207
            'easeInOutCirc',
208
            'easeInBack',
209
            'easeOutBack',
210
            'easeInOutBack',
211
            'easeInElastic',
212
            'easeOutElastic',
213
            'easeInOutElastic',
214
            'easeInBounce',
215
            'easeOutBounce',
216
            'easeInOutBounce',
217
        ];
218
    }
219
220
}