ComponentAbstract::__call()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 13
nc 3
nop 2
crap 4
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 25, 2016 - 7:55:29 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         ComponentAbstract.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\Helpers;
25
26
use \AframeVR\Interfaces\ComponentInterface;
27
use \AframeVR\Core\Exceptions\{
28
    InvalidComponentMethodException,
29
    InvalidComponentArgumentException
30
};
31
use \DOMAttr;
32
33
abstract class ComponentAbstract implements ComponentInterface
34
{
35
    /**
36
     * Array of dom attributes
37
     *
38
     * @var array
39
     */
40
    protected $dom_attributes = array();
41
    /**
42
     * Array of component scripts
43
     *
44
     * @var array
45
     */
46
    protected $component_scripts = array();
47
    /**
48
     * Name of DOM attribute
49
     *
50
     * @var string
51
     */
52
    protected $dom_attribute_name;
53
    /**
54
     * Loaded Component Method object
55
     *
56
     * @var object
57
     */
58
    protected $methodProvider;
59
60
    /**
61
     * Component Constructor
62
     */
63 84
    public function __construct()
64
    {
65 84
        $this->initializeComponent();
66 84
    }
67
68
    /**
69
     * Call passes all calls to no existing methods to self::methodProvider
70
     *
71
     * @param string $method            
72
     * @param array $args            
73
     * @throws InvalidComponentMethodException
74
     */
75 30
    public function __call(string $method, $args)
76
    {
77 30
        if (is_object($this->methodProvider) && method_exists($this->methodProvider, $method)) {
78 29
            array_unshift($args, 0);
79 29
            $args[0] = &$this->dom_attributes;
80 29
            call_user_func_array(
81
                array(
82 29
                    $this->methodProvider,
83 29
                    (string) $method
84
                ), $args);
85 29
            return $this;
86
        } else {
87 1
            $class = is_object($this->methodProvider) ? get_class($this->methodProvider) : get_called_class();
88 1
            throw new InvalidComponentMethodException($method, $class);
89
        }
90
    }
91
92
    /**
93
     * Return DOM attribute contents
94
     *
95
     * @return string
96
     */
97 20
    public function getDomAttributeString(): string
98
    {
99 20
        $attrs       = $this->getDOMAttributesArray();
100 20
        $attr_format = implode(': %s; ', array_keys($attrs)) . ': %s;';
101 20
        return vsprintf($attr_format, array_values($attrs));
102
    }
103
104
    /**
105
     * Set class providing component methods
106
     *
107
     * @param string $mp            
108
     * @return void
109
     */
110 35
    public function setMethodProvider(string $mp = 'DefaultMethods')
111
    {
112 35
        $mp_class = substr(get_called_class(), 0, strrpos(get_called_class(), '\\')) . '\Methods\\' . $mp;
113
        
114 35
        $this->methodProvider = class_exists($mp_class) ? new $mp_class() : null;
115 35
    }
116
117
    /**
118
     * Does component have DOM Atributes
119
     *
120
     * {@inheritdoc}
121
     *
122
     * @return bool
123
     */
124 16
    public function hasDOMAttributes(): bool
125
    {
126 16
        return ! empty($this->dom_attributes);
127
    }
128
129
    /**
130
     * Get component DOM Atributes array
131
     *
132
     * {@inheritdoc}
133
     *
134
     * @return array
135
     */
136 43
    public function getDOMAttributesArray(): array
137
    {
138 43
        return $this->dom_attributes;
139
    }
140
141
    /**
142
     * Get Component scripts
143
     *
144
     * {@inheritdoc}
145
     *
146
     * @return array
147
     */
148 2
    public function getComponentScripts(): array
149
    {
150 2
        return (array) $this->component_scripts;
151
    }
152
153
    /**
154
     * Add component scripts
155
     *
156
     * {@inheritdoc}
157
     *
158
     */
159 1
    public function addComponentScripts(string $vendor_component, string $script_name)
160
    {
161 1
        $this->component_scripts[$vendor_component] = $script_name;
162 1
    }
163
164
    /**
165
     * Get DOMAttr for the entity
166
     *
167
     * @return DOMAttr
168
     */
169 15
    public function getDOMAttr(): DOMAttr
170
    {
171 15
        return new DOMAttr($this->getDomAttributeName(), $this->getDomAttributeString());
172
    }
173
174
    /**
175
     * Get Dom attribute name
176
     *
177
     * @return string
178
     */
179 15
    public function getDomAttributeName(): string
180
    {
181 15
        return $this->dom_attribute_name;
182
    }
183
184
    /**
185
     * Set Dom Attribute name
186
     *
187
     * @param string $dom_attribute_name            
188
     * @return void
189
     */
190 84
    public function setDomAttribute(string $dom_attribute_name)
191
    {
192 84
        $this->dom_attribute_name = $dom_attribute_name;
193 84
    }
194
}
195