Element::removeCssClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace hemio\html\Abstract_;
4
5
abstract class Element implements \hemio\html\Interface_\HtmlCode, \hemio\html\Interface_\MaintainsAppendages,
6
    \hemio\html\Interface_\ElementTag
7
{
8
9
    use \hemio\html\Trait_\AppendageMaintainance,
10
        \hemio\html\Trait_\HooksToString;
11
    /**
12
     * parent element in DOM or something similar
13
     * @var \hemio\html\Interface_\MaintainsChilds
14
     */
15
    public $objParent = null;
16
17
    /**
18
     * atributes like 'type' => 'text' in <input type="text" />
19
     * @var array
20
     */
21
    protected $arrAttributes = array();
22
23
    /**
24
     * activated CSS classes like 'my_css_class' => true
25
     * @var array
26
     */
27
    protected $arrCssClasses = array();
28
29
    /**
30
     * concrete CSS options like 'color'=>'green' (please prefer CSS classes!)
31
     * @var array
32
     */
33
    protected $arrCssPropertys = array();
34
35
    /**
36
     * sets an antribute
37
     * @param string $strKey
38
     * @param mixed $mixValue
39
     */
40
    public function setAttribute($strKey, $mixValue)
41
    {
42
        if ($mixValue === true)
43
            $mixValue = $strKey;
44
        else if ($mixValue === false)
45
            $mixValue = '';
46
47
        $this->arrAttributes[$strKey] = $mixValue;
48
    }
49
50
    /**
51
     *
52
     * @param string $strKey
53
     */
54
    public function getAttribute($strKey)
55
    {
56
        return array_key_exists($strKey, $this->arrAttributes) ? $this
57
            ->arrAttributes[$strKey] : '';
58
    }
59
60
    /**
61
     * @param string $strClassName
62
     */
63
    public function addCssClass($strClassName)
64
    {
65
        if ($strClassName != '') {
66
            $this->arrCssClasses[$strClassName] = true;
67
        }
68
    }
69
70
    /**
71
     *
72
     * @param string $strClassName
73
     */
74
    public function removeCssClass($strClassName)
75
    {
76
        if (isset($this->arrCssClasses[$strClassName])) {
77
            unset($this->arrCssClasses[$strClassName]);
78
        }
79
    }
80
81
    /**
82
     * @param string $strKey
83
     * @param mixed $mixValue
84
     */
85
    public function setCssProperty($strKey, $mixValue)
86
    {
87
        $this->arrCssPropertys[$strKey] = $mixValue;
88
    }
89
90
    /**
91
     * @param string $strId
92
     */
93
    public function setId($strId)
94
    {
95
        $this->setAttribute('id', $strId);
96
    }
97
98
    /**
99
     * writes the style collected css styles and classes into the style atribute
100
     */
101
    protected function generateStyleAttribute()
102
    {
103
        // combine CSS classes
104
        $strClasses = '';
105
        foreach ($this->arrCssClasses as $strClass => $NULL) {
106
            if ($strClass != '')
107
                $strClass .= ' ';
108
            $strClasses .= $strClass;
109
        }
110
        $this->setAttribute('class', $strClasses);
111
112
        // combine CSS propertys
113
        $strPropertys = '';
114
        foreach ($this->arrCssPropertys as $strProperty => $strValue) {
115
            if ($strPropertys != '')
116
                $strPropertys .= ' ';
117
            $strPropertys .= $strProperty.': '.$strValue.';';
118
        }
119
        $this->setAttribute('style', $strPropertys);
120
    }
121
122
    /**
123
     * Returns what deemed to be the final atributes string
124
     * @return string
125
     */
126
    protected function getFormattedElementAttributes()
127
    {
128
        $strOptions = '';
129
        $this->generateStyleAttribute();
130
131
        foreach ($this->arrAttributes as $strKey => $strVal)
132
            if ($strVal !== '')
133
                $strOptions .= sprintf(
134
                    ' %s="%s"',
135
                    htmlspecialchars($strKey, ENT_QUOTES | ENT_XHTML),
136
                                     htmlspecialchars($strVal,
137
                                                      ENT_QUOTES | ENT_XHTML)
138
                );
139
140
        return $strOptions;
141
    }
142
143
    /**
144
     * Sets the parent HTML element.
145
     * Will be called while iterrating through __toString()
146
     * @param \hemio\html\Interface_\MaintainsChilds $objParent
147
     */
148
    public function setParent(\hemio\html\Interface_\MaintainsChilds $objParent)
149
    {
150
        $this->objParent = $objParent;
151
    }
152
153
    public function describe()
154
    {
155
        return $this->tagName();
156
    }
157
}
158