Completed
Push — master ( a16016...b0ba81 )
by Shcherbak
09:10
created

Html::__call()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

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