Html::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    public function addClass($className) {
40
      $currentClass = $this->getAttribute('class');
41
42
      if (!empty($currentClass)) {
43
        $className = $currentClass . ' ' . $className;
44
      }
45
46
      $this->setAttribute('class', $className);
47
48
      return $this;
49
    }
50
51
52
    /**
53
     * @param array $attributes
54
     * @return string
55
     */
56
    public static function renderAttributes(array $attributes = []) {
57
      $attributesInline = '';
58
      foreach ($attributes as $name => $value) {
59
        $attributesInline .= $name . '="' . addslashes($value) . '" ';
60
      }
61
      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
    public static function tag($tag, array $attributes = [], $content = null) {
73
      $html = '<' . $tag . ' ' . static::renderAttributes($attributes);
74
75
      if ($content !== null) {
76
        $html .= '>' . $content . '</' . $tag . '>';
77
      } else {
78
        $html .= ' />';
79
      }
80
81
      return $html;
82
    }
83
84
85
    /**
86
     * @return array
87
     */
88
    public function getAttributes() {
89
      return $this->attributes;
90
    }
91
92
93
    /**
94
     * @param array $attributes
95
     * @return $this
96
     */
97
    public function setAttributes(array $attributes) {
98
      foreach ($attributes as $name => $value) {
99
        $this->setAttribute($name, $value);
100
      }
101
      return $this;
102
    }
103
104
105
    /**
106
     * Add multiple attributes
107
     *
108
     * @param array $attributes
109
     * @return $this
110
     */
111
    public function addAttributes(array $attributes) {
112
      $this->setAttributes($attributes);
113
      return $this;
114
    }
115
116
117
    /**
118
     * @param string $name
119
     * @param string $value
120
     * @return $this
121
     */
122
    public function setAttribute($name, $value) {
123
      $this->attributes[$name] = $value;
124
      return $this;
125
    }
126
127
128
    /**
129
     * @param string $name
130
     * @return $this
131
     */
132
    public function removeAttribute($name) {
133
      unset($this->attributes[$name]);
134
      return $this;
135
    }
136
137
138
    /**
139
     * @param string $name Attribute name
140
     * @return string|null
141
     */
142
    public function getAttribute($name) {
143
      return !empty($this->attributes[$name]) ? $this->attributes[$name] : null;
144
    }
145
146
147
    /**
148
     * @return string
149
     */
150
    public function render() {
151
      return static::tag($this->tag, $this->attributes, $this->getContent());
152
    }
153
154
155
    /**
156
     * @return null|string
157
     */
158
    public function getContent() {
159
      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
    public function setTag($tag) {
176
      if (!is_string($tag)) {
177
        throw new \InvalidArgumentException('Expect tag to be a valid string');
178
      }
179
      $this->tag = $tag;
180
      return $this;
181
    }
182
183
  }