Failed Conditions
Push — master ( a26426...65249a )
by Arnold
14:53 queued 04:44
created

src/Traits/ContentNegotiation.php (2 issues)

Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace Jasny\Controller\Traits;
5
6
use Negotiation\{AbstractNegotiator, Negotiator, LanguageNegotiator, EncodingNegotiator, CharsetNegotiator};
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
/**
11
 * Controller methods to negotiate content
12
 */
13
trait ContentNegotiation
14
{
15
    abstract protected function getRequest(): ServerRequestInterface;
16
    abstract protected function getResponse(): ResponseInterface;
17
18
    abstract protected function header(string $header, string|int|\Stringable $value, bool $add = false): static;
19
20
    /**
21
     * Pick the best content type
22
     *
23
     * @param string[] $priorities
24
     * @return string
25
     */
26
    protected function negotiateContentType(array $priorities): string
27
    {
28
        $contentType = $this->negotiate(new Negotiator(), 'Accept', $priorities);
29
30
        if ($contentType !== '') {
31
            $this->header('Content-Type', $contentType);
32
        }
33
34
        return $contentType;
35
    }
36
37
    /**
38
     * Pick the best language and set the `Content-Language` header
39
     *
40
     * @param string[] $priorities
41
     * @return string
42
     */
43
    protected function negotiateLanguage(array $priorities): string
44
    {
45
        $language = $this->negotiate(new LanguageNegotiator(), 'Accept-Language', $priorities);
46
47
        if ($language !== '') {
48
            $this->header('Content-Language', $language);
49
        }
50
51
        return $language;
52
    }
53
54
    /**
55
     * Pick the best encoding and set `Content-Encoding` header
56
     *
57
     * @param string[] $priorities
58
     * @return string
59
     */
60
    protected function negotiateEncoding(array $priorities): string
61
    {
62
        $encoding = $this->negotiate(new EncodingNegotiator(), 'Accept-Encoding', $priorities);
63
64
        if ($encoding !== '') {
65
            $this->header('Content-Encoding', $encoding);
66
        }
67
68
        return $encoding;
69
    }
70
71
    /**
72
     * Pick the best charset.
73
     * This method will modify the `Content-Type` header if it's set.
74
     *
75
     * @param string[] $priorities
76
     * @return string
77
     */
78
    protected function negotiateCharset(array $priorities): string
79
    {
80
        $charset = $this->negotiate(new CharsetNegotiator(), 'Accept-Charset', $priorities);
81
82
        $contentType = $this->getResponse()->getHeaderLine('Content-Type');
83
        if ($contentType !== '') {
84
            $contentType = preg_replace('/;\s*charset\s*=[^;]+/', '', $contentType)
85
                . "; charset=$charset";
86
            $this->header('Content-Type', $contentType);
87
        }
88
89
        return $charset;
90
    }
91
92
    /**
93
     * Generalize negotiation.
94
     */
95
    private function negotiate(AbstractNegotiator $negotiator, string $header, array $priorities): string
96
    {
97
        $value = $this->getRequest()->getHeaderLine($header);
98
        $chosen = $negotiator->getBest($value, $priorities);
99
100
        return $chosen ? $chosen->getType() : '';
0 ignored issues
show
Call to an undefined method Negotiation\AcceptHeader::getType().
Loading history...
Only booleans are allowed in a ternary operator condition, Negotiation\AcceptHeader|null given.
Loading history...
101
    }
102
}
103