117 lines
4.3 KiB
PHP
117 lines
4.3 KiB
PHP
|
<?php
|
||
|
// JsonSerialiserSettings.php
|
||
|
// Created: 2022-01-13
|
||
|
// Updated: 2022-02-02
|
||
|
|
||
|
namespace Index\Serialisation;
|
||
|
|
||
|
/**
|
||
|
* Provides settings for altering JSON serialisation behaviour.
|
||
|
*/
|
||
|
class JsonSerialiserSettings {
|
||
|
public const DEFAULT_DEPTH = 512;
|
||
|
|
||
|
private int $flags;
|
||
|
private int $maxDepth;
|
||
|
|
||
|
/**
|
||
|
* Creates a new instance of JsonSerialiserSettings.
|
||
|
*
|
||
|
* @param int $maxDepth Maximum parsing depth before the serialisation/deserialisation should stop.
|
||
|
* @param bool $throwOnError Whether the serialiser should throw an exeception upon errors.
|
||
|
* @param bool $preserveZeroFraction Ensure that float values are never encoded as integers.
|
||
|
* @param bool $prettyPrint Whether the output should be formatted.
|
||
|
* @param bool $forceObject Output an object (stdClass instance) instead of an associative array.
|
||
|
* @param bool $encodeQuotes Converts " to \u0022.
|
||
|
* @param bool $convertTags Converts < and > to \u003C and \u003E respectively.
|
||
|
* @param bool $convertAmpersand Converts & to \u0026.
|
||
|
* @param bool $convertApostrophe Converts ' to \u0027.
|
||
|
* @param bool $ignoreInvalidUtf8 Ignores invalid UTF-8 characters.
|
||
|
* @param bool $substituteInvalidUtf8 Converts invalid UTF-8 characters to \0xfffd (Unicode Character 'REPLACEMENT CHARACTER').
|
||
|
* @param bool $numericCheck Encodes numeric strings as numbers.
|
||
|
* @param bool $partialOutputOnError Substitute some unencodable values instead of failing.
|
||
|
* @param bool $unescapedLineTerminators The line terminators are kept unescaped when $unescapedUnicode is true.
|
||
|
* @param bool $unescapedSlashes Don't escape /.
|
||
|
* @param bool $unescapedUnicode Encode multibyte Unicode characters literally (default is to escape as \uXXXX).
|
||
|
* @param bool $bigIntAsString Decodes large integers as their original string value.
|
||
|
* @param bool $objectAsArray Decodes JSON objects as PHP array.
|
||
|
*/
|
||
|
public function __construct(
|
||
|
int $maxDepth = self::DEFAULT_DEPTH,
|
||
|
bool $throwOnError = true,
|
||
|
bool $preserveZeroFraction = true,
|
||
|
bool $prettyPrint = false,
|
||
|
bool $forceObject = false,
|
||
|
bool $encodeQuotes = false,
|
||
|
bool $convertTags = false,
|
||
|
bool $convertAmpersand = false,
|
||
|
bool $convertApostrophe = false,
|
||
|
bool $ignoreInvalidUtf8 = false,
|
||
|
bool $substituteInvalidUtf8 = false,
|
||
|
bool $numericCheck = false,
|
||
|
bool $partialOutputOnError = false,
|
||
|
bool $unescapedLineTerminators = false,
|
||
|
bool $unescapedSlashes = false,
|
||
|
bool $unescapedUnicode = false,
|
||
|
bool $bigIntAsString = false,
|
||
|
bool $objectAsArray = false
|
||
|
) {
|
||
|
$this->maxDepth = $maxDepth;
|
||
|
|
||
|
$flags = 0;
|
||
|
if($throwOnError)
|
||
|
$flags |= JSON_THROW_ON_ERROR;
|
||
|
if($preserveZeroFraction)
|
||
|
$flags |= JSON_PRESERVE_ZERO_FRACTION;
|
||
|
if($prettyPrint)
|
||
|
$flags |= JSON_PRETTY_PRINT;
|
||
|
if($forceObject)
|
||
|
$flags |= JSON_FORCE_OBJECT;
|
||
|
if($encodeQuotes)
|
||
|
$flags |= JSON_HEX_QUOT;
|
||
|
if($convertTags)
|
||
|
$flags |= JSON_HEX_TAG;
|
||
|
if($convertAmpersand)
|
||
|
$flags |= JSON_HEX_AMP;
|
||
|
if($convertApostrophe)
|
||
|
$flags |= JSON_HEX_APOS;
|
||
|
if($ignoreInvalidUtf8)
|
||
|
$flags |= JSON_INVALID_UTF8_IGNORE;
|
||
|
if($substituteInvalidUtf8)
|
||
|
$flags |= JSON_INVALID_UTF8_SUBSTITUTE;
|
||
|
if($numericCheck)
|
||
|
$flags |= JSON_NUMERIC_CHECK;
|
||
|
if($partialOutputOnError)
|
||
|
$flags |= JSON_PARTIAL_OUTPUT_ON_ERROR;
|
||
|
if($unescapedLineTerminators)
|
||
|
$flags |= JSON_UNESCAPED_LINE_TERMINATORS;
|
||
|
if($unescapedSlashes)
|
||
|
$flags |= JSON_UNESCAPED_SLASHES;
|
||
|
if($unescapedUnicode)
|
||
|
$flags |= JSON_UNESCAPED_UNICODE;
|
||
|
if($bigIntAsString)
|
||
|
$flags |= JSON_BIGINT_AS_STRING;
|
||
|
if($objectAsArray)
|
||
|
$flags |= JSON_OBJECT_AS_ARRAY;
|
||
|
$this->flags = $flags;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets the maximum recursion depth.
|
||
|
*
|
||
|
* @return int Maximum recursion depth.
|
||
|
*/
|
||
|
public function getMaxDepth(): int {
|
||
|
return $this->maxDepth;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets the flagset constructed by the constructor arguments.
|
||
|
*
|
||
|
* @return int JSON flagset.
|
||
|
*/
|
||
|
public function getFlags(): int {
|
||
|
return $this->flags;
|
||
|
}
|
||
|
}
|