Completed
Pull Request — master (#12)
by Sergey
07:30
created

ContentNegotiation::negotiateEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 1
crap 1
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