[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/Request/ -> 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\Request;
  12  
  13  use Laminas\Diactoros\AbstractSerializer;
  14  use Laminas\Diactoros\Exception;
  15  use Laminas\Diactoros\Request;
  16  use Laminas\Diactoros\Stream;
  17  use Laminas\Diactoros\Uri;
  18  use Psr\Http\Message\RequestInterface;
  19  use Psr\Http\Message\StreamInterface;
  20  
  21  use function preg_match;
  22  use function sprintf;
  23  
  24  /**
  25   * Serialize (cast to string) or deserialize (cast string to Request) messages.
  26   *
  27   * This class provides functionality for serializing a RequestInterface instance
  28   * to a string, as well as the reverse operation of creating a Request instance
  29   * from a string/stream representing a message.
  30   */
  31  final class Serializer extends AbstractSerializer
  32  {
  33      /**
  34       * Deserialize a request string to a request instance.
  35       *
  36       * Internally, casts the message to a stream and invokes fromStream().
  37       *
  38       * @throws Exception\SerializationException when errors occur parsing the message.
  39       */
  40      public static function fromString(string $message) : Request
  41      {
  42          $stream = new Stream('php://temp', 'wb+');
  43          $stream->write($message);
  44          return self::fromStream($stream);
  45      }
  46  
  47      /**
  48       * Deserialize a request stream to a request instance.
  49       *
  50       * @throws Exception\InvalidArgumentException if the message stream is not
  51       *     readable or seekable.
  52       * @throws Exception\SerializationException if an invalid request line is detected.
  53       */
  54      public static function fromStream(StreamInterface $stream) : Request
  55      {
  56          if (! $stream->isReadable() || ! $stream->isSeekable()) {
  57              throw new Exception\InvalidArgumentException('Message stream must be both readable and seekable');
  58          }
  59  
  60          $stream->rewind();
  61  
  62          [$method, $requestTarget, $version] = self::getRequestLine($stream);
  63          $uri = self::createUriFromRequestTarget($requestTarget);
  64  
  65          [$headers, $body] = self::splitStream($stream);
  66  
  67          return (new Request($uri, $method, $body, $headers))
  68              ->withProtocolVersion($version)
  69              ->withRequestTarget($requestTarget);
  70      }
  71  
  72      /**
  73       * Serialize a request message to a string.
  74       */
  75      public static function toString(RequestInterface $request) : string
  76      {
  77          $httpMethod = $request->getMethod();
  78          $headers = self::serializeHeaders($request->getHeaders());
  79          $body    = (string) $request->getBody();
  80          $format  = '%s %s HTTP/%s%s%s';
  81  
  82          if (! empty($headers)) {
  83              $headers = "\r\n" . $headers;
  84          }
  85          if (! empty($body)) {
  86              $headers .= "\r\n\r\n";
  87          }
  88  
  89          return sprintf(
  90              $format,
  91              $httpMethod,
  92              $request->getRequestTarget(),
  93              $request->getProtocolVersion(),
  94              $headers,
  95              $body
  96          );
  97      }
  98  
  99      /**
 100       * Retrieve the components of the request line.
 101       *
 102       * Retrieves the first line of the stream and parses it, raising an
 103       * exception if it does not follow specifications; if valid, returns a list
 104       * with the method, target, and version, in that order.
 105       *
 106       * @throws Exception\SerializationException
 107       */
 108      private static function getRequestLine(StreamInterface $stream) : array
 109      {
 110          $requestLine = self::getLine($stream);
 111  
 112          if (! preg_match(
 113              '#^(?P<method>[!\#$%&\'*+.^_`|~a-zA-Z0-9-]+) (?P<target>[^\s]+) HTTP/(?P<version>[1-9]\d*\.\d+)$#',
 114              $requestLine,
 115              $matches
 116          )) {
 117              throw Exception\SerializationException::forInvalidRequestLine();
 118          }
 119  
 120          return [$matches['method'], $matches['target'], $matches['version']];
 121      }
 122  
 123      /**
 124       * Create and return a Uri instance based on the provided request target.
 125       *
 126       * If the request target is of authority or asterisk form, an empty Uri
 127       * instance is returned; otherwise, the value is used to create and return
 128       * a new Uri instance.
 129       */
 130      private static function createUriFromRequestTarget(string $requestTarget) : Uri
 131      {
 132          if (preg_match('#^https?://#', $requestTarget)) {
 133              return new Uri($requestTarget);
 134          }
 135  
 136          if (preg_match('#^(\*|[^/])#', $requestTarget)) {
 137              return new Uri();
 138          }
 139  
 140          return new Uri($requestTarget);
 141      }
 142  }


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