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

Html::getAttributeString()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.4444
c 0
b 0
f 0
cc 8
nc 6
nop 1
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
}