[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/web-token/jwt-core/Util/ -> JsonConverter.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  /*
   6   * The MIT License (MIT)
   7   *
   8   * Copyright (c) 2014-2020 Spomky-Labs
   9   *
  10   * This software may be modified and distributed under the terms
  11   * of the MIT license.  See the LICENSE file for details.
  12   */
  13  
  14  namespace Jose\Component\Core\Util;
  15  
  16  use InvalidArgumentException;
  17  use function is_string;
  18  use RuntimeException;
  19  use Throwable;
  20  
  21  final class JsonConverter
  22  {
  23      /**
  24       * @param mixed $payload
  25       *
  26       * @throws RuntimeException if the payload cannot be encoded
  27       */
  28      public static function encode($payload): string
  29      {
  30          try {
  31              $data = json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  32              if (!is_string($data)) {
  33                  throw new InvalidArgumentException('Unable to encode the data');
  34              }
  35  
  36              return $data;
  37          } catch (Throwable $throwable) {
  38              throw new RuntimeException('Invalid content.', $throwable->getCode(), $throwable);
  39          }
  40      }
  41  
  42      /**
  43       * @throws RuntimeException if the payload cannot be decoded
  44       *
  45       * @return mixed
  46       */
  47      public static function decode(string $payload)
  48      {
  49          try {
  50              return json_decode($payload, true, 512, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  51          } catch (Throwable $throwable) {
  52              throw new RuntimeException('Invalid content.', $throwable->getCode(), $throwable);
  53          }
  54      }
  55  }


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