Passed
Pull Request — main (#3)
by Peter
05:56 queued 03:02
created

ArrayHelper::toAttributes()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
nc 8
nop 2
dl 0
loc 19
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Html\Helper;
6
7
class ArrayHelper
8
{
9
    protected const ERROR_INVALID_ATTRIBUTES = 'invalid attributes (array of string[] and null items)';
10
11
    /**
12
     * @param array<string,string> $styles
13
     *
14
     * @return string
15
     */
16
    public static function toStyles(array $styles): string
17
    {
18
        $tmp = [];
19
        foreach ($styles as $k => $v) {
20
            $tmp[] = sprintf('%s: %s', $k, $v);
21
        }
22
23
        return implode('; ', $tmp);
24
    }
25
26
    /**
27
     * @param array<string,string> $parts
28
     *
29
     * @return string
30
     */
31
    public static function toQuery(array $parts): string
32
    {
33
        if (empty($parts)) {
34
            return '';
35
        }
36
37
        $tmp = [];
38
        foreach ($parts as $k => $v) {
39
            $tmp[] = sprintf('%s=%s', $k, urlencode($v));
40
        }
41
42
        return '?' . implode('&', $tmp);
43
    }
44
}
45