-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonDecoder.php
40 lines (35 loc) · 887 Bytes
/
JsonDecoder.php
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
<?php declare( strict_types = 1 );
namespace CodeKandis\JsonCodec;
use CodeKandis\JsonErrorHandler\JsonErrorHandler;
use CodeKandis\JsonErrorHandler\JsonErrorHandlerInterface;
use CodeKandis\Types\BaseObject;
use Override;
use function json_decode;
/**
* Represents a JSON decoder.
* @package codekandis/json-codec
* @author Christian Ramelow <[email protected]>
*/
class JsonDecoder extends BaseObject implements JsonDecoderInterface
{
/**
* Stores the JSON error handler.
*/
private JsonErrorHandlerInterface $errorHandler;
/**
* @inheritDoc
*/
#[Override]
public function decode( string $value, ?int $options = null, int $recursionDepth = 512 ): mixed
{
$decodedValue = json_decode(
$value,
null,
$recursionDepth,
$options ?? 0
);
$this->errorHandler ??= new JsonErrorHandler();
$this->errorHandler->handle();
return $decodedValue;
}
}