Skip to content

Commit

Permalink
fix for php >=8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Nov 3, 2023
1 parent 845f01f commit 2c72365
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 7 deletions.
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
"avadim/fast-excel-writer": "^4.8",
"avadim/fast-excel-reader": "^2.11"
},
"funding": [
{
"type": "other",
"url": "https://www.paypal.me/VShemarov"
}
]
"require-dev": {
"phpunit/phpunit": "^9.0"
}
}
6 changes: 5 additions & 1 deletion src/FastExcelTemplator/ExcelWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public function makeSheet(string $sheetName = null): SheetWriter
*/
public function replaceSheets($inputFile, $outputFile): bool
{
$result = $this->writer->_replaceSheets($inputFile, $outputFile);
$relationShips = [
'rel_id' => ['workbook' => 0],
];

$result = $this->writer->_replaceSheets($inputFile, $outputFile, $relationShips);
if ($result) {
$zip = new \ZipArchive();
if (!$zip->open($outputFile)) {
Expand Down
6 changes: 6 additions & 0 deletions src/FastExcelTemplator/RowTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,30 @@ public function attribute($name): ?string
return $this->attributes[$name] ?? null;
}

#[\ReturnTypeWillChange]
public function current()
{
return current($this->domCells);
}

#[\ReturnTypeWillChange]
public function key()
{
return key($this->domCells);
}

#[\ReturnTypeWillChange]
public function next()
{
return next($this->domCells);
}

#[\ReturnTypeWillChange]
public function rewind()
{
return reset($this->domCells);
}

public function valid(): bool
{
return (bool)current($this->domCells);
Expand Down
89 changes: 89 additions & 0 deletions tests/FastExcelTemplatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace avadim\FastExcelTemplator;

use PHPUnit\Framework\TestCase;
use avadim\FastExcelTemplator\Excel;

final class FastExcelTemplatorTest extends TestCase
{
const DEMO_DIR = __DIR__ . '/../demo/files/';

public function test01()
{
// =====================
$tpl = self::DEMO_DIR . 'demo1-tpl.xlsx';
$out = __DIR__ . '/demo1-out.xlsx';

$excel = Excel::template($tpl, $out);
$sheet = $excel->sheet();

$fillData = [
'{{COMPANY}}' => 'Comp Stock Shop',
'{{ADDRESS}}' => '123 ABC Street',
'{{CITY}}' => 'Peace City, TN',
];
$replaceData = ['{{BULK_QTY}}' => 12, '{{DATE}}' => date('m/d/Y')];
$list = [
[
'number' => 'AA-8465-947563',
'description' => 'NEC Mate Type ML PC-MK29MLZD1FWG (Corei5-3470S/2GB/250GB/Multi/No OF/Win 11 Home)',
'price1' => 816,
'price2' => 683,
],
[
'number' => 'QR-7956-048914',
'description' => 'Acer Aspire TC-1760-UA92 (Core i5-12400/12GB 3200MHz DDR4/512GB SSD/Win 11 Home)',
'price1' => 414,
'price2' => 339,
],
[
'number' => 'BD-3966-285936',
'description' => 'Dell Optiplex 7050 SFF (Core i7-7700/32GB DDR4/1TB SSD/Win 10 Pro/Renewed)',
'price1' => 249,
'price2' => 198,
],
];

$sheet
->fill($fillData)
->replace($replaceData)
;
$rowTemplate = $sheet->getRowTemplate(6);
$count = 0;
foreach ($list as $record) {
$rowData = [
// In the column A wil be written value from field 'number'
'A' => $record['number'],
// In the column B wil be written value from field 'description'
'B' => $record['description'],
// And so on...
'C' => $record['price1'],
'D' => $record['price2'],
];
if ($count++ === 0) {
// First we replace the row 6 (this is template row)
$sheet->replaceRow(6, $rowTemplate, $rowData);
}
else {
// Then we add new rows after last touched
$sheet->insertRowAfterLast($rowTemplate, $rowData);
}
}
$excel->save();
$this->assertTrue(is_file($out));

$excelReader = \avadim\FastExcelReader\Excel::open($out);
$cells = $excelReader->readCells();

$this->assertEquals($fillData['{{COMPANY}}'], $cells['A1']);
$this->assertEquals('*Bulk pricing applies to quantities of 12 or more', $cells['D3']);
$this->assertEquals(249, $cells['C8']);

unlink($out);
}

}

11 changes: 11 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$vendorDir = __DIR__ . '/../../..';

if (file_exists($file = $vendorDir . '/autoload.php')) {
require_once $file;
} else if (file_exists($file = './vendor/autoload.php')) {
require_once $file;
} else {
throw new \RuntimeException('Not found composer autoload');
}

0 comments on commit 2c72365

Please sign in to comment.