Completed
Push — master ( 3fd55a...36f978 )
by Shcherbak
02:38
created

Html::render()   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 0
Metric Value
c 0
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
   */
9
  class Html {
10
11
    /**
12
     * @var string
13
     */
14
    protected $tag = '';
15
16
    /**
17
     * @var null|string
18
     */
19
    protected $content = null;
20
21
    /**
22
     * @var array
23
     */
24
    protected $attributes = [];
25
26
27
    /**
28
     * @return string
29
     */
30
    public function __toString() {
31
      return $this->render();
32
    }
33
34
35
    /**
36
     * @param string $className
37
     * @return $this
38
     */
39 2
    public function addClass($className) {
40 2
      $currentClass = $this->getAttribute('class');
41
42 2
      if (!empty($currentClass)) {
43 2
        $className = $currentClass . ' ' . $className;
44
      }
45
46 2
      $this->setAttribute('class', $className);
47
48 2
      return $this;
49
    }
50
51
52
    /**
53
     * @param array $attributes
54
     * @return string
55
     */
56 13
    public static function renderAttributes(array $attributes = []) {
57 13
      $attributesInline = '';
58 13
      foreach ($attributes as $name => $value) {
59 13
        $attributesInline .= $name . '="' . addslashes($value) . '" ';
60
      }
61 13
      return $attributesInline;
62
    }
63
64
65
    /**
66
     *
67
     * @param string $tag
68
     * @param array $attributes
69
     * @param string|null $content
70
     * @return string
71
     */
72 11
    public static function tag($tag, array $attributes = [], $content = null) {
73 11
      $html = '<' . $tag . ' ' . static::renderAttributes($attributes);
74
75 11
      if ($content !== null) {
76 2
        $html .= '>' . $content . '</' . $tag . '>';
77
      } else {
78 10
        $html .= ' />';
79
      }
80
81 11
      return $html;
82
    }
83
84
85
    /**
86
     * @return array
87
     */
88 6
    public function getAttributes() {
89 6
      return $this->attributes;
90
    }
91
92
93
    /**
94
     * @param array $attributes
95
     * @return $this
96
     */
97 7
    public function setAttributes(array $attributes) {
98 7
      foreach ($attributes as $name => $value) {
99 7
        $this->setAttribute($name, $value);
100
      }
101 7
      return $this;
102
    }
103
104
105
    /**
106
     * Add multiple attributes
107
     *
108
     * @param array $attributes
109
     * @return $this
110
     */
111 6
    public function addAttributes(array $attributes) {
112 6
      $this->setAttributes($attributes);
113 6
      return $this;
114
    }
115
116
117
    /**
118
     * @param string $name
119
     * @param string $value
120
     * @return $this
121
     */
122 45
    public function setAttribute($name, $value) {
123 45
      $this->attributes[$name] = $value;
124 45
      return $this;
125
    }
126
127
128
    /**
129
     * @param string $name
130
     * @return $this
131
     */
132 2
    public function removeAttribute($name) {
133 2
      unset($this->attributes[$name]);
134 2
      return $this;
135
    }
136
137
138
    /**
139
     * @param string $name Attribute name
140
     * @return string|null
141
     */
142 36
    public function getAttribute($name) {
143 36
      return !empty($this->attributes[$name]) ? $this->attributes[$name] : null;
144
    }
145
146
147
    /**
148
     * @return string
149
     */
150 4
    public function render() {
151 4
      return static::tag($this->tag, $this->attributes, $this->getContent());
152
    }
153
154
155
    /**
156
     * @return null|string
157
     */
158 4
    public function getContent() {
159 4
      return $this->content;
160
    }
161
162
163
    /**
164
     * @return string
165
     */
166
    public function getTag() {
167
      return $this->tag;
168
    }
169
170
171
    /**
172
     * @param string $tag
173
     * @return $this
174
     */
175 1
    public function setTag($tag) {
176 1
      if (!is_string($tag)) {
177
        throw new \InvalidArgumentException('Expect tag to be a valid string');
178
      }
179 1
      $this->tag = $tag;
180 1
      return $this;
181
    }
182
183
  }