[ 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\Request; 12 13 use Laminas\Diactoros\Exception; 14 use Laminas\Diactoros\Request; 15 use Laminas\Diactoros\Stream; 16 use Psr\Http\Message\RequestInterface; 17 use Throwable; 18 19 use function sprintf; 20 21 /** 22 * Serialize or deserialize request messages to/from arrays. 23 * 24 * This class provides functionality for serializing a RequestInterface instance 25 * to an array, as well as the reverse operation of creating a Request instance 26 * from an array representing a message. 27 */ 28 final class ArraySerializer 29 { 30 /** 31 * Serialize a request message to an array. 32 */ 33 public static function toArray(RequestInterface $request) : array 34 { 35 return [ 36 'method' => $request->getMethod(), 37 'request_target' => $request->getRequestTarget(), 38 'uri' => (string) $request->getUri(), 39 'protocol_version' => $request->getProtocolVersion(), 40 'headers' => $request->getHeaders(), 41 'body' => (string) $request->getBody(), 42 ]; 43 } 44 45 /** 46 * Deserialize a request array to a request instance. 47 * 48 * @throws Exception\DeserializationException when cannot deserialize response 49 */ 50 public static function fromArray(array $serializedRequest) : Request 51 { 52 try { 53 $uri = self::getValueFromKey($serializedRequest, 'uri'); 54 $method = self::getValueFromKey($serializedRequest, 'method'); 55 $body = new Stream('php://memory', 'wb+'); 56 $body->write(self::getValueFromKey($serializedRequest, 'body')); 57 $headers = self::getValueFromKey($serializedRequest, 'headers'); 58 $requestTarget = self::getValueFromKey($serializedRequest, 'request_target'); 59 $protocolVersion = self::getValueFromKey($serializedRequest, 'protocol_version'); 60 61 return (new Request($uri, $method, $body, $headers)) 62 ->withRequestTarget($requestTarget) 63 ->withProtocolVersion($protocolVersion); 64 } catch (Throwable $exception) { 65 throw Exception\DeserializationException::forRequestFromArray($exception); 66 } 67 } 68 69 /** 70 * @return mixed 71 * @throws Exception\DeserializationException 72 */ 73 private static function getValueFromKey(array $data, string $key, string $message = null) 74 { 75 if (isset($data[$key])) { 76 return $data[$key]; 77 } 78 if ($message === null) { 79 $message = sprintf('Missing "%s" key in serialized request', $key); 80 } 81 throw new Exception\DeserializationException($message); 82 } 83 }
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 |