UIComponent::removeClass()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Asymptix\ui;
4
5
/**
6
 * Basic high level primitive class of all UI components and controls.
7
 *
8
 * @category Asymptix PHP Framework
9
 * @author Dmytro Zarezenko <[email protected]>
10
 * @copyright (c) 2009 - 2016, Dmytro Zarezenko
11
 *
12
 * @git https://github.com/Asymptix/Framework
13
 * @license http://opensource.org/licenses/MIT
14
 */
15
abstract class UIComponent {
16
    /**
17
     * Path to the components default template file.
18
     */
19
    const DEFAULT_TEMPLATE = "";
20
21
    /**
22
     * Specifies a unique id for an element.
23
     *
24
     * @var string
25
     */
26
    protected $id = "";
27
28
    /**
29
     * Name of the HTML element.
30
     *
31
     * @var string
32
     */
33
    protected $name = "";
34
35
    /**
36
     * Specifies a classname for an element (used to specify a class in a style
37
     * sheet).
38
     *
39
     * @var array<string>
40
     */
41
    protected $class = [];
42
43
    /**
44
     * Specifies a set of CSS style pairs for style attribute of the HTML element.
45
     *
46
     * @var array<string, string>
47
     */
48
    protected $style = [];
49
50
    /**
51
     * Specifies that the element is not relevant. Hidden elements are not
52
     * displayed (empty or 'hidden').
53
     *
54
     * @var string
55
     */
56
    protected $hidden = "";
57
58
    /**
59
     * Specifies extra information about an element.
60
     *
61
     * @var string
62
     */
63
    protected $title = "";
64
65
    /**
66
     * Dataset for the component.
67
     *
68
     * @var array
69
     */
70
    protected $dataSet = [];
71
72
    /**
73
     * Current value for the component.
74
     *
75
     * @var mixed
76
     */
77
    protected $currentValue;
78
79
    /**
80
     * Path to the components template file.
81
     *
82
     * @var string
83
     */
84
    protected $template = null;
85
86
    /**
87
     * Constructor of the class.
88
     *
89
     * @param array<string => string> $attributesList List of the component attributes.
90
     * @param string $template Path to the components template file.
91
     * @param <type> $dataSet
0 ignored issues
show
Documentation introduced by
The doc-type <type> could not be parsed: Unknown type name "<" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
92
     * @param <type> $currentValue
0 ignored issues
show
Documentation introduced by
The doc-type <type> could not be parsed: Unknown type name "<" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
93
     */
94
    protected function __construct($attributesList = [], $template = "", $dataSet = [], $currentValue = null) {
95
        $this->setAttributes($attributesList);
96
        $this->dataSet = $dataSet;
97
        $this->currentValue = $currentValue;
98
        $this->template = $template;
99
    }
100
101
    /**
102
     * Set HTML-element attributes if exists. Throws exception if attribute
103
     * doesn't exist.
104
     *
105
     * @param array<string => mixed> $attributesList List of the attributes.
106
     */
107
    protected function setAttributes($attributesList) {
108
        //var_dump($this);
109
        foreach ($attributesList as $attributeName => $attributeValue) {
110
            if (isset($this->$attributeName)) {
111
                $this->$attributeName = $attributeValue;
112
            } else {
113
                throw new \Exception("Wrong attribute '" . $attributeName . "' for " . get_class($this) . " component");
114
            }
115
        }
116
    }
117
118
    /**
119
     * Generate element HTML-code by template.
120
     *
121
     * @return nothing
122
     */
123
    public function show() {
124
        if (!empty($this->template)) {
125
            include($this->template);
126
127
            return;
128
        }
129
        include(self::DEFAULT_TEMPLATE);
130
    }
131
132
    /**
133
     * css($propertyName) Get the value of a CSS property;
134
     * css($propertyName, "") Unset a CSS property;
135
     * css($propertyName, $propertyValue) Set the value of a CSS property.
136
     *
137
     * @param string $propertyName Name of a CSS property.
138
     * @param mixed $propertyValue Value of a CSS property.
139
     *
140
     * @return Value of the CSS property or NULL if property is not exists.
141
     */
142
    public function css($propertyName, $propertyValue = null) {
143
        if (is_null($propertyValue)) {
144
            if (isset($this->style[$propertyName])) {
145
                return $this->style[$propertyName];
146
            }
147
148
            return null;
149
        } else {
150
            if ($propertyValue === "") {
151
                if (isset($this->style[$propertyName])) {
152
                    unset($this->style[$propertyName]);
153
                }
154
            } else {
155
                $this->style[$propertyName] = $propertyValue;
156
            }
157
        }
158
    }
159
160
    public function getStyle() {
161
        $style = "";
162
        foreach ($this->style as $propertyName => $propertyValue) {
163
            $style .= $propertyName . ":" . $propertyValue . ";";
164
        }
165
166
        return $style;
167
    }
168
169
    public function setStyle($style) {
170
        $this->style = [];
171
        $stylesList = array_filter(explode(";", $style));
172
        foreach ($stylesList as $style) {
173
            $stylePair = explode(":", $style);
174
            if (isset($stylePair[0]) && isset($stylePair[1])) {
175
                $propertyName = trim($stylePair[0]);
176
                $propertyValue = trim($stylePair[1]);
177
                $this->css($propertyName, $propertyValue);
178
            } else {
179
                throw new \Exception("Invalid CSS style string"); //todo: write own exception
180
            }
181
        }
182
    }
183
184
    /**
185
     * Determine whether element is assigned the given class.
186
     *
187
     * @param string $className Name of the class
188
     *
189
     * @return bool
190
     */
191
    public function hasClass($className) {
192
        return (array_search($className, $this->class) !== false);
193
    }
194
195
    public function addClass($className) {
196
        $this->class[] = $className;
197
        $this->class = array_unique($this->class);
198
    }
199
200
    public function removeClass($className = null) {
201
        if (is_null($className)) {
202
            $this->class = [];
203
        } else {
204
            $key = array_search($className, $this->class);
205
            if ($key !== false) {
206
                unset($this->class[$key]);
207
            }
208
        }
209
    }
210
211
    public function getClass() {
212
        return implode(" ", $this->class);
213
    }
214
215
    public function setClass($class) {
216
        $this->class = [];
217
        $class = preg_replace("#( ){2,}#", " ", $class);
218
        $classesList = array_filter(explode(" ", $class));
219
        foreach ($classesList as $className) {
220
            $this->addClass($className);
221
        }
222
    }
223
}
224