Skip to content

Commit ce78c7f

Browse files
Added an ability to prepare rows before appending (SpartnerNL#2892)
* added an ability to prepare rows before appinding * added test * revert composer * changed test * changed collection to array * updated changelog file * revert phpunit
1 parent 8b267b4 commit ce78c7f

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
55

6+
### Added
7+
8+
- Added an ability to prepare rows before appending rows to sheet. Just add `prepareRows` method for your export class if needed.
9+
610
## [3.1.24] - 2020-10-28
711

812
### Added

src/Sheet.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,10 @@ public function hasConcern(string $concern): string
586586
*/
587587
public function appendRows($rows, $sheetExport)
588588
{
589+
if (method_exists($sheetExport, 'prepareRows')) {
590+
$rows = $sheetExport->prepareRows($rows);
591+
}
592+
589593
$rows = (new Collection($rows))->flatMap(function ($row) use ($sheetExport) {
590594
if ($sheetExport instanceof WithMapping) {
591595
$row = $sheetExport->map($row);

tests/Concerns/FromQueryTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
use Illuminate\Foundation\Testing\DatabaseTransactions;
66
use Illuminate\Support\Facades\DB;
7+
use Maatwebsite\Excel\Tests\TestCase;
78
use Maatwebsite\Excel\Tests\Data\Stubs\Database\Group;
89
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
910
use Maatwebsite\Excel\Tests\Data\Stubs\FromGroupUsersQueuedQueryExport;
1011
use Maatwebsite\Excel\Tests\Data\Stubs\FromNestedArraysQueryExport;
1112
use Maatwebsite\Excel\Tests\Data\Stubs\FromNonEloquentQueryExport;
1213
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryExport;
1314
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryExportWithEagerLoad;
14-
use Maatwebsite\Excel\Tests\TestCase;
15+
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryExportWithPrepareRows;
1516

1617
class FromQueryTest extends TestCase
1718
{
@@ -245,4 +246,28 @@ protected function format_nested_arrays_expected_data($groups)
245246

246247
return $expected;
247248
}
249+
250+
/**
251+
* @test
252+
*/
253+
public function can_export_from_query_with_prepare_rows()
254+
{
255+
$export = new FromUsersQueryExportWithPrepareRows;
256+
257+
$this->assertTrue(method_exists($export, 'prepareRows'));
258+
259+
$response = $export->store('from-query-store.xlsx');
260+
261+
$this->assertTrue($response);
262+
263+
$contents = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-query-store.xlsx', 'Xlsx');
264+
265+
$allUsers = $export->query()->get()->map(function (User $user) {
266+
$user->name .= '_prepared_name';
267+
268+
return array_values($user->toArray());
269+
})->toArray();
270+
271+
$this->assertEquals($allUsers, $contents);
272+
}
248273
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Maatwebsite\Excel\Tests\Data\Stubs;
4+
5+
use Illuminate\Database\Query\Builder;
6+
use Illuminate\Support\Collection;
7+
use Maatwebsite\Excel\Concerns\FromQuery;
8+
use Maatwebsite\Excel\Concerns\Exportable;
9+
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
10+
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
11+
12+
class FromUsersQueryExportWithPrepareRows implements FromQuery, WithCustomChunkSize
13+
{
14+
use Exportable;
15+
16+
/**
17+
* @return Builder
18+
*/
19+
public function query()
20+
{
21+
return User::query();
22+
}
23+
24+
/**
25+
* @return int
26+
*/
27+
public function chunkSize(): int
28+
{
29+
return 10;
30+
}
31+
32+
/**
33+
* @param iterable $rows
34+
* @return iterable
35+
*/
36+
public function prepareRows($rows)
37+
{
38+
return (new Collection($rows))->map(function ($user) {
39+
$user->name .= '_prepared_name';
40+
41+
return $user;
42+
})->toArray();
43+
}
44+
}

0 commit comments

Comments
 (0)