Completed
Push — master ( 697c38...fae26d )
by Marko
9s
created

Animation::setAttribute()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 7
ccs 5
cts 5
cp 1
rs 9.2
cc 4
eloc 4
nc 2
nop 3
crap 4
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 20, 2016 - 9:07:01 PM
5
 * Contact      [email protected]
6
 * @copyright   2016 Marko Kungla - https://github.com/mkungla
7
 * @license     The MIT License (MIT)
8
 * 
9
 * @category       AframeVR
10
 * @package        aframe-php
11
 * 
12
 * Lang         PHP (php version >= 7)
13
 * Encoding     UTF-8
14
 * File         Animation.php
15
 * Code format  PSR-2 and 12
16
 * @link        https://github.com/mkungla/aframe-php
17
 * @issues      https://github.com/mkungla/aframe-php/issues
18
 * ********************************************************************
19
 * Contributors:
20
 * @author Marko Kungla <[email protected]>
21
 * ********************************************************************
22
 * Comments:
23
 * @formatter:on */
24
namespace AframeVR\Core;
25
26
use \AframeVR\Interfaces\AnimationInterface;
27
use \DOMElement;
28
29
final class Animation implements AnimationInterface
30
{
31
    /**
32
     * Animation DOM attributes array
33
     * 
34
     * @var array
35
     */
36
    protected $attrs;
37
    
38
    /**
39
     * Constructor
40
     *
41
     * @param string $id            
42
     */
43 5
    public function __construct(string $id = 'untitled')
44
    {
45 5
        $this->attrs['id'] = $id;
46 5
    }
47
48
    /**
49
     * Attribute to animate
50
     *
51
     * Attribute to animate. To specify a component attribute, use componentName.property syntax (e.g.,
52
     * light.intensity).
53
     *
54
     * @param string $attr            
55
     * @return AnimationInterface
56
     */
57 4
    public function attribute(string $attr = 'rotation'): AnimationInterface
58
    {
59 4
        $this->attrs['attribute'] = $attr;
60 4
        return $this;
61
    }
62
63
    /**
64
     * Delay (in milliseconds)
65
     *
66
     * Delay (in milliseconds) or event name to wait on before beginning animation
67
     *
68
     * @param int|string $ms            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $ms a bit more specific; maybe use integer.
Loading history...
69
     * @return AnimationInterface
70
     */
71 4
    public function begin($ms = 0): AnimationInterface
72
    {
73 4
        $this->attrs['begin'] = $ms;
74 4
        return $this;
75
    }
76
77
    /**
78
     * Direction of the animation
79
     *
80
     * Direction of the animation (between from and to). One of alternate, alternateReverse, normal, reverse.
81
     *
82
     * @param string $direction            
83
     * @return AnimationInterface
84
     */
85 1
    public function direction(string $direction = 'normal'): AnimationInterface
86
    {
87 1
        $this->attrs['direction'] = $direction;
88 1
        return $this;
89
    }
90
91
    /**
92
     * Duration in (milliseconds)
93
     *
94
     * Duration in (milliseconds) of the animation.
95
     *
96
     * @param int $ms            
97
     * @return AnimationInterface
98
     */
99 4
    public function dur(int $ms = 1000): AnimationInterface
100
    {
101 4
        $this->attrs['dur'] = $ms;
102 4
        return $this;
103
    }
104
105
    /**
106
     * Easing function
107
     *
108
     * Easing function of the animation. There are very many to choose from.
109
     *
110
     * @param string $func            
111
     * @return AnimationInterface
112
     */
113 4
    public function easing(string $func = 'ease'): AnimationInterface
114
    {
115 4
        $this->attrs['easing'] = $func;
116 4
        return $this;
117
    }
118
119
    /**
120
     * Determines effect of animation when not actively in play
121
     *
122
     * One of backwards, both, forwards, none.
123
     *
124
     * @param string $effect            
125
     * @return AnimationInterface
126
     */
127 4
    public function fill(string $effect = 'forwards'): AnimationInterface
128
    {
129 4
        $this->attrs['fill'] = $effect;
130 4
        return $this;
131
    }
132
133
    /**
134
     * Starting value.
135
     *
136
     * @param string $val            
137
     * @return AnimationInterface
138
     */
139 4
    public function from(string $val = 'Current'): AnimationInterface
140
    {
141 4
        $this->attrs['from'] = $val;
142 4
        return $this;
143
    }
144
145
    /**
146
     * Repeat count or indefinite.
147
     *
148
     * @param int $count            
149
     * @return AnimationInterface
150
     */
151 1
    public function repeat(int $count = 0): AnimationInterface
152
    {
153 1
        $this->attrs['repeat'] = $count;
154 1
        return $this;
155
    }
156
157
    /**
158
     * Ending value.
159
     * Must be specified.
160
     *
161
     * @param string $val            
162
     * @return AnimationInterface
163
     */
164 4
    public function to(string $val = 'true'): AnimationInterface
165
    {
166 4
        $this->attrs['to'] = $val;
167 4
        return $this;
168
    }
169
170
    /**
171
     * Create and add DOM element of the entity
172
     *
173
     * @param \DOMDocument $aframe_dom            
174
     * @return \DOMElement
175
     */
176 2
    public function domElement(\DOMDocument &$aframe_dom): DOMElement
177
    {
178 2
        $a_entity = $aframe_dom->createElement('a-animation');
179 2
        $this->appendAttributes($a_entity);
180 2
        return $a_entity;
181
    }
182
183
    /**
184
     * Append DOM attributes no set by components
185
     *
186
     * @param \DOMElement $a_entity            
187
     */
188 2
    private function appendAttributes(\DOMElement &$a_entity)
189
    {
190 2
        foreach ($this->attrs as $attribute => $val) {
191 2
            $this->setAttribute($a_entity, $attribute, $val);
192
        }
193 2
    }
194
    
195 2 View Code Duplication
    private function setAttribute(&$a_entity, $attribute, $val)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
    {
197 2
        if ($attribute === 'id' && ($val === 'untitled' || is_numeric($val)))
198 2
            return;
199
    
200 2
        $a_entity->setAttribute($attribute, $val);
201 2
    }
202
    
203
}
204