spaghettisolutions /
xlsx-parser
| 1 | <?php declare(strict_types = 1); |
||
| 2 | |||
| 3 | namespace Spaghetti\XLSXParser; |
||
| 4 | |||
| 5 | use Iterator; |
||
| 6 | use XMLReader; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @internal |
||
| 10 | */ |
||
| 11 | final class RowIterator implements Iterator |
||
| 12 | { |
||
| 13 | private const ROW = 'row'; |
||
| 14 | private const ROW_INDEX = 'r'; |
||
| 15 | private const COLUMN = 'c'; |
||
| 16 | private const VALUE = 'v'; |
||
| 17 | private const STYLE = 's'; |
||
| 18 | private const TYPE = 't'; |
||
| 19 | |||
| 20 | private ?Row $row = null; |
||
| 21 | private XMLReader $xml; |
||
| 22 | private array $currentValue; |
||
| 23 | private bool $valid; |
||
| 24 | private int $currentKey; |
||
| 25 | private int $index; |
||
| 26 | private readonly Transformer\Column $columnTransformer; |
||
| 27 | private string $style; |
||
| 28 | private string $type; |
||
| 29 | |||
| 30 | public function __construct( |
||
| 31 | private readonly Transformer\Value $valueTransformer, |
||
| 32 | private readonly string $path, |
||
| 33 | ?Transformer\Column $columnTransformer = null, |
||
| 34 | ) { |
||
| 35 | $this->columnTransformer = $columnTransformer ?? new Transformer\Column(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 36 | } |
||
| 37 | |||
| 38 | public function current(): array |
||
| 39 | { |
||
| 40 | return $this->currentValue; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function key(): int |
||
| 44 | { |
||
| 45 | return $this->currentKey; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function next(): void |
||
| 49 | { |
||
| 50 | $this->valid = false; |
||
| 51 | |||
| 52 | while ($this->xml->read()) { |
||
| 53 | $this->processEndElement(); |
||
| 54 | |||
| 55 | if ($this->valid) { |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | $this->process(); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | public function rewind(): void |
||
| 64 | { |
||
| 65 | $xml = new XMLReader(); |
||
| 66 | |||
| 67 | $this->xml = false === $xml->open(uri: $this->path) ? null : $xml; |
||
| 68 | |||
| 69 | $this->next(); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function valid(): bool |
||
| 73 | { |
||
| 74 | return $this->valid; |
||
| 75 | } |
||
| 76 | |||
| 77 | private function processEndElement(): void |
||
| 78 | { |
||
| 79 | if (XMLReader::END_ELEMENT === $this->xml->nodeType) { |
||
| 80 | $this->processEndValue(); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | private function processEndValue(): void |
||
| 85 | { |
||
| 86 | if (self::ROW === $this->xml->name) { |
||
| 87 | $currentValue = $this->row?->getData(); |
||
| 88 | if ([] !== $currentValue) { |
||
| 89 | $this->currentValue = $currentValue; |
||
| 90 | $this->valid = true; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | private function process(): void |
||
| 96 | { |
||
| 97 | if (XMLReader::ELEMENT === $this->xml->nodeType) { |
||
| 98 | match ($this->xml->name) { |
||
| 99 | self::ROW => $this->processRow(), |
||
|
0 ignored issues
–
show
Are you sure the usage of
$this->processRow() targeting Spaghetti\XLSXParser\RowIterator::processRow() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 100 | self::COLUMN => $this->processColumn(), |
||
|
0 ignored issues
–
show
Are you sure the usage of
$this->processColumn() targeting Spaghetti\XLSXParser\RowIterator::processColumn() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 101 | self::VALUE => $this->processValue(), |
||
|
0 ignored issues
–
show
Are you sure the usage of
$this->processValue() targeting Spaghetti\XLSXParser\RowIterator::processValue() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 102 | default => $this->processDefault(), |
||
|
0 ignored issues
–
show
Are you sure the usage of
$this->processDefault() targeting Spaghetti\XLSXParser\RowIterator::processDefault() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 103 | }; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | private function processRow(): void |
||
| 108 | { |
||
| 109 | $this->currentKey = (int) $this->xml->getAttribute(name: self::ROW_INDEX); |
||
| 110 | $this->row = new Row(); |
||
| 111 | } |
||
| 112 | |||
| 113 | private function processColumn(): void |
||
| 114 | { |
||
| 115 | $this->index = $this->columnTransformer->transform(name: $this->xml->getAttribute(name: self::ROW_INDEX)); |
||
| 116 | $this->style = $this->xml->getAttribute(name: self::STYLE) ?? ''; |
||
| 117 | $this->type = $this->xml->getAttribute(name: self::TYPE) ?? ''; |
||
| 118 | } |
||
| 119 | |||
| 120 | private function processValue(): void |
||
| 121 | { |
||
| 122 | $this->row?->addValue(columnIndex: $this->index, value: $this->valueTransformer->transform(value: $this->xml->readString(), type: $this->type, style: $this->style), ); |
||
| 123 | } |
||
| 124 | |||
| 125 | private function processDefault(): void |
||
| 126 | { |
||
| 127 | $this->row?->addValue(columnIndex: $this->index, value: $this->xml->readString()); |
||
| 128 | } |
||
| 129 | } |
||
| 130 |