Completed
Push — 0.3.x ( e294c0...140210 )
by Marko
04:05
created

Animation::repeat()   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
    /**
33
     * Constructor
34
     *
35
     * @param string $id            
36
     */
37 5
    public function __construct(string $id = 'untitled')
38
    {
39 5
        $this->attrs['id'] = $id;
0 ignored issues
show
Bug introduced by
The property attrs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40 5
    }
41
42
    /**
43
     * Attribute to animate
44
     *
45
     * Attribute to animate. To specify a component attribute, use componentName.property syntax (e.g.,
46
     * light.intensity).
47
     *
48
     * @param string $attr            
49
     * @return AnimationInterface
50
     */
51 4
    public function attribute(string $attr = 'rotation'): AnimationInterface
52
    {
53 4
        $this->attrs['attribute'] = $attr;
54 4
        return $this;
55
    }
56
57
    /**
58
     * Delay (in milliseconds)
59
     *
60
     * Delay (in milliseconds) or event name to wait on before beginning animation
61
     *
62
     * @param mixed $ms            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $ms a bit more specific; maybe use integer.
Loading history...
63
     * @return AnimationInterface
64
     */
65 4
    public function begin($ms = 0): AnimationInterface
66
    {
67 4
        $this->attrs['begin'] = $ms;
68 4
        return $this;
69
    }
70
71
    /**
72
     * Direction of the animation
73
     *
74
     * Direction of the animation (between from and to). One of alternate, alternateReverse, normal, reverse.
75
     *
76
     * @param string $direction            
77
     * @return AnimationInterface
78
     */
79 1
    public function direction(string $direction = 'normal'): AnimationInterface
80
    {
81 1
        $this->attrs['direction'] = $direction;
82 1
        return $this;
83
    }
84
85
    /**
86
     * Duration in (milliseconds)
87
     *
88
     * Duration in (milliseconds) of the animation.
89
     *
90
     * @param int $ms            
91
     * @return AnimationInterface
92
     */
93 4
    public function dur(int $ms = 1000): AnimationInterface
94
    {
95 4
        $this->attrs['dur'] = $ms;
96 4
        return $this;
97
    }
98
99
    /**
100
     * Easing function
101
     *
102
     * Easing function of the animation. There are very many to choose from.
103
     *
104
     * @param string $func            
105
     * @return AnimationInterface
106
     */
107 4
    public function easing(string $func = 'ease'): AnimationInterface
108
    {
109 4
        $this->attrs['easing'] = $func;
110 4
        return $this;
111
    }
112
113
    /**
114
     * Determines effect of animation when not actively in play
115
     *
116
     * One of backwards, both, forwards, none.
117
     *
118
     * @param string $effect            
119
     * @return AnimationInterface
120
     */
121 4
    public function fill(string $effect = 'forwards'): AnimationInterface
122
    {
123 4
        $this->attrs['fill'] = $effect;
124 4
        return $this;
125
    }
126
127
    /**
128
     * Starting value.
129
     *
130
     * @param string $val            
131
     * @return AnimationInterface
132
     */
133 4
    public function from(string $val = 'Current'): AnimationInterface
134
    {
135 4
        $this->attrs['from'] = $val;
136 4
        return $this;
137
    }
138
139
    /**
140
     * Repeat count or indefinite.
141
     *
142
     * @param int $count            
143
     * @return AnimationInterface
144
     */
145 1
    public function repeat(int $count = 0): AnimationInterface
146
    {
147 1
        $this->attrs['repeat'] = $count;
148 1
        return $this;
149
    }
150
151
    /**
152
     * Ending value.
153
     * Must be specified.
154
     *
155
     * @param string $val            
156
     * @return AnimationInterface
157
     */
158 4
    public function to(string $val = 'true'): AnimationInterface
159
    {
160 4
        $this->attrs['to'] = $val;
161 4
        return $this;
162
    }
163
164
    /**
165
     * Create and add DOM element of the entity
166
     *
167
     * @param \DOMDocument $aframe_dom            
168
     * @return \DOMElement
169
     */
170 2
    public function domElement(\DOMDocument &$aframe_dom): DOMElement
171
    {
172 2
        $a_entity = $aframe_dom->createElement('a-animation');
173 2
        $this->appendAttributes($a_entity);
174 2
        return $a_entity;
175
    }
176
177
    /**
178
     * Append DOM attributes no set by components
179
     *
180
     * @param \DOMElement $a_entity            
181
     */
182 2 View Code Duplication
    private function appendAttributes(\DOMElement &$a_entity)
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...
183
    {
184 2
        foreach ($this->attrs as $attr => $val) {
185 2
            if ($attr === 'id' && ($val === 'untitled' || is_numeric($val)))
186 2
                continue;
187 2
            $a_entity->setAttribute($attr, $val);
188
        }
189 2
    }
190
}
191