Skip to content

Commit 2c72365

Browse files
committed
fix for php >=8.1
1 parent 845f01f commit 2c72365

File tree

5 files changed

+114
-7
lines changed

5 files changed

+114
-7
lines changed

composer.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@
3232
"avadim/fast-excel-writer": "^4.8",
3333
"avadim/fast-excel-reader": "^2.11"
3434
},
35-
"funding": [
36-
{
37-
"type": "other",
38-
"url": "https://www.paypal.me/VShemarov"
39-
}
40-
]
35+
"require-dev": {
36+
"phpunit/phpunit": "^9.0"
37+
}
4138
}

src/FastExcelTemplator/ExcelWriter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public function makeSheet(string $sheetName = null): SheetWriter
2929
*/
3030
public function replaceSheets($inputFile, $outputFile): bool
3131
{
32-
$result = $this->writer->_replaceSheets($inputFile, $outputFile);
32+
$relationShips = [
33+
'rel_id' => ['workbook' => 0],
34+
];
35+
36+
$result = $this->writer->_replaceSheets($inputFile, $outputFile, $relationShips);
3337
if ($result) {
3438
$zip = new \ZipArchive();
3539
if (!$zip->open($outputFile)) {

src/FastExcelTemplator/RowTemplate.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,30 @@ public function attribute($name): ?string
6868
return $this->attributes[$name] ?? null;
6969
}
7070

71+
#[\ReturnTypeWillChange]
7172
public function current()
7273
{
7374
return current($this->domCells);
7475
}
7576

77+
#[\ReturnTypeWillChange]
7678
public function key()
7779
{
7880
return key($this->domCells);
7981
}
8082

83+
#[\ReturnTypeWillChange]
8184
public function next()
8285
{
8386
return next($this->domCells);
8487
}
88+
89+
#[\ReturnTypeWillChange]
8590
public function rewind()
8691
{
8792
return reset($this->domCells);
8893
}
94+
8995
public function valid(): bool
9096
{
9197
return (bool)current($this->domCells);

tests/FastExcelTemplatorTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace avadim\FastExcelTemplator;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use avadim\FastExcelTemplator\Excel;
9+
10+
final class FastExcelTemplatorTest extends TestCase
11+
{
12+
const DEMO_DIR = __DIR__ . '/../demo/files/';
13+
14+
public function test01()
15+
{
16+
// =====================
17+
$tpl = self::DEMO_DIR . 'demo1-tpl.xlsx';
18+
$out = __DIR__ . '/demo1-out.xlsx';
19+
20+
$excel = Excel::template($tpl, $out);
21+
$sheet = $excel->sheet();
22+
23+
$fillData = [
24+
'{{COMPANY}}' => 'Comp Stock Shop',
25+
'{{ADDRESS}}' => '123 ABC Street',
26+
'{{CITY}}' => 'Peace City, TN',
27+
];
28+
$replaceData = ['{{BULK_QTY}}' => 12, '{{DATE}}' => date('m/d/Y')];
29+
$list = [
30+
[
31+
'number' => 'AA-8465-947563',
32+
'description' => 'NEC Mate Type ML PC-MK29MLZD1FWG (Corei5-3470S/2GB/250GB/Multi/No OF/Win 11 Home)',
33+
'price1' => 816,
34+
'price2' => 683,
35+
],
36+
[
37+
'number' => 'QR-7956-048914',
38+
'description' => 'Acer Aspire TC-1760-UA92 (Core i5-12400/12GB 3200MHz DDR4/512GB SSD/Win 11 Home)',
39+
'price1' => 414,
40+
'price2' => 339,
41+
],
42+
[
43+
'number' => 'BD-3966-285936',
44+
'description' => 'Dell Optiplex 7050 SFF (Core i7-7700/32GB DDR4/1TB SSD/Win 10 Pro/Renewed)',
45+
'price1' => 249,
46+
'price2' => 198,
47+
],
48+
];
49+
50+
$sheet
51+
->fill($fillData)
52+
->replace($replaceData)
53+
;
54+
$rowTemplate = $sheet->getRowTemplate(6);
55+
$count = 0;
56+
foreach ($list as $record) {
57+
$rowData = [
58+
// In the column A wil be written value from field 'number'
59+
'A' => $record['number'],
60+
// In the column B wil be written value from field 'description'
61+
'B' => $record['description'],
62+
// And so on...
63+
'C' => $record['price1'],
64+
'D' => $record['price2'],
65+
];
66+
if ($count++ === 0) {
67+
// First we replace the row 6 (this is template row)
68+
$sheet->replaceRow(6, $rowTemplate, $rowData);
69+
}
70+
else {
71+
// Then we add new rows after last touched
72+
$sheet->insertRowAfterLast($rowTemplate, $rowData);
73+
}
74+
}
75+
$excel->save();
76+
$this->assertTrue(is_file($out));
77+
78+
$excelReader = \avadim\FastExcelReader\Excel::open($out);
79+
$cells = $excelReader->readCells();
80+
81+
$this->assertEquals($fillData['{{COMPANY}}'], $cells['A1']);
82+
$this->assertEquals('*Bulk pricing applies to quantities of 12 or more', $cells['D3']);
83+
$this->assertEquals(249, $cells['C8']);
84+
85+
unlink($out);
86+
}
87+
88+
}
89+

tests/bootstrap.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
$vendorDir = __DIR__ . '/../../..';
4+
5+
if (file_exists($file = $vendorDir . '/autoload.php')) {
6+
require_once $file;
7+
} else if (file_exists($file = './vendor/autoload.php')) {
8+
require_once $file;
9+
} else {
10+
throw new \RuntimeException('Not found composer autoload');
11+
}

0 commit comments

Comments
 (0)