|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Represents a Link header. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\RestBundle\HttpFoundation; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Represents a Link header. |
|
12
|
|
|
* |
|
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
14
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
15
|
|
|
* @link http://swisscom.ch |
|
16
|
|
|
*/ |
|
17
|
|
|
class LinkHeader |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var LinkHeaderItem[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private $items = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor |
|
26
|
|
|
* |
|
27
|
|
|
* @param LinkHeaderItem[] $items link header items |
|
28
|
|
|
*/ |
|
29
|
2 |
|
public function __construct(array $items) |
|
30
|
|
|
{ |
|
31
|
2 |
|
$this->items = $items; |
|
32
|
2 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Builds a LinkHeader instance from a string. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $headerValue value of complete header |
|
38
|
|
|
* |
|
39
|
|
|
* @return LinkHeader |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public static function fromString($headerValue) |
|
42
|
|
|
{ |
|
43
|
2 |
|
return new self( |
|
44
|
2 |
|
array_map( |
|
45
|
2 |
|
function ($itemValue) use (&$index) { |
|
46
|
|
|
$item = LinkHeaderItem::fromString(trim($itemValue)); |
|
47
|
|
|
|
|
48
|
|
|
return $item; |
|
49
|
2 |
|
}, |
|
50
|
2 |
|
preg_split('/(".+?"|[^,]+)(?:,|$)/', $headerValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) |
|
51
|
2 |
|
) |
|
52
|
2 |
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* get LinkHeader instance from response |
|
57
|
|
|
* |
|
58
|
|
|
* @param \Symfony\Component\HttpFoundation\Response $response response to get header from |
|
59
|
|
|
* |
|
60
|
|
|
* @return LinkHeader |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public static function fromResponse(Response $response) |
|
63
|
|
|
{ |
|
64
|
2 |
|
$header = $response->headers->get('Link'); |
|
65
|
2 |
|
if (is_array($header)) { |
|
66
|
|
|
implode(',', $header); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return self::fromString($header); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* get all items |
|
74
|
|
|
* |
|
75
|
|
|
* @return LinkHeaderItem[] |
|
76
|
|
|
*/ |
|
77
|
|
|
public function all() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->items; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* add a LinkHeaderItem. |
|
84
|
|
|
* |
|
85
|
|
|
* @param LinkHeaderItem $item item to add |
|
86
|
|
|
* |
|
87
|
|
|
* @return LinkHeader |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function add(LinkHeaderItem $item) |
|
90
|
|
|
{ |
|
91
|
2 |
|
$this->items[] = $item; |
|
92
|
|
|
|
|
93
|
2 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Cast contents to string. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function __toString() |
|
102
|
|
|
{ |
|
103
|
2 |
|
return implode(',', $this->items); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.