Completed
Pull Request — master (#110)
by Liam
01:14
created

Html   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getAttributeString() 0 18 8
A getAttributeStringForInheritedProperties() 0 16 2
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use Illuminate\Support\Str;
6
7
class Html
8
{
9
    public static function getAttributeString(array $attributes): string
10
    {
11
        $finalAttributes = [];
12
13
        foreach ($attributes as $attribute => $value) {
14
            if (is_string($value) || is_numeric($value)) {
15
                $finalAttributes[] = $attribute.'="'.$value.'"';
16
            } elseif ($value === true) {
17
                $finalAttributes[] = $attribute;
18
            } elseif ($value === false) {
19
                $finalAttributes[] = $attribute.'="false"';
20
            } elseif (is_object($value) && method_exists($value, '__toString')) {
21
                $finalAttributes[] = $attribute.'="'.$value->__toString().'"';
22
            }
23
        }
24
25
        return implode(' ', $finalAttributes);
26
    }
27
28
    public static function getAttributeStringForInheritedProperties(array $attributes): string
29
    {
30
        $attributes = array_filter($attributes, function ($key) {
31
            return strpos($key, 'inherited_') === 0;
32
        }, ARRAY_FILTER_USE_KEY);
33
34
        $finalAttributes = [];
35
36
        foreach ($attributes as $key => $value) {
37
            $key = Str::kebab(Str::after($key, 'inherited_'));
38
39
            $finalAttributes[$key] = $value;
40
        }
41
42
        return static::getAttributeString($finalAttributes);
43
    }
44
}