Attributable   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 29
c 1
b 0
f 0
dl 0
loc 65
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C buildHtmlAttributes() 0 45 13
1
<?php
2
namespace Itstructure\GridView\Traits;
3
4
use Itstructure\GridView\Helpers\HtmlHelper;
5
6
/**
7
 * Class Attributable
8
 * @package Itstructure\GridView\Traits
9
 */
10
trait Attributable
11
{
12
    /**
13
     * @var array
14
     */
15
    protected static $dataAttributes = ['data', 'data-ng', 'ng'];
16
17
    /**
18
     * @var array
19
     */
20
    protected $htmlAttributes = [];
21
22
    /**
23
     * @var string
24
     */
25
    protected $charset = 'UTF-8';
26
27
    /**
28
     * @return string
29
     */
30
    public function buildHtmlAttributes()
31
    {
32
        $html = '';
33
34
        foreach ($this->htmlAttributes as $name => $value) {
35
            if (is_bool($value)) {
36
                if ($value) {
37
                    $html .= " $name";
38
                }
39
                continue;
40
            }
41
42
            if (is_array($value)) {
43
                if (in_array($name, static::$dataAttributes)) {
44
                    foreach ($value as $n => $v) {
45
                        if (is_string($v)) {
46
                            $html .= " $name-$n=\"" . HtmlHelper::encode($v, $this->charset) . '"';
47
                        }
48
                    }
49
                    continue;
50
                }
51
52
                if ($name === 'class') {
53
                    if (empty($value)) {
54
                        continue;
55
                    }
56
                    $html .= " $name=\"" . HtmlHelper::encode(implode(' ', $value), $this->charset) . '"';
57
                    continue;
58
                }
59
60
                if ($name === 'style') {
61
                    if (empty($value)) {
62
                        continue;
63
                    }
64
                    $html .= " $name=\"" . HtmlHelper::encode(HtmlHelper::cssStyleFromArray($value), $this->charset) . '"';
65
                }
66
                continue;
67
            }
68
69
            if ($value !== null) {
70
                $html .= " $name=\"" . HtmlHelper::encode($value, $this->charset) . '"';
71
            }
72
        }
73
74
        return $html;
75
    }
76
}
77