|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the DataImporter package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Loïc Sapone <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace IQ2i\DataImporter\Reader; |
|
15
|
|
|
|
|
16
|
|
|
class JsonReader implements ReaderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private readonly \SplFileInfo $file; |
|
19
|
|
|
|
|
20
|
|
|
private readonly \ArrayIterator $iterator; |
|
21
|
|
|
|
|
22
|
|
|
private int $index = 1; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct( |
|
25
|
|
|
string $filePath, |
|
26
|
|
|
private readonly ?string $dto = null, |
|
27
|
|
|
) { |
|
28
|
|
|
$this->file = new \SplFileInfo($filePath); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
if (!$this->file->isReadable()) { |
|
31
|
|
|
throw new \InvalidArgumentException('The file '.$this->file->getFilename().' is not readable.'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$array = \json_decode(\file_get_contents($this->file->getRealPath()), true, 512, \JSON_THROW_ON_ERROR); |
|
35
|
|
|
$this->iterator = new \ArrayIterator($array); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$this->rewind(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getDto(): ?string |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->dto; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function isDenormalizable(): bool |
|
46
|
|
|
{ |
|
47
|
|
|
return null !== $this->dto; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getFile(): \SplFileInfo |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->file; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function index(): mixed |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->index; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function current(): array |
|
61
|
|
|
{ |
|
62
|
|
|
if (!$this->valid()) { |
|
63
|
|
|
return []; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->iterator->current(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function next(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->iterator->next(); |
|
72
|
|
|
++$this->index; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function key(): mixed |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->iterator->key(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function valid(): bool |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->iterator->valid(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function rewind(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$this->iterator->rewind(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function count(): int |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->iterator->count(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|