1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny\Controller; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Controller methods to negotiate content |
7
|
|
|
*/ |
8
|
|
|
trait ContentNegotiation |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Get request, set for controller |
12
|
|
|
* |
13
|
|
|
* @return ServerRequestInterface |
14
|
|
|
*/ |
15
|
|
|
abstract public function getRequest(); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Pick best content type |
19
|
|
|
* |
20
|
|
|
* @param array $priorities |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
2 |
|
public function negotiateContentType(array $priorities) |
24
|
|
|
{ |
25
|
2 |
|
return $this->negotiate($priorities); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Pick best language |
30
|
|
|
* |
31
|
|
|
* @param array $priorities |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
2 |
|
public function negotiateLanguage(array $priorities) |
35
|
|
|
{ |
36
|
2 |
|
return $this->negotiate($priorities, 'language'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Pick best encoding |
41
|
|
|
* |
42
|
|
|
* @param array $priorities |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
2 |
|
public function negotiateEncoding(array $priorities) |
46
|
|
|
{ |
47
|
2 |
|
return $this->negotiate($priorities, 'encoding'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Pick best charset |
52
|
|
|
* |
53
|
|
|
* @param array $priorities |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
2 |
|
public function negotiateCharset(array $priorities) |
57
|
|
|
{ |
58
|
2 |
|
return $this->negotiate($priorities, 'charset'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Generalize negotiation |
63
|
|
|
* |
64
|
|
|
* @param array $priorities |
65
|
|
|
* @param string $type Negotiator type |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
8 |
|
protected function negotiate(array $priorities, $type = '') |
69
|
|
|
{ |
70
|
8 |
|
$header = 'Accept'; |
71
|
|
|
|
72
|
8 |
|
if ($type) { |
73
|
6 |
|
$header .= '-' . ucfirst($type); |
74
|
6 |
|
} |
75
|
|
|
|
76
|
8 |
|
$header = $this->getRequest()->getHeader($header); |
77
|
8 |
|
$header = join(', ', $header); |
78
|
|
|
|
79
|
8 |
|
$negotiator = $this->getNegotiator($type); |
80
|
8 |
|
$chosen = $negotiator->getBest($header, $priorities); |
81
|
|
|
|
82
|
8 |
|
return $chosen ? $chosen->getType() : ''; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get negotiation library instance |
87
|
|
|
* |
88
|
|
|
* @param string $type Negotiator type |
89
|
|
|
* @return Negotiation\AbstractNegotiator |
90
|
|
|
*/ |
91
|
|
|
protected function getNegotiator($type = '') |
92
|
|
|
{ |
93
|
|
|
$class = $this->getNegotiatorName($type); |
94
|
|
|
|
95
|
|
|
return new $class(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get negotiator name |
100
|
|
|
* |
101
|
|
|
* @param string $type |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
8 |
|
protected function getNegotiatorName($type = '') |
105
|
|
|
{ |
106
|
8 |
|
return 'Negotiation\\' . ucfirst($type) . 'Negotiator'; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|