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