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
|
1 |
|
public function __construct( |
25
|
|
|
string $filePath, |
26
|
|
|
private readonly ?string $dto = null, |
27
|
|
|
) { |
28
|
1 |
|
$this->file = new \SplFileInfo($filePath); |
|
|
|
|
29
|
|
|
|
30
|
1 |
|
if (!$this->file->isReadable()) { |
31
|
|
|
throw new \InvalidArgumentException('The file '.$this->file->getFilename().' is not readable.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
$array = \json_decode(\file_get_contents($this->file->getRealPath()), true, 512, \JSON_THROW_ON_ERROR); |
35
|
1 |
|
$this->iterator = new \ArrayIterator($array); |
|
|
|
|
36
|
|
|
|
37
|
1 |
|
$this->rewind(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getDto(): ?string |
41
|
|
|
{ |
42
|
|
|
return $this->dto; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function isDenormalizable(): bool |
46
|
|
|
{ |
47
|
1 |
|
return null !== $this->dto; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function getFile(): \SplFileInfo |
51
|
|
|
{ |
52
|
1 |
|
return $this->file; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function index(): mixed |
56
|
|
|
{ |
57
|
1 |
|
return $this->index; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function current(): array |
61
|
|
|
{ |
62
|
1 |
|
if (!$this->valid()) { |
63
|
1 |
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return $this->iterator->current(); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function next(): void |
70
|
|
|
{ |
71
|
1 |
|
$this->iterator->next(); |
72
|
1 |
|
++$this->index; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function key(): mixed |
76
|
|
|
{ |
77
|
1 |
|
return $this->iterator->key(); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function valid(): bool |
81
|
|
|
{ |
82
|
1 |
|
return $this->iterator->valid(); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function rewind(): void |
86
|
|
|
{ |
87
|
1 |
|
$this->iterator->rewind(); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function count(): int |
91
|
|
|
{ |
92
|
1 |
|
return $this->iterator->count(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|