[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @see https://github.com/laminas/laminas-diactoros for the canonical source repository 5 * @copyright https://github.com/laminas/laminas-diactoros/blob/master/COPYRIGHT.md 6 * @license https://github.com/laminas/laminas-diactoros/blob/master/LICENSE.md New BSD License 7 */ 8 9 declare(strict_types=1); 10 11 namespace Laminas\Diactoros; 12 13 use Psr\Http\Message\ServerRequestInterface; 14 use Psr\Http\Message\StreamInterface; 15 use Psr\Http\Message\UploadedFileInterface; 16 use Psr\Http\Message\UriInterface; 17 18 use function array_key_exists; 19 use function is_array; 20 21 /** 22 * Server-side HTTP request 23 * 24 * Extends the Request definition to add methods for accessing incoming data, 25 * specifically server parameters, cookies, matched path parameters, query 26 * string arguments, body parameters, and upload file information. 27 * 28 * "Attributes" are discovered via decomposing the request (and usually 29 * specifically the URI path), and typically will be injected by the application. 30 * 31 * Requests are considered immutable; all methods that might change state are 32 * implemented such that they retain the internal state of the current 33 * message and return a new instance that contains the changed state. 34 */ 35 class ServerRequest implements ServerRequestInterface 36 { 37 use RequestTrait; 38 39 /** 40 * @var array 41 */ 42 private $attributes = []; 43 44 /** 45 * @var array 46 */ 47 private $cookieParams = []; 48 49 /** 50 * @var null|array|object 51 */ 52 private $parsedBody; 53 54 /** 55 * @var array 56 */ 57 private $queryParams = []; 58 59 /** 60 * @var array 61 */ 62 private $serverParams; 63 64 /** 65 * @var array 66 */ 67 private $uploadedFiles; 68 69 /** 70 * @param array $serverParams Server parameters, typically from $_SERVER 71 * @param array $uploadedFiles Upload file information, a tree of UploadedFiles 72 * @param null|string|UriInterface $uri URI for the request, if any. 73 * @param null|string $method HTTP method for the request, if any. 74 * @param string|resource|StreamInterface $body Message body, if any. 75 * @param array $headers Headers for the message, if any. 76 * @param array $cookies Cookies for the message, if any. 77 * @param array $queryParams Query params for the message, if any. 78 * @param null|array|object $parsedBody The deserialized body parameters, if any. 79 * @param string $protocol HTTP protocol version. 80 * @throws Exception\InvalidArgumentException for any invalid value. 81 */ 82 public function __construct( 83 array $serverParams = [], 84 array $uploadedFiles = [], 85 $uri = null, 86 string $method = null, 87 $body = 'php://input', 88 array $headers = [], 89 array $cookies = [], 90 array $queryParams = [], 91 $parsedBody = null, 92 string $protocol = '1.1' 93 ) { 94 $this->validateUploadedFiles($uploadedFiles); 95 96 if ($body === 'php://input') { 97 $body = new PhpInputStream(); 98 } 99 100 $this->initialize($uri, $method, $body, $headers); 101 $this->serverParams = $serverParams; 102 $this->uploadedFiles = $uploadedFiles; 103 $this->cookieParams = $cookies; 104 $this->queryParams = $queryParams; 105 $this->parsedBody = $parsedBody; 106 $this->protocol = $protocol; 107 } 108 109 /** 110 * {@inheritdoc} 111 */ 112 public function getServerParams() : array 113 { 114 return $this->serverParams; 115 } 116 117 /** 118 * {@inheritdoc} 119 */ 120 public function getUploadedFiles() : array 121 { 122 return $this->uploadedFiles; 123 } 124 125 /** 126 * {@inheritdoc} 127 */ 128 public function withUploadedFiles(array $uploadedFiles) : ServerRequest 129 { 130 $this->validateUploadedFiles($uploadedFiles); 131 $new = clone $this; 132 $new->uploadedFiles = $uploadedFiles; 133 return $new; 134 } 135 136 /** 137 * {@inheritdoc} 138 */ 139 public function getCookieParams() : array 140 { 141 return $this->cookieParams; 142 } 143 144 /** 145 * {@inheritdoc} 146 */ 147 public function withCookieParams(array $cookies) : ServerRequest 148 { 149 $new = clone $this; 150 $new->cookieParams = $cookies; 151 return $new; 152 } 153 154 /** 155 * {@inheritdoc} 156 */ 157 public function getQueryParams() : array 158 { 159 return $this->queryParams; 160 } 161 162 /** 163 * {@inheritdoc} 164 */ 165 public function withQueryParams(array $query) : ServerRequest 166 { 167 $new = clone $this; 168 $new->queryParams = $query; 169 return $new; 170 } 171 172 /** 173 * {@inheritdoc} 174 */ 175 public function getParsedBody() 176 { 177 return $this->parsedBody; 178 } 179 180 /** 181 * {@inheritdoc} 182 */ 183 public function withParsedBody($data) : ServerRequest 184 { 185 if (! is_array($data) && ! is_object($data) && null !== $data) { 186 throw new Exception\InvalidArgumentException(sprintf( 187 '%s expects a null, array, or object argument; received %s', 188 __METHOD__, 189 gettype($data) 190 )); 191 } 192 193 $new = clone $this; 194 $new->parsedBody = $data; 195 return $new; 196 } 197 198 /** 199 * {@inheritdoc} 200 */ 201 public function getAttributes() : array 202 { 203 return $this->attributes; 204 } 205 206 /** 207 * {@inheritdoc} 208 */ 209 public function getAttribute($attribute, $default = null) 210 { 211 if (! array_key_exists($attribute, $this->attributes)) { 212 return $default; 213 } 214 215 return $this->attributes[$attribute]; 216 } 217 218 /** 219 * {@inheritdoc} 220 */ 221 public function withAttribute($attribute, $value) : ServerRequest 222 { 223 $new = clone $this; 224 $new->attributes[$attribute] = $value; 225 return $new; 226 } 227 228 /** 229 * {@inheritdoc} 230 */ 231 public function withoutAttribute($attribute) : ServerRequest 232 { 233 $new = clone $this; 234 unset($new->attributes[$attribute]); 235 return $new; 236 } 237 238 /** 239 * Recursively validate the structure in an uploaded files array. 240 * 241 * @throws Exception\InvalidArgumentException if any leaf is not an UploadedFileInterface instance. 242 */ 243 private function validateUploadedFiles(array $uploadedFiles) : void 244 { 245 foreach ($uploadedFiles as $file) { 246 if (is_array($file)) { 247 $this->validateUploadedFiles($file); 248 continue; 249 } 250 251 if (! $file instanceof UploadedFileInterface) { 252 throw new Exception\InvalidArgumentException('Invalid leaf in uploaded files structure'); 253 } 254 } 255 } 256 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |