Passed
Push — master ( 3a35da...a738f6 )
by Nikolaos
02:35
created

Header   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 61
ccs 27
cts 28
cp 0.9643
rs 10
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B serialize() 0 52 9
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Html\Link\Serializer;
15
16
use function implode;
17
use function is_array;
18
use function is_bool;
19
20
/**
21
 * Class Phalcon\Http\Link\Serializer\Header
22
 */
23
class Header implements SerializerInterface
24
{
25
    /**
26
     * Serializes all the passed links to a HTTP link header
27
     *
28
     * @param array $links
29
     *
30
     * @return string|null
31
     */
32 2
    public function serialize(array $links): ?string
33
    {
34 2
        $elements = [];
35 2
        $result   = null;
36 2
        foreach ($links as $link) {
37
            /**
38
             * Leave templated links alone
39
             */
40 1
            if ($link->isTemplated()) {
41
                continue;
42
            }
43
44
            /**
45
             * Split the parts of the attributes so that we can parse them
46
             */
47 1
            $attributes = $link->getAttributes();
48 1
            $rels       = $link->getRels();
49
            $parts      = [
50 1
                "",
51 1
                "rel=\"" . implode(" ", $rels) . "\""
52
            ];
53
54 1
            foreach ($attributes as $key => $value) {
55 1
                if (is_array($value)) {
56 1
                    foreach ($value as $subValue) {
57 1
                        $parts[] = $key . "=\"" . $subValue . "\"";
58
                    }
59 1
                    continue;
60
                }
61
62 1
                if (!is_bool($value)) {
63 1
                    $parts[] = $key . "=\"" . $value . "\"";
64 1
                    continue;
65
                }
66
67 1
                if (true === $value) {
68 1
                    $parts[] = $key;
69 1
                    continue;
70
                }
71
            }
72
73 1
            $elements[] = "<"
74 1
                . $link->getHref()
75 1
                . ">"
76 1
                . implode("; ", $parts);
77
        }
78
79 2
        if (count($elements) > 0) {
80 1
            $result = implode(",", $elements);
81
        }
82
83 2
        return $result;
84
    }
85
}
86