TagTrait::br()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
1
<?php /** MicroTagTrait */
2
3
namespace Micro\Web\Html;
4
5
6
/**
7
 * TagTrait trait file.
8
 *
9
 * @author Oleg Lunegov <[email protected]>
10
 * @link https://github.com/linpax/microphp-framework
11
 * @copyright Copyright (c) 2013 Oleg Lunegov
12
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
13
 * @package Micro
14
 * @subpackage Web\Html
15
 * @version 1.0
16
 * @since 1.0
17
 */
18
trait TagTrait
19
{
20
    /**
21
     * Render BR tag
22
     *
23
     * @access public
24
     *
25
     * @param integer $num number of render BR's
26
     * @param array $attributes attributes tag
27
     *
28
     * @return string
29
     * @static
30
     */
31
    public static function br($num = 1, array $attributes = [])
32
    {
33
        $str = '';
34
        for ($i = 0; $i < $num; $i++) {
35
            $str .= static::tag('br', $attributes);
36
        }
37
38
        return $str;
39
    }
40
41
    /**
42
     * Render tag
43
     *
44
     * @access public
45
     *
46
     * @param  string $name tag name
47
     * @param  array $attributes tag attributes
48
     *
49
     * @return string
50
     * @static
51
     */
52 View Code Duplication
    public static function tag($name, array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $result = '';
55
        foreach ($attributes AS $elem => $value) {
56
            $result .= ' '.$elem.'="'.$value.'" ';
57
        }
58
59
        return '<'.$name.$result.'/>';
60
    }
61
62
    /**
63
     * Render mail a tag
64
     *
65
     * @access public
66
     *
67
     * @param  string $name name of e-mail
68
     * @param  string $email e-mail path
69
     * @param  array $attributes attributes tag
70
     *
71
     * @return string
72
     * @static
73
     */
74
    public static function mailto($name, $email, array $attributes = [])
75
    {
76
        return static::openTag('a', array_merge($attributes, ['href' => 'mailto:'.$email])).
77
        $name.
78
        static::closeTag('a');
79
    }
80
81
    /**
82
     * Render open tag
83
     *
84
     * @access public
85
     *
86
     * @param  string $name tag name
87
     * @param  array $attributes tag attributes
88
     *
89
     * @return string
90
     * @static
91
     */
92 View Code Duplication
    public static function openTag($name, array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $result = '';
95
        foreach ($attributes AS $key => $value) {
96
            $result .= ' '.$key.'="'.$value.'"';
97
        }
98
99
        return '<'.$name.$result.'>';
100
    }
101
102
    /**
103
     * Render close tag
104
     *
105
     * @access public
106
     *
107
     * @param  string $name tag name
108
     *
109
     * @return string
110
     * @static
111
     */
112
    public static function closeTag($name)
113
    {
114
        return '</'.$name.'>';
115
    }
116
117
    /**
118
     * Render anchor
119
     *
120
     * @access public
121
     *
122
     * @param string $name name to link
123
     * @param string $url path to link
124
     * @param array $attributes attributes tag
125
     *
126
     * @return string
127
     * @static
128
     */
129
    public static function href($name, $url, array $attributes = [])
130
    {
131
        return static::openTag('a', array_merge($attributes, ['href' => $url])).
132
            $name.
133
            static::closeTag('a');
134
    }
135
136
    /**
137
     * Render H{1-N} tag
138
     *
139
     * @access public
140
     *
141
     * @param  string $num H number
142
     * @param  string $value H value
143
     * @param  array $attributes attributes tag
144
     *
145
     * @return string
146
     * @static
147
     */
148
    public static function heading($num, $value = null, array $attributes = [])
149
    {
150
        return static::openTag('h'.$num, $attributes).$value.static::closeTag('h'.$num);
151
    }
152
153
    /**
154
     * Render image map tag
155
     *
156
     * @access public
157
     *
158
     * @param string $alt Alternative text
159
     * @param string $source Path to image
160
     * @param string $name Map name
161
     * @param array $attributeImg Attributes for image
162
     * @param array $coordinates Coordinates for image
163
     *
164
     * @return string
165
     * @static
166
     */
167
    public static function imageMap($alt, $source, $name, array $attributeImg = [], array $coordinates = [])
168
    {
169
        $areas = '';
170
        foreach ($coordinates AS $coord) {
171
            $areas .= static::tag('area', $coord);
172
        }
173
174
        return static::image($alt, $source, array_merge($attributeImg, ['usemap' => $name])).
175
        static::openTag('map', ['name' => $name, 'id' => $name]).
176
        $areas.
177
        static::closeTag('map');
178
    }
179
180
    /**
181
     * Render image file
182
     *
183
     * @access public
184
     *
185
     * @param  string $name name of image
186
     * @param  string $file path image file
187
     * @param  array $attributes attributes tag
188
     *
189
     * @return string
190
     * @static
191
     */
192
    public static function image($name, $file, array $attributes = [])
193
    {
194
        return static::tag('img', array_merge($attributes, [
195
            'src' => $file,
196
            'alt' => $name
197
        ]));
198
    }
199
200
    /**
201
     * Render object tag
202
     *
203
     * @access public
204
     *
205
     * @param string $source Path to content
206
     * @param array $attributes Attributes for object
207
     * @param array $params Parameters for object
208
     *
209
     * @return string
210
     * @static
211
     */
212
    public static function object($source, array $attributes = [], array $params = [])
213
    {
214
        $attributes['data'] = $source;
215
        $paramsConverted = '';
216
217
        foreach ($params AS $key => $val) {
218
            $paramsConverted .= static::tag('param', ['name' => $key, 'value' => $val]);
219
        }
220
221
        return static::openTag('object', $attributes).$paramsConverted.static::closeTag('object');
222
    }
223
224
    /**
225
     * Embedding objects (video, audio, flash, etc.)
226
     *
227
     * @access public
228
     *
229
     * @param string $source Path to content
230
     * @param array $attributes Attributes for embedding
231
     *
232
     * @return string
233
     */
234
    public static function embed($source, array $attributes = [])
235
    {
236
        $attributes['source'] = $source;
237
238
        return static::openTag('embed', $attributes).static::closeTag('embed');
239
    }
240
241
    /**
242
     * List elements generator
243
     *
244
     * @access public
245
     *
246
     * @param array $items lists multiple array
247
     * @param array $attributes attributes tag
248
     * @param bool $isNumeric Is a numeric list?
249
     *
250
     * @return string
251
     * @static
252
     */
253
    public static function lists(array $items = [], array $attributes = [], $isNumeric = false)
254
    {
255
        $parentTag = $isNumeric ? 'ol' : 'ul';
256
257
        $result = null;
258
        foreach ($items AS $item) {
259
            $result .= static::openTag('li', !empty($item['attr']) ? $item['attr'] : []);
260
            if (!empty($item['parents'])) {
261
                $result .= !empty($item['text']) ? $item['text'] : null;
262
                $result .= static::lists(
263
                    $item['parents'],
264
                    (!empty($item['parentsAttr']) ? $item['parentsAttr'] : []),
265
                    (!empty($item['parentsIsNumeric']) ? true : false)
266
                );
267
            } else {
268
                $result .= $item['text'];
269
            }
270
            $result .= static::closeTag('li');
271
        }
272
273
        return static::openTag($parentTag, $attributes).$result.static::closeTag($parentTag);
274
    }
275
}
276