Completed
Push — master ( f8571a...f0770d )
by Marko
04:14
created

Component::prepareShader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
crap 3
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         PositionComponent.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\Material;
25
26
use \AframeVR\Core\Helpers\ComponentAbstract;
27
use \AframeVR\Interfaces\ComponentInterface;
28
use \AframeVR\Interfaces\Core\Components\Material\MaterialInterface;
29
use \AframeVR\Core\Exceptions\BadShaderCallException;
30
use \AframeVR\Interfaces\ShaderInterface;
31
32
/**
33
 * AframeVR\Core\Components\Material
34
 *
35
 * The material component defines the appearance of an entity.
36
 * The built-in shaders allow us to define properties such as color,
37
 * opacity, or textures. Custom shaders can be registered to extend
38
 * the material component to allow for a wide range of visual effects.
39
 * The geometry component can be defined alongside to provide a shape alongside
40
 * the appearance to create a complete mesh. The material component is coupled to shaders.
41
 * Some of the built-in shading models will provide properties like color or texture to the material component.
42
 */
43
class Component extends ComponentAbstract implements ComponentInterface, MaterialInterface
44
{
45
46
    private $shaderObj;
47
48
    /**
49
     * Initialize Component
50
     *
51
     * {@inheritdoc}
52
     *
53
     * @return bool
54
     */
55 32
    public function initializeComponent(): bool
56
    {
57 32
        $this->setDomAttributeName('material');
58 32
        return true;
59
    }
60
61
    /**
62
     * Return DOM attribute contents
63
     *
64
     * @return string
65
     */
66 2
    public function getDomAttributeString(): string
67
    {
68 2
        $this->prepareShader();
69 2
        $material_attrs = $this->getDOMAttributesArray();
70 2
        $format = implode(': %s; ', array_keys($material_attrs)) . ': %s;';
71 2
        return vsprintf($format, array_values($material_attrs));
72
    }
73
74
    /**
75
     * Material Shader
76
     *
77
     * {@inheritdoc}
78
     *
79
     * @param string $shader            
80
     * @throws BadShaderCallException
81
     * @return object|null
82
     */
83 13
    public function shader(string $shader = 'standard')
84
    {
85 13
        $this->dom_attributes['shader'] = $shader;
86
        
87 13
        if ($this->shaderObj instanceof ShaderInterface)
88 2
            return $this->shaderObj;
89
        
90 13
        $shader = sprintf('\AframeVR\Core\Shaders\%s', ucfirst($shader));
91 13
        if (class_exists($shader)) {
92 12
            $this->shaderObj = new $shader();
93
        } else {
94 1
            throw new BadShaderCallException($shader);
95
        }
96 12
        return $this->shaderObj ?? null;
97
    }
98
99
    /**
100
     * Prepare Shader attributes
101
     *
102
     * @return void
103
     */
104 2
    private function prepareShader()
105
    {
106 2
        if (! empty($this->shaderObj)) {
107 2
            $this->shaderObj->removeDefaultDOMAttributes();
108 2
            foreach ($this->shaderObj as $key => $val)
109 2
                $this->dom_attributes[$key] = $val;
110
        }
111 2
    }
112
}
113