Skip to content

Commit 7f5edce

Browse files
Add ArrayAccess to Row (SpartnerNL#2818)
1 parent b814525 commit 7f5edce

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Row.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Maatwebsite\Excel;
44

5+
use ArrayAccess;
56
use Illuminate\Support\Collection;
67
use PhpOffice\PhpSpreadsheet\Worksheet\Row as SpreadsheetRow;
78

8-
class Row
9+
class Row implements ArrayAccess
910
{
1011
use DelegatedMacroable;
1112

@@ -17,7 +18,12 @@ class Row
1718
/**
1819
* @var SpreadsheetRow
1920
*/
20-
private $row;
21+
protected $row;
22+
23+
/**
24+
* @var array|null
25+
*/
26+
protected $rowCache;
2127

2228
/**
2329
* @param SpreadsheetRow $row
@@ -61,6 +67,10 @@ public function toCollection($nullValue = null, $calculateFormulas = false, $for
6167
*/
6268
public function toArray($nullValue = null, $calculateFormulas = false, $formatData = true, ?string $endColumn = null)
6369
{
70+
if (is_array($this->rowCache)) {
71+
return $this->rowCache;
72+
}
73+
6474
$cells = [];
6575

6676
$i = 0;
@@ -76,6 +86,8 @@ public function toArray($nullValue = null, $calculateFormulas = false, $formatDa
7686
$i++;
7787
}
7888

89+
$this->rowCache = $cells;
90+
7991
return $cells;
8092
}
8193

@@ -86,4 +98,24 @@ public function getIndex(): int
8698
{
8799
return $this->row->getRowIndex();
88100
}
101+
102+
public function offsetExists($offset)
103+
{
104+
return isset(($this->toArray())[$offset]);
105+
}
106+
107+
public function offsetGet($offset)
108+
{
109+
return ($this->toArray())[$offset];
110+
}
111+
112+
public function offsetSet($offset, $value)
113+
{
114+
//
115+
}
116+
117+
public function offsetUnset($offset)
118+
{
119+
//
120+
}
89121
}

tests/Concerns/OnEachRowTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function onRow(Row $row)
3333
'test', 'test',
3434
], $row->toArray());
3535

36+
Assert::assertEquals('test', $row[0]);
37+
3638
$this->called++;
3739
}
3840
};

0 commit comments

Comments
 (0)