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 |
|
$parts = $this->processArray($parts, $value, $key); |
57
|
1 |
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
if (!is_bool($value)) { |
61
|
1 |
|
$parts[] = $key . "=\"" . $value . "\""; |
62
|
1 |
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
if (true === $value) { |
66
|
1 |
|
$parts[] = $key; |
67
|
1 |
|
continue; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$elements[] = "<" |
72
|
1 |
|
. $link->getHref() |
73
|
1 |
|
. ">" |
74
|
1 |
|
. implode("; ", $parts); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
if (count($elements) > 0) { |
78
|
1 |
|
$result = implode(",", $elements); |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Traverses a value array and add the parts |
86
|
|
|
* |
87
|
|
|
* @param array $parts |
88
|
|
|
* @param array $value |
89
|
|
|
* @param string $key |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
1 |
|
private function processArray(array $parts, array $value, string $key): array |
94
|
|
|
{ |
95
|
1 |
|
foreach ($value as $subValue) { |
96
|
1 |
|
$parts[] = $key . "=\"" . $subValue . "\""; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return $parts; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|