Completed
Push — master ( 60d066...9b845d )
by Shcherbak
03:05
created

Html::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * @param string $className
38
     * @return $this
39
     */
40 2
    public function addClass($className) {
41 2
      $currentClass = $this->getAttribute('class');
42
43 2
      if (!empty($currentClass)) {
44 2
        $className = $currentClass . ' ' . $className;
45
      }
46
47 2
      $this->setAttribute('class', $className);
48
49 2
      return $this;
50
    }
51
52
53
    /**
54
     * @param array $attributes
55
     * @return string
56
     */
57 13
    public static function renderAttributes(array $attributes = []) {
58 13
      $attributesInline = '';
59 13
      foreach ($attributes as $name => $value) {
60 12
        $attributesInline .= $name . '="' . addslashes($value) . '" ';
61
      }
62 13
      return $attributesInline;
63
    }
64
65
66
    /**
67
     *
68
     * @param string $tag
69
     * @param array $attributes
70
     * @param bool $content
71
     * @return string
72
     */
73 10
    public static function tag($tag, array $attributes, $content = null) {
74 10
      $html = '<' . $tag . ' ' . static::renderAttributes($attributes);
75
76 10
      if ($content !== null) {
77
        $html .= '>' . $content . '</' . $tag . '>';
78
      } else {
79 10
        $html .= ' />';
80
      }
81
82 10
      return $html;
83
    }
84
85
86
    /**
87
     * @return array
88
     */
89 6
    public function getAttributes() {
90 6
      return $this->attributes;
91
    }
92
93
94
    /**
95
     * @param array $attributes
96
     * @return $this
97
     */
98 5
    public function setAttributes(array $attributes) {
99 5
      foreach ($attributes as $name => $value) {
100 4
        $this->setAttribute($name, $value);
101
      }
102 5
      return $this;
103
    }
104
105
106
    /**
107
     * Add multiple attributes
108
     *
109
     * @param array $attributes
110
     * @return $this
111
     */
112 3
    public function addAttributes(array $attributes) {
113 3
      $this->setAttributes($attributes);
114 3
      return $this;
115
    }
116
117
118
    /**
119
     * @param string $name
120
     * @param string $value
121
     * @return $this
122
     */
123 47
    public function setAttribute($name, $value) {
124 47
      $this->attributes[$name] = $value;
125 47
      return $this;
126
    }
127
128
129
    /**
130
     * @param string $name
131
     * @return $this
132
     */
133 8
    public function removeAttribute($name) {
134 8
      unset($this->attributes[$name]);
135 8
      return $this;
136
    }
137
138
139
    /**
140
     * @param string $name Attribute name
141
     * @return string|null
142
     */
143 40
    public function getAttribute($name) {
144 40
      return !empty($this->attributes[$name]) ? $this->attributes[$name] : null;
145
    }
146
147
148
    /**
149
     * @return string
150
     */
151
    public function getAttributesAsString() {
152
      trigger_error('Deprecated. use Html::renderAttributes', E_USER_DEPRECATED);
153
      return static::renderAttributes($this->attributes);
154
    }
155
156
157
    /**
158
     * @return string
159
     */
160 7
    public function render() {
161 7
      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...
162
    }
163
164
165
    /**
166
     * @return null|string
167
     */
168 7
    public function getContent() {
169 7
      return $this->content;
170
    }
171
172
173
    /**
174
     * @return string
175
     */
176
    public function getTag() {
177
      return $this->tag;
178
    }
179
180
181
    /**
182
     * @param string $tag
183
     * @return $this
184
     */
185 1
    public function setTag($tag) {
186 1
      if (!is_string($tag)) {
187
        throw new \InvalidArgumentException('Expect tag to be a valid string');
188
      }
189 1
      $this->tag = $tag;
190 1
      return $this;
191
    }
192
193
  }