|
1
|
|
|
<?php namespace Neomerx\JsonApi\Http\Headers; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 [email protected] (www.neomerx.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use \InvalidArgumentException; |
|
20
|
|
|
use \Neomerx\JsonApi\Contracts\Http\Headers\HeaderInterface; |
|
21
|
|
|
use \Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @package Neomerx\JsonApi |
|
25
|
|
|
*/ |
|
26
|
|
|
class Header implements HeaderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $name; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var MediaTypeInterface[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $mediaTypes; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @param MediaTypeInterface[] $mediaTypes |
|
41
|
|
|
*/ |
|
42
|
53 |
|
public function __construct($name, $mediaTypes) |
|
43
|
|
|
{ |
|
44
|
53 |
|
$name = trim($name); |
|
45
|
53 |
|
if (empty($name) === true) { |
|
46
|
1 |
|
throw new InvalidArgumentException('name'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
52 |
|
if (is_array($mediaTypes) === false) { |
|
50
|
1 |
|
throw new InvalidArgumentException('mediaTypes'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
51 |
|
$this->name = $name; |
|
54
|
51 |
|
$this->mediaTypes = $mediaTypes; |
|
55
|
51 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritdoc |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function getName() |
|
61
|
1 |
|
{ |
|
62
|
2 |
|
return $this->name; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
*/ |
|
68
|
40 |
|
public function getMediaTypes() |
|
69
|
|
|
{ |
|
70
|
40 |
|
return $this->mediaTypes; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Parse header. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $header |
|
77
|
|
|
* @param string $name |
|
78
|
|
|
* |
|
79
|
|
|
* @return HeaderInterface |
|
|
|
|
|
|
80
|
|
|
*/ |
|
81
|
55 |
|
public static function parse($header, $name) |
|
82
|
|
|
{ |
|
83
|
55 |
|
if (is_string($name) === false || empty($name) === true) { |
|
84
|
1 |
|
throw new InvalidArgumentException('header'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
54 |
|
$mediaTypes = []; |
|
88
|
54 |
|
$ranges = preg_split("/,(?=([^\"]*\"[^\"]*\")*[^\"]*$)/", $header); |
|
|
|
|
|
|
89
|
54 |
|
$count = count($ranges); |
|
90
|
54 |
|
for ($idx = 0; $idx < $count; ++$idx) { |
|
91
|
54 |
|
$mediaTypes[] = static::parseMediaType($idx, $ranges[$idx]); |
|
92
|
50 |
|
} |
|
93
|
|
|
|
|
94
|
50 |
|
return static::newInstance($name, $mediaTypes); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param int $position |
|
99
|
|
|
* @param string $mediaType |
|
100
|
|
|
* |
|
101
|
|
|
* @return MediaTypeInterface |
|
|
|
|
|
|
102
|
|
|
*/ |
|
103
|
37 |
|
protected static function parseMediaType($position, $mediaType) |
|
104
|
|
|
{ |
|
105
|
37 |
|
return MediaType::parse($position, $mediaType); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $name |
|
110
|
|
|
* @param MediaTypeInterface[] $mediaTypes |
|
111
|
|
|
* |
|
112
|
|
|
* @return Header |
|
113
|
|
|
*/ |
|
114
|
36 |
|
protected static function newInstance($name, $mediaTypes) |
|
115
|
|
|
{ |
|
116
|
36 |
|
return new static($name, $mediaTypes); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.