| 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 | use JsonMachine\Items; |
||
| 17 | use JsonMachine\JsonDecoder\ExtJsonDecoder; |
||
| 18 | |||
| 19 | class JsonReader implements ReaderInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | final public const POINTER = 'pointer'; |
||
| 25 | |||
| 26 | private readonly \SplFileInfo $file; |
||
| 27 | |||
| 28 | private readonly \Iterator $iterator; |
||
| 29 | |||
| 30 | private int $index = 1; |
||
| 31 | |||
| 32 | private int $count = 0; |
||
| 33 | |||
| 34 | private array $defaultContext = [ |
||
| 35 | self::POINTER => null, |
||
| 36 | ]; |
||
| 37 | |||
| 38 | 2 | public function __construct( |
|
| 39 | string $filePath, |
||
| 40 | private readonly ?string $dto = null, |
||
| 41 | array $defaultContext = [], |
||
| 42 | ) { |
||
| 43 | 2 | $this->file = new \SplFileInfo($filePath); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 44 | |||
| 45 | 2 | if (!$this->file->isReadable()) { |
|
| 46 | throw new \InvalidArgumentException('The file '.$this->file->getFilename().' is not readable.'); |
||
| 47 | } |
||
| 48 | |||
| 49 | 2 | $this->defaultContext = \array_merge($this->defaultContext, $defaultContext); |
|
| 50 | 2 | $options = [ |
|
| 51 | 2 | 'decoder' => new ExtJsonDecoder(true), |
|
| 52 | 2 | ]; |
|
| 53 | |||
| 54 | 2 | if (null !== $this->defaultContext[self::POINTER]) { |
|
| 55 | 1 | $options['pointer'] = $this->defaultContext[self::POINTER]; |
|
| 56 | } |
||
| 57 | |||
| 58 | 2 | $this->count = \iterator_count(Items::fromFile($this->file->getRealPath(), $options)); |
|
| 59 | 2 | $this->iterator = Items::fromFile($this->file->getRealPath(), $options)->getIterator(); |
|
|
0 ignored issues
–
show
|
|||
| 60 | |||
| 61 | 2 | $this->rewind(); |
|
| 62 | } |
||
| 63 | |||
| 64 | public function getDto(): ?string |
||
| 65 | { |
||
| 66 | return $this->dto; |
||
| 67 | } |
||
| 68 | |||
| 69 | 2 | public function isDenormalizable(): bool |
|
| 70 | { |
||
| 71 | 2 | return null !== $this->dto; |
|
| 72 | } |
||
| 73 | |||
| 74 | 2 | public function getFile(): \SplFileInfo |
|
| 75 | { |
||
| 76 | 2 | return $this->file; |
|
| 77 | } |
||
| 78 | |||
| 79 | 2 | public function index(): mixed |
|
| 80 | { |
||
| 81 | 2 | return $this->index; |
|
| 82 | } |
||
| 83 | |||
| 84 | 2 | public function current(): array |
|
| 85 | { |
||
| 86 | 2 | if (!$this->valid()) { |
|
| 87 | 2 | return []; |
|
| 88 | } |
||
| 89 | |||
| 90 | 2 | return $this->iterator->current(); |
|
| 91 | } |
||
| 92 | |||
| 93 | 2 | public function next(): void |
|
| 94 | { |
||
| 95 | 2 | $this->iterator->next(); |
|
| 96 | 2 | ++$this->index; |
|
| 97 | } |
||
| 98 | |||
| 99 | 2 | public function key(): mixed |
|
| 100 | { |
||
| 101 | 2 | return $this->iterator->key(); |
|
| 102 | } |
||
| 103 | |||
| 104 | 2 | public function valid(): bool |
|
| 105 | { |
||
| 106 | 2 | return $this->iterator->valid(); |
|
| 107 | } |
||
| 108 | |||
| 109 | 2 | public function rewind(): void |
|
| 110 | { |
||
| 111 | 2 | $this->iterator->rewind(); |
|
| 112 | } |
||
| 113 | |||
| 114 | 2 | public function count(): int |
|
| 115 | { |
||
| 116 | 2 | return $this->count; |
|
| 117 | } |
||
| 118 | } |
||
| 119 |