Completed
Pull Request — master (#211)
by Woody
22:54
created

AbstractStrategy::getDefaultResponseHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace League\Route\Strategy;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
abstract class AbstractStrategy implements StrategyInterface
8
{
9
    /** @var array */
10
    protected $defaultResponseHeaders = [];
11
12
    /**
13
     * Get current default response headers
14
     *
15
     * @return array
16
     */
17
    public function getDefaultResponseHeaders() : array
18
    {
19
        return $this->defaultResponseHeaders;
20
    }
21
22
    /**
23
     * Add or replace a default response header
24
     *
25
     * @param string $name
26
     * @param string $value
27
     * @return static
28
     */
29
    public function addDefaultResponseHeader(string $name, string $value) : AbstractStrategy
30
    {
31
        $this->defaultResponseHeaders[strtolower($name)] = $value;
32
33
        return $this;
34
    }
35
36
    /**
37
     * Add multiple default response headers
38
     *
39
     * @param array $headers
40
     * @return static
41
     */
42
    public function addDefaultResponseHeaders(array $headers) : AbstractStrategy
43
    {
44
        foreach ($headers as $name => $value) {
45
            $this->addDefaultResponseHeader($name, $value);
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * Apply default response headers
53
     *
54
     * Headers that already exist on the response will NOT be replaced.
55
     *
56
     * @param \Psr\Http\Message\ResponseInterface $response
57
     * @return \Psr\Http\Message\ResponseInterface
58
     */
59
    protected function applyDefaultResponseHeaders(ResponseInterface $response) : ResponseInterface
60
    {
61
        foreach ($this->defaultResponseHeaders as $name => $value) {
62
            if (! $response->hasHeader($name)) {
63
                $response = $response->withHeader($name, $value);
64
            }
65
        }
66
67
        return $response;
68
    }
69
}
70