Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / http / MwHttpRequestToResponseInterfaceAdapter.php
blob26fac9e079b91ddd9789bdab19d34db8e43c8ec3
1 <?php
3 declare( strict_types = 1 );
4 namespace MediaWiki\Http;
6 use GuzzleHttp\Psr7\Utils;
7 use LogicException;
8 use MWHttpRequest;
9 use Psr\Http\Message\ResponseInterface;
10 use Psr\Http\Message\StreamInterface;
12 /**
13 * @since 1.36
14 * @unstable
16 * @license GPL-2.0-or-later
18 class MwHttpRequestToResponseInterfaceAdapter implements ResponseInterface {
20 /**
21 * @var MWHttpRequest
23 private $mwHttpRequest;
25 /**
26 * @param MWHttpRequest $mwHttpRequest the MWHttpRequest must contain response information, i.e. must have been
27 * `execute`d
29 public function __construct( MWHttpRequest $mwHttpRequest ) {
30 $this->validateHasResponse( $mwHttpRequest );
31 $this->mwHttpRequest = $mwHttpRequest;
34 public function getProtocolVersion(): string {
35 // @phan-suppress-previous-line PhanPluginNeverReturnMethod
36 // This is not accessible via MWHttpRequest, but it is set in its protected `respVersion` property.
37 // If this is ever needed, it can get exposed in MWHttpRequest.
38 throw new LogicException( __METHOD__ . ' is not implemented' );
41 public function withProtocolVersion( $version ): self {
42 $this->throwExceptionForBuilderMethod( __METHOD__ );
45 public function getHeaders(): array {
46 return $this->mwHttpRequest->getResponseHeaders();
49 public function hasHeader( $name ): bool {
50 return isset( $this->mwHttpRequest->getResponseHeaders()[$name] );
53 public function getHeader( $name ): array {
54 return $this->hasHeader( $name ) ? $this->mwHttpRequest->getResponseHeaders()[$name] : [];
57 public function getHeaderLine( $name ): string {
58 return $this->hasHeader( $name )
59 ? implode( ',', $this->mwHttpRequest->getResponseHeaders()[$name] )
60 : '';
63 public function withHeader( $name, $value ): self {
64 $this->throwExceptionForBuilderMethod( __METHOD__ );
67 public function withAddedHeader( $name, $value ): self {
68 $this->throwExceptionForBuilderMethod( __METHOD__ );
71 public function withoutHeader( $name ): self {
72 $this->throwExceptionForBuilderMethod( __METHOD__ );
75 public function getBody(): StreamInterface {
76 return Utils::streamFor( $this->mwHttpRequest->getContent() );
79 public function withBody( StreamInterface $body ): self {
80 $this->throwExceptionForBuilderMethod( __METHOD__ );
83 public function getStatusCode(): int {
84 return $this->mwHttpRequest->getStatus();
87 public function withStatus( $code, $reasonPhrase = '' ): self {
88 $this->throwExceptionForBuilderMethod( __METHOD__ );
91 public function getReasonPhrase(): string {
92 return ''; // not exposed through MWHttpRequest, unlikely to ever be useful
95 /**
96 * @param string $method
97 * @return never
99 private function throwExceptionForBuilderMethod( string $method ): void {
100 throw new LogicException( "Builder method $method is not supported." );
103 private function validateHasResponse( MWHttpRequest $mwHttpRequest ): void {
105 * MWHttpRequest objects contain request information, but also contain response information after calling
106 * `execute`. The best way of determining whether a MWHttpRequest contains response information is to check
107 * whether its headers list is empty.
109 if ( !$mwHttpRequest->getResponseHeaders() ) {
110 throw new LogicException( 'Trying to get response information from a request that was not yet executed' );