[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/Response/ -> ArraySerializer.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer