PHP Classes

File: src/JSON.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   iaso PHP JSON Parser Library   src/JSON.php   Download  
File: src/JSON.php
Role: Class source
Content type: text/plain
Description: Class source
Class: iaso PHP JSON Parser Library
Parse JSON strings immune to hash-DoS attacks
Author: By
Last change:
Date: 2 years ago
Size: 2,218 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);
namespace
ParagonIE\Iaso;

use
ParagonIE\Iaso\Contract\Blank;
use
ParagonIE\Iaso\Result\Bare;

/**
 * Class JSON
 *
 * This is just an easy public API
 *
 * @package ParagonIE\Iaso
 */
abstract class JSON
{
   
/**
     * @var string
     */
   
protected static $seed;

   
/**
     * @var Parser
     */
   
protected static $parser;

   
/**
     * @param string $json Input data
     * @param Contract|null $contract Contract object for the parser
     * @param bool $asArray Convert objects to arrays?
     * @return mixed
     */
   
public static function parse(
       
string $json,
       
Contract $contract = null,
       
bool $asArray = false
   
) {
       
$parsed = isset($contract)
            ? static::
parseWithContract($json, $contract)
            : static::
parseString($json);

        if (
$parsed instanceof Bare) {
            return
$parsed->getBareValue();
        } elseif (
$asArray) {
            return
$parsed->asArray(true);
        }
        return
$parsed;
    }

   
/**
     * @param string $json
     * @return ResultSet
     */
   
protected static function parseString(string $json = ''): ResultSet
   
{
       
$parser = static::getParser();
        return
$parser->parse($json, new Blank);
    }

   
/**
     * @param string $json
     * @param Contract $contract
     * @return ResultSet
     */
   
protected static function parseWithContract(
       
string $json,
       
Contract $contract
   
): ResultSet {
       
$parser = static::getParser();
        return
$parser->parse($json, $contract);
    }

   
/**
     * Overloadable method for getting the parser object we desire
     */
   
public static function getParser(): Parser
   
{
        if (!isset(
self::$parser)) {
           
self::$parser = new Parser;
        }
        return
self::$parser;
    }

   
/**
     * Get a unique random secret once per script execution
     *
     * @return string
     */
   
public static function getSeed(): string
   
{
        if (!
self::$seed) {
           
self::$seed = \random_bytes(
                \
ParagonIE_Sodium_Compat::CRYPTO_SHORTHASH_KEYBYTES
           
);
        }
        return
self::$seed;
    }
}