Completed
Push — master ( 610f9f...30d47a )
by Marko
03:48
created

GeometryComponent::translate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 25, 2016 - 7:51:42 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         GeometryComponent.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\Components\Geometry;
25
26
use \AframeVR\Core\Helpers\ComponentAbstract;
27
use \AframeVR\Interfaces\Core\Components\Geometry\GeometryInterface;
28
use \AframeVR\Core\Exceptions\InvalidComponentArgumentException;
29
30
/**
31
 * AframeVR\Core\Components\Geometry
32
 *
33
 * The geometry component provides a basic shape for an entity.
34
 * The general geometry is defined by the primitive property.
35
 * Geometric primitives, in computer graphics, means an extremely
36
 * basic shape. With the primitive defined, additional properties
37
 * are used to further define the geometry. A material component
38
 * is usually defined alongside to provide a appearance
39
 * alongside the shape to create a complete mesh.
40
 */
41
class GeometryComponent extends ComponentAbstract implements GeometryInterface
42
{
43
44
    /**
45
     * Initialize Component
46
     *
47
     * {@inheritdoc}
48
     *
49
     * @return bool
50
     */
51 40
    public function initializeComponent(): bool
52
    {
53 40
        $this->setDomAttributeName('geometry');
54 40
        return true;
55
    }
56
57
    /**
58
     * Return DOM attribute contents
59
     *
60
     * @return string
61
     */
62 3
    public function getDomAttributeString(): string
63
    {
64 3
        $geometry_attrs = $this->getDOMAttributesArray();
65 3
        $format = implode(': %s; ', array_keys($geometry_attrs)) . ': %s;';
66 3
        return vsprintf($format, array_values($geometry_attrs));
67
    }
68
69
    /**
70
     * Set geometry primitive
71
     *
72
     * One of box, circle, cone, cylinder, plane, ring, sphere, torus, torusKnot.
73
     *
74
     * @param string $primitive            
75
     * @throws InvalidComponentArgumentException
76
     * @return void
77
     */
78 38
    public function primitive(string $primitive)
79
    {
80 38
        if (in_array($primitive, self::ALLOWED_PRIMITIVES)) {
81 37
            $this->dom_attributes = array();
82 37
            $method_provider = sprintf('%sMethods', ucfirst($primitive));
83 37
            $this->dom_attributes['primitive'] = $primitive;
84 37
            $this->setMethodProvider($method_provider);
85
        } else {
86 2
            throw new InvalidComponentArgumentException((string) $primitive, 'Geometry::primitive');
87
        }
88 37
    }
89
90
    /**
91
     * translate
92
     *
93
     * {@inheritdoc}
94
     *
95
     * @param int|float $x            
96
     * @param int|float $y            
97
     * @param int|float $z            
98
     * @return void
99
     */
100 7
    public function translate(float $x = 0, float $y = 0, float $z = 0)
101
    {
102 7
        $this->dom_attributes['translate'] = sprintf('%d %d %d', $x, $y, $z);
103 7
    }
104
105
    /**
106
     * Set Buffer
107
     *
108
     * {@inheritdoc}
109
     *
110
     * @param bool $buffer            
111
     * @return void
112
     */
113 1
    public function buffer(bool $buffer = true)
114
    {
115 1
        $this->dom_attributes['buffer'] = $buffer ? 'true' : 'false';
116 1
    }
117
118
    /**
119
     * skipCache
120
     *
121
     * {@inheritdoc}
122
     *
123
     * @param bool $skipCache            
124
     * @return void
125
     */
126 1
    public function skipCache(bool $skipCache = false)
127
    {
128 1
        $this->dom_attributes['skipCache'] = $skipCache ? 'true' : 'false';
129 1
    }
130
}
131