xref: /dokuwiki/vendor/simplepie/simplepie/src/HTTP/Psr7Response.php (revision 8e88a29b81301f78509349ab1152bb09c229123e)
1<?php
2
3// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
4// SPDX-License-Identifier: BSD-3-Clause
5
6declare(strict_types=1);
7
8namespace SimplePie\HTTP;
9
10use Psr\Http\Message\ResponseInterface;
11
12/**
13 * HTTP Response based on a PSR-7 response
14 *
15 * This interface must be interoperable with Psr\Http\Message\ResponseInterface
16 * @see https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface
17 *
18 * @internal
19 */
20final class Psr7Response implements Response
21{
22    /**
23     * @var ResponseInterface
24     */
25    private $response;
26
27    /**
28     * @var string
29     */
30    private $permanent_url;
31
32    /**
33     * @var string
34     */
35    private $requested_url;
36
37    public function __construct(ResponseInterface $response, string $permanent_url, string $requested_url)
38    {
39        $this->response = $response;
40        $this->permanent_url = $permanent_url;
41        $this->requested_url = $requested_url;
42    }
43
44    public function get_permanent_uri(): string
45    {
46        return $this->permanent_url;
47    }
48
49    public function get_final_requested_uri(): string
50    {
51        return $this->requested_url;
52    }
53
54    public function get_status_code(): int
55    {
56        return $this->response->getStatusCode();
57    }
58
59    public function get_headers(): array
60    {
61        // The filtering is probably redundant but let’s make PHPStan happy.
62        return array_filter($this->response->getHeaders(), function (array $header): bool {
63            return count($header) >= 1;
64        });
65    }
66
67    public function has_header(string $name): bool
68    {
69        return $this->response->hasHeader($name);
70    }
71
72    public function with_header(string $name, $value)
73    {
74        return new self($this->response->withHeader($name, $value), $this->permanent_url, $this->requested_url);
75    }
76
77    public function get_header(string $name): array
78    {
79        return $this->response->getHeader($name);
80    }
81
82    public function get_header_line(string $name): string
83    {
84        return $this->response->getHeaderLine($name);
85    }
86
87    public function get_body_content(): string
88    {
89        return $this->response->getBody()->__toString();
90    }
91}
92