1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fracture\Http\Headers; |
4
|
|
|
|
5
|
|
|
class ContentType extends Common |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
protected $headerName = 'Content-Type'; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param string $headerValue |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
8 |
|
protected function extractData($headerValue) |
16
|
|
|
{ |
17
|
8 |
|
$result = []; |
18
|
8 |
|
$parts = preg_split('#;\s?#', $headerValue, -1, \PREG_SPLIT_NO_EMPTY); |
19
|
|
|
|
20
|
8 |
|
if (count($parts) === 0) { |
21
|
1 |
|
return []; |
22
|
|
|
} |
23
|
|
|
|
24
|
7 |
|
$result['value'] = array_shift($parts); |
25
|
|
|
|
26
|
7 |
|
foreach ($parts as $item) { |
27
|
4 |
|
list($key, $value) = explode('=', $item . '='); |
28
|
4 |
|
$result[ $key ] = $value; |
29
|
|
|
} |
30
|
|
|
|
31
|
7 |
|
return $result; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $type |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
3 |
|
public function contains($type) |
40
|
|
|
{ |
41
|
3 |
|
return array_key_exists('value', $this->data) && $this->data['value'] === $type; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
5 |
|
public function match($type) |
46
|
|
|
{ |
47
|
5 |
|
if ($this->contains($type)) { |
48
|
1 |
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
return $this->isCompatible($this->data['value'], $type); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
4 |
|
private function isCompatible($target, $pattern) |
56
|
|
|
{ |
57
|
4 |
|
return $this->replaceStars($target, $pattern) === $this->replaceStars($pattern, $target); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $target |
63
|
|
|
* @param string $pattern |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
4 |
|
private function replaceStars($target, $pattern) |
67
|
|
|
{ |
68
|
4 |
|
$target = explode('/', $target . '/*'); |
69
|
4 |
|
$pattern = explode('/', $pattern . '/*'); |
70
|
|
|
|
71
|
4 |
|
if ($pattern[0] === '*') { |
72
|
1 |
|
$target[0] = '*'; |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
if ($pattern[1] === '*') { |
76
|
3 |
|
$target[1] = '*'; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
return $target[0] . '/' . $target[1]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|