1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
<?php
/**
* Copyright (c) 2016 Bernardo van der Wal
* MIT License
*
* Refer to the LICENSE file for the full copyright notice.
*/
namespace OneOfZero\Json;
use Interop\Container\ContainerInterface;
use OneOfZero\Json\ContractResolvers\ContractResolverInterface;
use OneOfZero\Json\Enums\IncludeStrategy;
use OneOfZero\Json\Enums\OnMaxDepth;
use OneOfZero\Json\Enums\OnRecursion;
use OneOfZero\Json\Enums\ReferenceResolutionStrategy;
/**
* The Configuration class provides various options that allow you to control the behaviour of the serializer.
*/
class Configuration
{
/**
* @var MetaHintWhitelist $metaHintWhitelist
*/
private $metaHintWhitelist;
/**
* @var ConverterConfiguration $converters
*/
private $converters;
/**
* When enabled, a MissingTypeException will be thrown if the provided type hint or embedded type cannot be found.
* Otherwise the type information will be disregarded.
*
* @var bool $strictTypeResolution
*/
public $strictTypeResolution = false;
/**
* When enabled, type information will be embedded in serialized objects. This type information can be
*
* @var bool $embedTypeMetadata
*/
public $embedTypeMetadata = true;
/**
* Enable/disable pretty JSON printing.
*
* @var bool $prettyPrint
*/
public $prettyPrint = false;
/**
* Option flags that are passed to the internally used json_encode() and json_decode() functions.
*
* @var int $jsonEncodeOptions
*/
public $jsonEncodeOptions = 0;
/**
* Specifies whether members with null values should be included in serialization.
*
* @var bool $includeNullValues
*/
public $includeNullValues = false;
/**
* Specifies the maximum serialization depth for the internally used json_encode() and json_decode() functions.
*
* @var int $maxDepth
*/
public $maxDepth = 32;
/**
* Specifies the default strategy for resolving references when deserializing.
*
* @var int $defaultReferenceResolutionStrategy
*/
public $defaultReferenceResolutionStrategy = ReferenceResolutionStrategy::LAZY;
/**
* Specifies one or more kinds of members that will be automatically included during serialization.
*
* The value uses bit flags, so you may use the bitwise OR (|) to specify multiple member kinds.
*
* @var int $defaultMemberInclusionStrategy
*/
public $defaultMemberInclusionStrategy = IncludeStrategy::PUBLIC_PROPERTIES;
/**
* Specifies the default handling strategy that will be used when recursion is detected during serialization.
*
* @var int $defaultRecursionHandlingStrategy
*/
public $defaultRecursionHandlingStrategy = OnRecursion::THROW_EXCEPTION;
/**
* Specifies the default handling strategy that will be used when the maximum depth is reached.
*
* @var int $defaultMaxDepthHandlingStrategy
*/
public $defaultMaxDepthHandlingStrategy = OnMaxDepth::THROW_EXCEPTION;
/**
* Configures the contract resolver to use during serialization and deserialization.
*
* @var ContractResolverInterface|null $contractResolver
*/
public $contractResolver = null;
/**
* Configures the whitelist that should be used to determine which classes may be used as meta type hints during
* deserialization.
*
* @return MetaHintWhitelist
*/
public function getMetaHintWhitelist()
{
return $this->metaHintWhitelist;
}
/**
* Allows configuration of global and type-assigned converters.
*
* @return ConverterConfiguration
*/
public function getConverters()
{
return $this->converters;
}
/**
* @param MetaHintWhitelist $metaHintWhitelist
*/
public function setMetaHintWhitelist(MetaHintWhitelist $metaHintWhitelist)
{
$this->metaHintWhitelist = $metaHintWhitelist;
}
/**
* @param ConverterConfiguration $converters
*/
public function setConverters(ConverterConfiguration $converters)
{
$this->converters = $converters;
}
/**
* @param ContainerInterface|null $container
* @param bool $loadDefaultConverters
*/
public function __construct(ContainerInterface $container = null, $loadDefaultConverters = true)
{
$this->metaHintWhitelist = new MetaHintWhitelist();
$this->converters = new ConverterConfiguration($container, $loadDefaultConverters);
}
public function __clone()
{
$this->metaHintWhitelist = clone $this->metaHintWhitelist;
$this->converters = clone $this->converters;
}
/**
* Returns a hash for this configuration.
*
* @return string
*/
public function getHash()
{
return sha1(json_encode($this));
}
}