Completed
Push — master ( 9b845d...79393d )
by Shcherbak
03:40
created

Html   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 91.49%

Importance

Changes 13
Bugs 8 Features 4
Metric Value
wmc 20
c 13
b 8
f 4
lcom 1
cbo 0
dl 0
loc 175
ccs 43
cts 47
cp 0.9149
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A addClass() 0 11 2
A renderAttributes() 0 7 2
A tag() 0 11 2
A getAttributes() 0 3 1
A setAttributes() 0 6 2
A addAttributes() 0 4 1
A setAttribute() 0 4 1
A removeAttribute() 0 4 1
A getAttribute() 0 3 2
A render() 0 3 1
A getContent() 0 3 1
A getTag() 0 3 1
A setTag() 0 7 2
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;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $className. This often makes code more readable.
Loading history...
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 7
    public function render() {
152 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...
153
    }
154
155
156
    /**
157
     * @return null|string
158
     */
159 7
    public function getContent() {
160 7
      return $this->content;
161
    }
162
163
164
    /**
165
     * @return string
166
     */
167
    public function getTag() {
168
      return $this->tag;
169
    }
170
171
172
    /**
173
     * @param string $tag
174
     * @return $this
175
     */
176 1
    public function setTag($tag) {
177 1
      if (!is_string($tag)) {
178
        throw new \InvalidArgumentException('Expect tag to be a valid string');
179
      }
180 1
      $this->tag = $tag;
181 1
      return $this;
182
    }
183
184
  }