Completed
Push — master ( ad5651...3fe817 )
by Marko
02:53
created

Entity::domElement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
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 58
    public function __construct()
44
    {
45
        /* Components which All entities inherently have */
46 58
        $this->component('Position');
47 58
        $this->component('Rotation');
48 58
        $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 58
        $this->position();
55 58
        $this->rotation();
56 58
        $this->scale();
57
        
58
        /* Extending entity components and init */
59 58
        $this->init();
60
        
61
        /* Extending entity defaults */
62 58
        $this->defaults();
63 58
    }
64
65 31
    public function init()
66 31
    {}
67
68 31
    public function defaults()
69 31
    {}
70
71
    /**
72
     * Position component
73
     *
74
     * All entities inherently have the position component.
75
     *
76
     * @param int|float $x_axis            
77
     * @param int|float $y_axis            
78
     * @param int|float $z_axis            
79
     * @return \AframeVR\Core\Entity
80
     */
81 58
    public function position(float $x_axis = 0, float $y_axis = 0, float $z_axis = 0): Entity
82
    {
83 58
        $this->component('Position')->positionX($x_axis);
84 58
        $this->component('Position')->positionY($y_axis);
85 58
        $this->component('Position')->positionZ($z_axis);
86 58
        return $this;
87
    }
88
89
    /**
90
     * Rotation component
91
     *
92
     * All entities inherently have the rotation component.
93
     *
94
     * @param int|float $roll            
95
     * @param int|float $pitch            
96
     * @param int|float $yaw            
97
     * @return \AframeVR\Core\Entity
98
     */
99 58
    public function rotation(float $roll = 0, float $pitch = 0, float $yaw = 0): Entity
100
    {
101 58
        $this->component('Rotation')->roll($roll);
102 58
        $this->component('Rotation')->pitch($pitch);
103 58
        $this->component('Rotation')->yaw($yaw);
104 58
        return $this;
105
    }
106
107
    /**
108
     * Scale component
109
     *
110
     * All entities inherently have the scale component.
111
     *
112
     * @param int|float $scale_x            
113
     * @param int|float $scale_y            
114
     * @param int|float $scale_z            
115
     * @return \AframeVR\Core\Entity
116
     */
117 58
    public function scale(float $scale_x = 1, float $scale_y = 1, float $scale_z = 1): Entity
118
    {
119 58
        $this->component('Scale')->scaleX($scale_x);
120 58
        $this->component('Scale')->scaleY($scale_y);
121 58
        $this->component('Scale')->scaleZ($scale_z);
122 58
        return $this;
123
    }
124
125
    /**
126
     * Animations
127
     *
128
     * @param string $name            
129
     * @return \AframeVR\Interfaces\AnimationInterface
130
     */
131 1
    public function animation(string $name = 'untitled'): AnimationInterface
132
    {
133 1
        return $this->animations[$name] ?? $this->animations[$name] = new Animation();
134
    }
135
136
    /**
137
     * Load component for this entity
138
     *
139
     * @param string $component_name            
140
     * @throws \AframeVR\Core\Exceptions\BadComponentCallException
141
     * @return object|null
142
     */
143 58
    public function component(string $component_name)
144
    {
145 58
        $component_name = strtolower($component_name);
146
        
147 58
        if (! array_key_exists($component_name, $this->components)) {
148 58
            $component = sprintf('\AframeVR\Core\Components\%s\%sComponent', ucfirst($component_name), ucfirst($component_name));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
149 58
            if (class_exists($component)) {
150 58
                $this->components[$component_name] = new $component();
151
            } else {
152 1
                throw new BadComponentCallException($component_name);
153
            }
154
        }
155
        
156 58
        return $this->components[$component_name] ?? null;
157
    }
158
159
    /**
160
     * Handle entity components
161
     *
162
     * Since we might need to customize these to have
163
     * custom components loaded as $this->methosd aswell therefore
164
     * we have these placeholder magic methods here
165
     *
166
     * @param string $component_name            
167
     * @param array $args            
168
     */
169 12
    public function __call(string $component_name, array $args)
170
    {
171 12
        if (! method_exists($this, $component_name)) {
172 12
            $this->{$component_name} = Closure::bind(function () use ($component_name) {
173 12
                return $this->component($component_name);
174 12
            }, $this, get_class());
175
        }
176
        
177 12
        return call_user_func($this->{$component_name}, $args);
178
    }
179
180
    /**
181
     * Create and add DOM element of the entity
182
     *
183
     * @param \DOMDocument $aframe_dom            
184
     * @return \DOMElement
185
     */
186 2
    public function domElement(&$aframe_dom): DOMElement
187
    {
188 2
        $a_entity = $aframe_dom->createElement('a-entity');
189 2
        foreach ($this->components as $component) {
190
            /*
191
             * Check does component has any attributes to add to DOM element.
192
             * default attributes most of cases are ommited so we might not have any attributes to add
193
             */
194 2
            if ($component->hasDOMAttributes())
195 2
                $a_entity->setAttributeNode($component->getDOMAttr());
196
        }
197 2
        return $a_entity;
198
    }
199
}
200