[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/Response/ -> JsonResponse.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  
  17  use function is_object;
  18  use function is_resource;
  19  use function json_encode;
  20  use function json_last_error;
  21  use function json_last_error_msg;
  22  use function sprintf;
  23  
  24  use const JSON_ERROR_NONE;
  25  
  26  /**
  27   * JSON response.
  28   *
  29   * Allows creating a response by passing data to the constructor; by default,
  30   * serializes the data to JSON, sets a status code of 200 and sets the
  31   * Content-Type header to application/json.
  32   */
  33  class JsonResponse extends Response
  34  {
  35      use InjectContentTypeTrait;
  36  
  37      /**
  38       * Default flags for json_encode; value of:
  39       *
  40       * <code>
  41       * JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES
  42       * </code>
  43       *
  44       * @const int
  45       */
  46      const DEFAULT_JSON_FLAGS = 79;
  47  
  48      /**
  49       * @var mixed
  50       */
  51      private $payload;
  52  
  53      /**
  54       * @var int
  55       */
  56      private $encodingOptions;
  57  
  58      /**
  59       * Create a JSON response with the given data.
  60       *
  61       * Default JSON encoding is performed with the following options, which
  62       * produces RFC4627-compliant JSON, capable of embedding into HTML.
  63       *
  64       * - JSON_HEX_TAG
  65       * - JSON_HEX_APOS
  66       * - JSON_HEX_AMP
  67       * - JSON_HEX_QUOT
  68       * - JSON_UNESCAPED_SLASHES
  69       *
  70       * @param mixed $data Data to convert to JSON.
  71       * @param int $status Integer status code for the response; 200 by default.
  72       * @param array $headers Array of headers to use at initialization.
  73       * @param int $encodingOptions JSON encoding options to use.
  74       * @throws Exception\InvalidArgumentException if unable to encode the $data to JSON.
  75       */
  76      public function __construct(
  77          $data,
  78          int $status = 200,
  79          array $headers = [],
  80          int $encodingOptions = self::DEFAULT_JSON_FLAGS
  81      ) {
  82          $this->setPayload($data);
  83          $this->encodingOptions = $encodingOptions;
  84  
  85          $json = $this->jsonEncode($data, $this->encodingOptions);
  86          $body = $this->createBodyFromJson($json);
  87  
  88          $headers = $this->injectContentType('application/json', $headers);
  89  
  90          parent::__construct($body, $status, $headers);
  91      }
  92  
  93      /**
  94       * @return mixed
  95       */
  96      public function getPayload()
  97      {
  98          return $this->payload;
  99      }
 100  
 101      /**
 102       * @param mixed $data
 103       */
 104      public function withPayload($data) : JsonResponse
 105      {
 106          $new = clone $this;
 107          $new->setPayload($data);
 108          return $this->updateBodyFor($new);
 109      }
 110  
 111      public function getEncodingOptions() : int
 112      {
 113          return $this->encodingOptions;
 114      }
 115  
 116      public function withEncodingOptions(int $encodingOptions) : JsonResponse
 117      {
 118          $new = clone $this;
 119          $new->encodingOptions = $encodingOptions;
 120          return $this->updateBodyFor($new);
 121      }
 122  
 123      private function createBodyFromJson(string $json) : Stream
 124      {
 125          $body = new Stream('php://temp', 'wb+');
 126          $body->write($json);
 127          $body->rewind();
 128  
 129          return $body;
 130      }
 131  
 132      /**
 133       * Encode the provided data to JSON.
 134       *
 135       * @param mixed $data
 136       * @throws Exception\InvalidArgumentException if unable to encode the $data to JSON.
 137       */
 138      private function jsonEncode($data, int $encodingOptions) : string
 139      {
 140          if (is_resource($data)) {
 141              throw new Exception\InvalidArgumentException('Cannot JSON encode resources');
 142          }
 143  
 144          // Clear json_last_error()
 145          json_encode(null);
 146  
 147          $json = json_encode($data, $encodingOptions);
 148  
 149          if (JSON_ERROR_NONE !== json_last_error()) {
 150              throw new Exception\InvalidArgumentException(sprintf(
 151                  'Unable to encode data to JSON in %s: %s',
 152                  __CLASS__,
 153                  json_last_error_msg()
 154              ));
 155          }
 156  
 157          return $json;
 158      }
 159  
 160      /**
 161       * @param mixed $data
 162       */
 163      private function setPayload($data) : void
 164      {
 165          if (is_object($data)) {
 166              $data = clone $data;
 167          }
 168  
 169          $this->payload = $data;
 170      }
 171  
 172      /**
 173       * Update the response body for the given instance.
 174       *
 175       * @param self $toUpdate Instance to update.
 176       * @return JsonResponse Returns a new instance with an updated body.
 177       */
 178      private function updateBodyFor(JsonResponse $toUpdate) : JsonResponse
 179      {
 180          $json = $this->jsonEncode($toUpdate->payload, $toUpdate->encodingOptions);
 181          $body = $this->createBodyFromJson($json);
 182          return $toUpdate->withBody($body);
 183      }
 184  }


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