-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonReader.php
43 lines (33 loc) · 1012 Bytes
/
JsonReader.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
41
42
43
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Json;
/**
* Reads json files.
*
* @author Joshua Estes <[email protected]>
*/
class JsonReader
{
private readonly JsonDecoder $decoder;
public function __construct(JsonDecoder $decoder = null)
{
$this->decoder = $decoder ?? new JsonDecoder();
}
public function read(string $filename, bool $associative = null, int $depth = null, int $flags = null): array
{
if (!is_readable($filename)) {
throw new JsonException(sprintf('The file "%s" does not exist or cannot be read', $filename));
}
$decoder = $this->decoder;
if (null !== $associative) {
$decoder = $decoder->objectAsArray();
}
if (null !== $depth) {
$decoder = $decoder->withDepth($depth);
}
if (null !== $flags) {
$decoder = $decoder->withFlags($flags);
}
return $decoder->decode(file_get_contents($filename));
}
}