Completed
Push — 0.3.x ( 8ea901...715f96 )
by Marko
04:14
created

ComponentAbstract::__call()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 12
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 64
    public function __construct()
64
    {
65 64
        $this->initializeComponent();
66 64
        $this->setMethodProvider();
67 64
    }
68
69
    /**
70
     * Call passes all calls to no existing methods to self::methodProvider
71
     * 
72
     * @param string $method
73
     * @param array $args
74
     * @throws InvalidComponentMethodException
75
     */
76 52
    public function __call( string $method, $args)
77
    {
78 52
        if (is_object($this->methodProvider) && method_exists($this->methodProvider, $method)) {
79 52
            array_unshift($args, 0);
80 52
            $args[0] = &$this->dom_attributes;
81 52
            return call_user_func_array(
82
                array(
83 52
                    $this->methodProvider,
84 52
                    (string) $method
85
                ), $args);
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 6
    public function getDomAttributeString(): string
98
    {
99 6
        $attrs       = $this->getDOMAttributesArray();
100 6
        $attr_format = implode(': %s; ', array_keys($attrs)) . ': %s;';
101 6
        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 64
    public function setMethodProvider( string $mp = 'DefaultMethods')
111
    {
112 64
        $mp_class = substr(get_called_class(), 0, strrpos(get_called_class(), '\\')) . '\Methods\\' . $mp;
113
        
114 64
        $this->methodProvider = class_exists($mp_class) ? new $mp_class() : null;
115 64
    }
116
117
    /**
118
     * Does component have DOM Atributes
119
     * 
120
     * {@inheritdoc}
121
     *
122
     * @return bool
123
     */
124 4
    public function hasDOMAttributes(): bool
125
    {
126 4
        return ! empty($this->dom_attributes);
127
    }
128
129
    /**
130
     * Get component DOM Atributes array
131
     * 
132
     * {@inheritdoc}
133
     *
134
     * @return array
135
     */
136 22
    public function getDOMAttributesArray(): array
137
    {
138 22
        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 4
    public function getDOMAttr(): DOMAttr
170
    {
171 4
        return new DOMAttr($this->getDomAttributeName(), $this->getDomAttributeString());
172
    }
173
174
    /**
175
     * Get Dom attribute name
176
     * 
177
     * @return string
178
     */
179 4
    public function getDomAttributeName(): string
180
    {
181 4
        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 64
    public function setDomAttribute( string $dom_attribute_name)
191
    {
192 64
        $this->dom_attribute_name = $dom_attribute_name;
193 64
    }
194
}
195