Completed
Push — master ( 5f3088...bfb206 )
by Marko
02:40
created

Entity::defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 20, 2016 - 9:11:10 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         Entity.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\Core\Exceptions\BadComponentCallException;
27
use \AframeVR\Interfaces\{
28
    ComponentInterface,
29
    EntityInterface,
30
    AnimationInterface
31
};
32
use \AframeVR\Core\Animation;
33
use \DOMElement;
34
use \Closure;
35
36
class Entity implements EntityInterface
37
{
38
39
    protected $components = array();
40
41
    protected $animations = array();
42
43 51
    public function __construct()
44
    {
45
        /* Components which All entities inherently have */
46 51
        $this->component('Position');
47 51
        $this->component('Rotation');
48 51
        $this->component('Scale');
49
        
50
        /*
51
         * We initialize common entity components here since
52
         * init and defaults are most cases overwritten by extending class
53
         */
54 51
        $this->position();
55 51
        $this->rotation();
56 51
        $this->scale();
57
        
58
        /* Extending entity components and init */
59 51
        $this->init();
60
        
61
        /* Extending entity defaults */
62 51
        $this->defaults();
63 51
    }
64
65 26
    public function init()
66 26
    {}
67
68 26
    public function defaults()
69 26
    {}
70
71
    /**
72
     * Position component
73
     *
74
     * All entities inherently have the position component.
75
     *
76
     * @param number $x            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $x a bit more specific; maybe use integer.
Loading history...
77
     * @param number $y            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $y a bit more specific; maybe use integer.
Loading history...
78
     * @param number $z            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $z a bit more specific; maybe use integer.
Loading history...
79
     * @return Entity
80
     */
81 51
    public function position($x = 0, $y = 0, $z = 0): EntityInterface
82
    {
83 51
        $this->component('Position')->update($x, $y, $z);
84 51
        return $this;
85
    }
86
87
    /**
88
     * Rotation component
89
     *
90
     * All entities inherently have the rotation component.
91
     *
92
     * @param number $x            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $x a bit more specific; maybe use integer.
Loading history...
93
     * @param number $y            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $y a bit more specific; maybe use integer.
Loading history...
94
     * @param number $z            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $z a bit more specific; maybe use integer.
Loading history...
95
     * @return EntityInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be \AframeVR\Interfaces\EntityInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
     */
97 51
    public function rotation($x = 0, $y = 0, $z = 0): EntityInterface
98
    {
99 51
        $this->component('Rotation')->update($x, $y, $z);
100 51
        return $this;
101
    }
102
103
    /**
104
     * Scale component
105
     *
106
     * All entities inherently have the scale component.
107
     *
108
     * @param number $x            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $x a bit more specific; maybe use integer.
Loading history...
109
     * @param number $y            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $y a bit more specific; maybe use integer.
Loading history...
110
     * @param number $z            
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $z a bit more specific; maybe use integer.
Loading history...
111
     * @return EntityInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be \AframeVR\Interfaces\EntityInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
112
     */
113 51
    public function scale($x = 0, $y = 0, $z = 0): EntityInterface
114
    {
115 51
        $this->component('Scale')->update($x, $y, $z);
116 51
        return $this;
117
    }
118
119
    /**
120
     * Animations
121
     *
122
     * @param string $name            
123
     * @return AnimationInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be \AframeVR\Interfaces\AnimationInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
124
     */
125 1
    public function animation(string $name = 'untitled'): AnimationInterface
126
    {
127 1
        return $this->animations[$name] ?? $this->animations[$name] = new Animation();
128
    }
129
130
    /**
131
     * Load component for this entity
132
     *
133
     * @param string $component_name            
134
     * @throws BadComponentCallException
135
     * @return ComponentInterface|null
136
     */
137 51
    public function component(string $component_name)
138
    {
139 51
        $component_name = strtolower($component_name);
140
        
141 51
        if (! array_key_exists($component_name, $this->components)) {
142 51
            $component = sprintf('\AframeVR\Components\%s', ucfirst($component_name));
143 51
            if (class_exists($component)) {
144 51
                $this->components[$component_name] = new $component();
145
            } else {
146 1
                throw new BadComponentCallException($component_name);
147
            }
148
        }
149
        
150 51
        return $this->components[$component_name] ?? null;
151
    }
152
153
    /**
154
     * Handle entity components
155
     *
156
     * Since we might need to customize these to have
157
     * custom components loaded as $this->methosd aswell therefore
158
     * we have these placeholder magic methods here
159
     *
160
     * @param string $component_name            
161
     * @param array $args            
162
     */
163 11
    public function __call(string $component_name, array $args)
164
    {
165 11
        if (! method_exists($this, $component_name)) {
166 11
            $this->{$component_name} = Closure::bind(function () use ($component_name) {
167 11
                return $this->component($component_name);
168 11
            }, $this, get_class());
169
        }
170
        
171 11
        return call_user_func($this->{$component_name}, $args);
172
    }
173
174
    /**
175
     * Create and add DOM element of the entity
176
     *
177
     * @param unknown $aframe_dom            
178
     * @return DOMElement
179
     */
180 5
    public function DOMElement(&$aframe_dom): DOMElement
181
    {
182
        /* Create entity DOMElement */
183 5
        $a_entity = $aframe_dom->createElement('a-entity', "\n");
184 5
        foreach ($this->components as $component) {
185
            /**
186
             * Remeve component default properties which are not needed
187
             */
188 5
            $component->removeDefaultDOMAttributes();
189
            /*
190
             * Check does component has any attributes to add to DOM element.
191
             * default attributes most of cases are ommited so we might not have any attributes to add
192
             */
193 5
            if ($component->hasDOMAttributes())
194 5
                $a_entity->setAttributeNode($component->getDOMAttributes());
195
        }
196 5
        return $a_entity;
197
    }
198
}
199