[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/registry/src/Format/ -> Json.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Registry Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Registry\Format;
  10  
  11  use Joomla\Registry\Factory;
  12  use Joomla\Registry\FormatInterface;
  13  
  14  /**
  15   * JSON format handler for Registry.
  16   *
  17   * @since  1.0
  18   */
  19  class Json implements FormatInterface
  20  {
  21      /**
  22       * Converts an object into a JSON formatted string.
  23       *
  24       * @param   object  $object   Data source object.
  25       * @param   array   $options  Options used by the formatter.
  26       *
  27       * @return  string  JSON formatted string.
  28       *
  29       * @since   1.0
  30       */
  31  	public function objectToString($object, array $options = [])
  32      {
  33          $bitMask = $options['bitmask'] ?? 0;
  34          $depth   = $options['depth'] ?? 512;
  35  
  36          return json_encode($object, $bitMask, $depth);
  37      }
  38  
  39      /**
  40       * Parse a JSON formatted string and convert it into an object.
  41       *
  42       * If the string is not in JSON format, this method will attempt to parse it as INI format.
  43       *
  44       * @param   string  $data     JSON formatted string to convert.
  45       * @param   array   $options  Options used by the formatter.
  46       *
  47       * @return  object   Data object.
  48       *
  49       * @since   1.0
  50       * @throws  \RuntimeException
  51       */
  52  	public function stringToObject($data, array $options = ['processSections' => false])
  53      {
  54          $data = trim($data);
  55  
  56          // Because developers are clearly not validating their data before pushing it into a Registry, we'll do it for them
  57          if (empty($data))
  58          {
  59              return new \stdClass;
  60          }
  61  
  62          $decoded = json_decode($data);
  63  
  64          // Check for an error decoding the data
  65          if ($decoded === null && json_last_error() !== JSON_ERROR_NONE)
  66          {
  67              // If it's an ini file, parse as ini.
  68              if ($data !== '' && $data[0] !== '{')
  69              {
  70                  return Factory::getFormat('Ini')->stringToObject($data, $options);
  71              }
  72  
  73              throw new \RuntimeException(sprintf('Error decoding JSON data: %s', json_last_error_msg()));
  74          }
  75  
  76          return (object) $decoded;
  77      }
  78  }


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