[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/Response/ -> Serializer.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\AbstractSerializer;
  14  use Laminas\Diactoros\Exception;
  15  use Laminas\Diactoros\Response;
  16  use Laminas\Diactoros\Stream;
  17  use Psr\Http\Message\ResponseInterface;
  18  use Psr\Http\Message\StreamInterface;
  19  
  20  use function preg_match;
  21  use function sprintf;
  22  
  23  final class Serializer extends AbstractSerializer
  24  {
  25      /**
  26       * Deserialize a response string to a response instance.
  27       *
  28       * @throws Exception\SerializationException when errors occur parsing the message.
  29       */
  30      public static function fromString(string $message) : Response
  31      {
  32          $stream = new Stream('php://temp', 'wb+');
  33          $stream->write($message);
  34          return static::fromStream($stream);
  35      }
  36  
  37      /**
  38       * Parse a response from a stream.
  39       *
  40       * @throws Exception\InvalidArgumentException when the stream is not readable.
  41       * @throws Exception\SerializationException when errors occur parsing the message.
  42       */
  43      public static function fromStream(StreamInterface $stream) : Response
  44      {
  45          if (! $stream->isReadable() || ! $stream->isSeekable()) {
  46              throw new Exception\InvalidArgumentException('Message stream must be both readable and seekable');
  47          }
  48  
  49          $stream->rewind();
  50  
  51          [$version, $status, $reasonPhrase] = self::getStatusLine($stream);
  52          [$headers, $body]                  = self::splitStream($stream);
  53  
  54          return (new Response($body, $status, $headers))
  55              ->withProtocolVersion($version)
  56              ->withStatus((int) $status, $reasonPhrase);
  57      }
  58  
  59      /**
  60       * Create a string representation of a response.
  61       */
  62      public static function toString(ResponseInterface $response) : string
  63      {
  64          $reasonPhrase = $response->getReasonPhrase();
  65          $headers      = self::serializeHeaders($response->getHeaders());
  66          $body         = (string) $response->getBody();
  67          $format       = 'HTTP/%s %d%s%s%s';
  68  
  69          if (! empty($headers)) {
  70              $headers = "\r\n" . $headers;
  71          }
  72  
  73          $headers .= "\r\n\r\n";
  74  
  75          return sprintf(
  76              $format,
  77              $response->getProtocolVersion(),
  78              $response->getStatusCode(),
  79              ($reasonPhrase ? ' ' . $reasonPhrase : ''),
  80              $headers,
  81              $body
  82          );
  83      }
  84  
  85      /**
  86       * Retrieve the status line for the message.
  87       *
  88       * @return array Array with three elements: 0 => version, 1 => status, 2 => reason
  89       * @throws Exception\SerializationException if line is malformed
  90       */
  91      private static function getStatusLine(StreamInterface $stream) : array
  92      {
  93          $line = self::getLine($stream);
  94  
  95          if (! preg_match(
  96              '#^HTTP/(?P<version>[1-9]\d*\.\d) (?P<status>[1-5]\d{2})(\s+(?P<reason>.+))?$#',
  97              $line,
  98              $matches
  99          )) {
 100              throw Exception\SerializationException::forInvalidStatusLine();
 101          }
 102  
 103          return [$matches['version'], (int) $matches['status'], isset($matches['reason']) ? $matches['reason'] : ''];
 104      }
 105  }


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