| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
abstract class RequestBase implements RequestInterface { |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
private $headerCollection; |
| 13 |
|
| 14 |
|
| 15 |
private $pathParams = []; |
| 16 |
|
| 17 |
|
| 18 |
private $cookiePrefix; |
| 19 |
|
| 20 |
protected ?array $parsedBody = null; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function __construct( $cookiePrefix ) { |
| 27 |
$this->cookiePrefix = $cookiePrefix; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
protected function initHeaders() { |
| 37 |
} |
| 38 |
|
| 39 |
public function __clone() { |
| 40 |
if ( $this->headerCollection !== null ) { |
| 41 |
$this->headerCollection = clone $this->headerCollection; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function setHeaders( $headers ) { |
| 56 |
$this->headerCollection = new HeaderContainer; |
| 57 |
$this->headerCollection->resetHeaders( $headers ); |
| 58 |
} |
| 59 |
|
| 60 |
public function getHeaders() { |
| 61 |
if ( $this->headerCollection === null ) { |
| 62 |
$this->initHeaders(); |
| 63 |
} |
| 64 |
return $this->headerCollection->getHeaders(); |
| 65 |
} |
| 66 |
|
| 67 |
public function getHeader( $name ) { |
| 68 |
if ( $this->headerCollection === null ) { |
| 69 |
$this->initHeaders(); |
| 70 |
} |
| 71 |
return $this->headerCollection->getHeader( $name ); |
| 72 |
} |
| 73 |
|
| 74 |
public function hasHeader( $name ) { |
| 75 |
if ( $this->headerCollection === null ) { |
| 76 |
$this->initHeaders(); |
| 77 |
} |
| 78 |
return $this->headerCollection->hasHeader( $name ); |
| 79 |
} |
| 80 |
|
| 81 |
public function getHeaderLine( $name ) { |
| 82 |
if ( $this->headerCollection === null ) { |
| 83 |
$this->initHeaders(); |
| 84 |
} |
| 85 |
return $this->headerCollection->getHeaderLine( $name ); |
| 86 |
} |
| 87 |
|
| 88 |
public function setPathParams( $params ) { |
| 89 |
$this->pathParams = $params; |
| 90 |
} |
| 91 |
|
| 92 |
public function getPathParams() { |
| 93 |
return $this->pathParams; |
| 94 |
} |
| 95 |
|
| 96 |
public function getPathParam( $name ) { |
| 97 |
return $this->pathParams[$name] ?? null; |
| 98 |
} |
| 99 |
|
| 100 |
public function getCookiePrefix() { |
| 101 |
return $this->cookiePrefix; |
| 102 |
} |
| 103 |
|
| 104 |
public function getCookie( $name, $default = null ) { |
| 105 |
$cookies = $this->getCookieParams(); |
| 106 |
$prefixedName = $this->getCookiePrefix() . $name; |
| 107 |
if ( array_key_exists( $prefixedName, $cookies ) ) { |
| 108 |
return $cookies[$prefixedName]; |
| 109 |
} else { |
| 110 |
return $default; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public function getParsedBody(): ?array { |
| 115 |
return $this->parsedBody; |
| 116 |
} |
| 117 |
|
| 118 |
public function setParsedBody( ?array $data ) { |
| 119 |
$this->parsedBody = $data; |
| 120 |
} |
| 121 |
|
| 122 |
public function getBodyType(): ?string { |
| 123 |
[ $ct ] = explode( ';', $this->getHeaderLine( 'Content-Type' ), 2 ); |
| 124 |
$ct = strtolower( trim( $ct ) ); |
| 125 |
|
| 126 |
if ( $ct === '' ) { |
| 127 |
return null; |
| 128 |
} |
| 129 |
return $ct; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function hasBody(): bool { |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if ( $this->getHeaderLine( 'content-length' ) !== '' ) { |
| 149 |
|
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
if ( $this->getHeaderLine( 'transfer-encoding' ) !== '' ) { |
| 154 |
|
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
} |