Completed
Push — master ( 9c1626...6bcea8 )
by Shcherbak
22:09 queued 19:43
created

Html::removeAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
  namespace Fiv\Form\Element;
4
5
  /**
6
   *
7
   * @author  Ivan Shcherbak <[email protected]>
8
   * @package Fiv\Form\Html
9
   */
10
  class Html {
11
12
    /**
13
     * @var string
14
     */
15
    protected $tag = '';
16
17
    /**
18
     * @var null|string
19
     */
20
    protected $content = null;
21
22
    /**
23
     * @var array
24
     */
25
    protected $attributes = [];
26
27
28
    /**
29
     * @return string
30
     */
31 2
    public function __toString() {
32 2
      return $this->render();
33
    }
34
35
36
    /**
37
     * @deprecated
38
     * @param $name
39
     * @param $arguments
40
     * @return $this|null
41
     * @throws \Exception
42
     */
43 1
    public function __call($name, $arguments) {
44 1
      trigger_error('Deprecated', E_USER_DEPRECATED);
45
      if (strpos($name, 'set') === 0 and isset($arguments[0])) {
46
        $name = strtolower(substr($name, 3));
47
        $this->setAttribute($name, $arguments[0]);
48
        return $this;
49
      } elseif (strpos($name, 'get') === 0 and !isset($arguments[0])) {
50
        return $this->getAttribute(strtolower(substr($name, 3)));
51
      } else {
52
        throw new \Exception('Invalid method: ' . $name);
53
      }
54
    }
55
56
57
    /**
58
     * @param string $className
59
     * @return $this
60
     */
61 1
    public function addClass($className) {
62 1
      $currentClass = $this->getAttribute('class');
63
64 1
      if (!empty($currentClass)) {
65 1
        $className = $currentClass . ' ' . $className;
66
      }
67
68 1
      $this->setAttribute('class', $className);
69
70 1
      return $this;
71
    }
72
73
74
    /**
75
     * @param array $attributes
76
     * @return string
77
     */
78 8
    public static function renderAttributes(array $attributes = []) {
79 8
      $attributesInline = '';
80 8
      foreach ($attributes as $name => $value) {
81 7
        $attributesInline .= $name . '="' . addslashes($value) . '" ';
82
      }
83 8
      return $attributesInline;
84
    }
85
86
87
    /**
88
     *
89
     * @param string $tag
90
     * @param array $attributes
91
     * @param bool $content
92
     * @return string
93
     */
94 6
    public static function tag($tag, array $attributes, $content = null) {
95 6
      $html = '<' . $tag . ' ' . static::renderAttributes($attributes);
96
97 6
      if ($content !== null) {
98
        $html .= '>' . $content . '</' . $tag . '>';
99
      } else {
100 6
        $html .= ' />';
101
      }
102
103 6
      return $html;
104
    }
105
106
107
    /**
108
     * @param array $attributes
109
     * @return $this
110
     */
111 4
    public function setAttributes(array $attributes) {
112 4
      foreach ($attributes as $name => $value) {
113 3
        $this->setAttribute($name, $value);
114
      }
115 4
      return $this;
116
    }
117
118
119
    /**
120
     * Add multiple attributes
121
     *
122
     * @param array $attributes
123
     * @return $this
124
     */
125 3
    public function addAttributes(array $attributes) {
126 3
      $this->setAttributes($attributes);
127 3
      return $this;
128
    }
129
130
131
    /**
132
     * @param string $name
133
     * @param string $value
134
     * @return $this
135
     */
136 22
    public function setAttribute($name, $value) {
137 22
      $this->attributes[$name] = $value;
138 22
      return $this;
139
    }
140
141
142
    /**
143
     * @param string $name
144
     * @return $this
145
     */
146 1
    public function removeAttribute($name) {
147 1
      unset($this->attributes[$name]);
148 1
      return $this;
149
    }
150
151
152
    /**
153
     * @param string $name Attribute name
154
     * @return string|null
155
     */
156 19
    public function getAttribute($name) {
157 19
      return !empty($this->attributes[$name]) ? $this->attributes[$name] : null;
158
    }
159
160
161
    /**
162
     * @return string
163
     */
164 4
    public function getAttributesAsString() {
165 4
      return static::renderAttributes($this->attributes);
166
    }
167
168
169
    /**
170
     * @return string
171
     */
172 5
    public function render() {
173 5
      return static::tag($this->tag, $this->attributes, $this->getContent());
0 ignored issues
show
Bug introduced by
It seems like $this->getContent() targeting Fiv\Form\Element\Html::getContent() can also be of type string; however, Fiv\Form\Element\Html::tag() does only seem to accept boolean|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
174
    }
175
176
177
    /**
178
     * @return null|string
179
     */
180 5
    public function getContent() {
181 5
      return $this->content;
182
    }
183
184
  }