Completed
Push — master ( 507e84...e8c35c )
by Marko
03:03
created

Animation::delay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 2
    public function __construct(string $id = 'untitled')
44
    {
45 2
        $this->attrs['id'] = $id;
46 2
    }
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 1
    public function attribute(string $attr = 'rotation'): AnimationInterface
58
    {
59 1
        $this->attrs['attribute'] = $attr;
60 1
        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 string $ms            
69
     * @return AnimationInterface
70
     */
71 1
    public function delay($ms = '0'): AnimationInterface
72
    {
73 1
        $this->attrs['delay'] = $ms;
74 1
        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 1
    public function dur(int $ms = 1000): AnimationInterface
100
    {
101 1
        $this->attrs['dur'] = $ms;
102 1
        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 1
    public function easing(string $func = 'ease'): AnimationInterface
114
    {
115 1
        $this->attrs['easing'] = $func;
116 1
        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 1
    public function fill(string $effect = 'forwards'): AnimationInterface
128
    {
129 1
        $this->attrs['fill'] = $effect;
130 1
        return $this;
131
    }
132
133
    /**
134
     * Starting value.
135
     *
136
     * @param string $val            
137
     * @return AnimationInterface
138
     */
139 1
    public function from(string $val = 'Current'): AnimationInterface
140
    {
141 1
        $this->attrs['from'] = $val;
142 1
        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 1
    public function to(string $val = 'true'): AnimationInterface
165
    {
166 1
        $this->attrs['to'] = $val;
167 1
        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 1
    public function domElement(\DOMDocument &$aframe_dom): DOMElement
177
    {
178 1
        $a_entity = $aframe_dom->createElement('a-animation');
179 1
        $this->appendAttributes($a_entity);
180 1
        return $a_entity;
181
    }
182
183
    /**
184
     * Append DOM attributes no set by components
185
     *
186
     * @param \DOMElement $a_entity            
187
     */
188 1
    private function appendAttributes(\DOMElement &$a_entity)
189
    {
190 1
        foreach ($this->attrs as $attribute => $val) {
191 1
            $this->setAttribute($a_entity, $attribute, $val);
192
        }
193 1
    }
194
    
195 1
    private function setAttribute(&$a_entity, $attribute, $val)
196
    {
197 1
        if ($attribute === 'id' && ($val === 'untitled' || is_numeric($val)))
198 1
            return;
199
    
200 1
        $a_entity->setAttribute($attribute, $val);
201 1
    }
202
    
203
}
204