Completed
Branch master (fb685e)
by Marko
02:00
created

ComponentAbstract::getDOMAttr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
    /**
37
     * Array of dom attributes
38
     *
39
     * @var array
40
     */
41
    protected $dom_attributes = array();
42
43
    /**
44
     * Array of component scripts
45
     *
46
     * @var array
47
     */
48
    protected $component_scripts = array();
49
50
    /**
51
     * Name of DOM attribute
52
     *
53
     * @var unknown
54
     */
55
    protected $dom_attribute_name;
56
57
    /**
58
     * Loaded Component Method object
59
     *
60
     * @var object
61
     */
62
    protected $methodProvider;
63
64
    /**
65
     * Component Constructor
66
     */
67 51
    public function __construct()
68
    {
69 51
        $this->initializeComponent();
70 51
        $this->setMethodProvider();
71 51
    }
72
73
    /**
74
     * Call passes all calls to no existing methods to self::methodProvider
75
     *
76
     * @param string $method            
77
     * @param array $args            
78
     * @throws InvalidComponentMethodException
79
     */
80 51
    public function __call(string $method, $args)
81
    {
82 51
        if (is_object($this->methodProvider) && method_exists($this->methodProvider, $method)) {
83 51
            array_unshift($args, 0);
84 51
            $args[0] = &$this->dom_attributes;
85
            
86 51
            return call_user_func_array(array(
87 51
                $this->methodProvider,
88 51
                (string) $method
89
            ), $args);
90
        } else {
91 1
            $class = is_object($this->methodProvider) ? get_class($this->methodProvider) : get_called_class();
92 1
            throw new InvalidComponentMethodException($method, $class);
93
        }
94
    }
95
96
    /**
97
     * Set class providing component methods
98
     *
99
     * @param string $method_provider            
100
     * @return void
101
     */
102 51
    public function setMethodProvider(string $method_provider = 'DefaultMethods')
103
    {
104 51
        $method_provider_class = substr(get_called_class(), 0, strrpos(get_called_class(), '\\')) . '\Methods\\' . $method_provider;
105 51
        $this->methodProvider = new $method_provider_class();
106 51
    }
107
108
    /**
109
     * Does component have DOM Atributes
110
     *
111
     * {@inheritdoc}
112
     *
113
     * @return bool
114
     */
115 5
    public function hasDOMAttributes(): bool
116
    {
117 5
        return ! empty($this->dom_attributes);
118
    }
119
120
    /**
121
     * Get component DOM Atributes array
122
     *
123
     * {@inheritdoc}
124
     *
125
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
126
     */
127 15
    public function getDOMAttributesArray(): array
128
    {
129 15
        return $this->dom_attributes;
130
    }
131
132
    /**
133
     * Get Component scripts
134
     *
135
     * {@inheritdoc}
136
     *
137
     * @return array
138
     */
139 2
    public function getComponentScripts(): array
140
    {
141 2
        return (array) $this->component_scripts;
142
    }
143
144
    /**
145
     * Add component scripts
146
     *
147
     * {@inheritdoc}
148
     *
149
     */
150 1
    public function addComponentScripts(string $vendor_component, string $script_name)
151
    {
152 1
        $this->component_scripts[$vendor_component] = $script_name;
153 1
    }
154
155
    /**
156
     * Get DOMAttr for the entity
157
     *
158
     * @return DOMAttr
159
     */
160 5
    public function getDOMAttr(): DOMAttr
161
    {
162 5
        return new DOMAttr($this->getDomAttributeName(), $this->getDomAttributeString());
163
    }
164
165
    /**
166
     * Get Dom attribute name
167
     *
168
     * @return string
169
     */
170 5
    public function getDomAttributeName(): string
171
    {
172 5
        return $this->dom_attribute_name;
173
    }
174
175
    /**
176
     * Set Dom Attribute name
177
     *
178
     * @return void
179
     */
180 51
    public function setDomAttributeName(string $dom_attribute_name)
181
    {
182 51
        $this->dom_attribute_name = $dom_attribute_name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dom_attribute_name of type string is incompatible with the declared type object<AframeVR\Core\Helpers\unknown> of property $dom_attribute_name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
183 51
    }
184
}
185