Skip to content

Commit 654adc4

Browse files
authored
Refactor box/spout bridge for a class detailed structure (#50)
* Refactor box/spout FlatFileReader for a more flexible class architecture * Refactor box/spout FlatFileWriter for a more flexible class architecture * Allow FlatFileWriter to write array and Row entity from box/spout library * Fixed flat file writing on multiple sheets * Fixed code style & static analysis * Add missing doc for SheetFilter
1 parent d27a214 commit 654adc4

36 files changed

+1519
-786
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Cannot cast mixed to string\\.$#"
5-
count: 1
6-
path: src/batch-box-spout/src/FlatFileReader.php
7-
8-
-
9-
message: "#^Method Yokai\\\\Batch\\\\Bridge\\\\Box\\\\Spout\\\\FlatFileReader\\:\\:combine\\(\\) should return array\\<string, string\\>\\|null but returns array\\<string, mixed\\>\\.$#"
10-
count: 1
11-
path: src/batch-box-spout/src/FlatFileReader.php
12-
13-
-
14-
message: "#^Method Yokai\\\\Batch\\\\Bridge\\\\Box\\\\Spout\\\\FlatFileReader\\:\\:rows\\(\\) return type has no value type specified in iterable type array\\.$#"
15-
count: 1
16-
path: src/batch-box-spout/src/FlatFileReader.php
17-
18-
-
19-
message: "#^Offset 'encoding' on array\\{delimiter\\?\\: string, enclosure\\?\\: string\\} in isset\\(\\) does not exist\\.$#"
20-
count: 1
21-
path: src/batch-box-spout/src/FlatFileReader.php
22-
23-
-
24-
message: "#^Cannot cast mixed to string\\.$#"
25-
count: 1
26-
path: src/batch-box-spout/src/FlatFileWriter.php
27-
283
-
294
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
305
count: 2

src/batch-box-spout/docs/flat-file-item-reader.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
# Item reader with CSV/ODS/XLSX files
22

3-
The [FlatFileReader](../src/FlatFileReader.php) is a reader
3+
The [FlatFileReader](../src/Reader/FlatFileReader.php) is a reader
44
that will read from CSV/ODS/XLSX file and return each line as an array.
55

66
```php
77
<?php
88

9-
use Box\Spout\Common\Type;
10-
use Yokai\Batch\Bridge\Box\Spout\FlatFileReader;
9+
use Yokai\Batch\Bridge\Box\Spout\Reader\FlatFileReader;
10+
use Yokai\Batch\Bridge\Box\Spout\Reader\HeaderStrategy;
11+
use Yokai\Batch\Bridge\Box\Spout\Reader\Options\CSVOptions;
12+
use Yokai\Batch\Bridge\Box\Spout\Reader\Options\ODSOptions;
13+
use Yokai\Batch\Bridge\Box\Spout\Reader\Options\SheetFilter;
14+
use Yokai\Batch\Bridge\Box\Spout\Reader\Options\XLSXOptions;
1115
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
1216

1317
// Read .xlsx file
14-
// Each item will be an array_combine of first line as key and line as values
15-
new FlatFileReader(Type::XLSX, new StaticValueParameterAccessor('/path/to/file.xlsx'));
18+
// Every sheet will be read
19+
// First line of each sheet will be skipped
20+
// Other lines will be read as simple array
21+
new FlatFileReader(new StaticValueParameterAccessor('/path/to/file.xlsx'), new XLSXOptions());
1622

1723
// Read .csv file
18-
// Each item will be an array_combine of first line as key and line as values
1924
// The CSV delimiter and enclosure has been changed from default (respectively ',' & '"')
20-
new FlatFileReader(Type::CSV, new StaticValueParameterAccessor('/path/to/file.csv'), ['delimiter' => ';', 'enclosure' => '|']);
25+
// Each lines will be read as simple array
26+
new FlatFileReader(
27+
new StaticValueParameterAccessor('/path/to/file.csv'),
28+
new CSVOptions(';', '|'),
29+
HeaderStrategy::none()
30+
);
2131

2232
// Read .ods file
23-
// Each item will be an array_combine of headers constructor arg as key and line as values
24-
new FlatFileReader(Type::ODS, new StaticValueParameterAccessor('/path/to/file.ods'), [], FlatFileReader::HEADERS_MODE_SKIP, ['static', 'header', 'keys']);
33+
// Only sheet named "Sheet name to read" will be read
34+
// Each item will be an array_combine of first line as key and line as values
35+
new FlatFileReader(
36+
new StaticValueParameterAccessor('/path/to/file.ods'),
37+
new ODSOptions(SheetFilter::nameIs('Sheet name to read')),
38+
HeaderStrategy::combine()
39+
);
2540
```
2641

2742
## On the same subject

src/batch-box-spout/docs/flat-file-item-writer.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
# Item writer with CSV/ODS/XLSX files
22

3-
The [FlatFileWriter](../src/FlatFileWriter.php) is a writer that will write to CSV/ODS/XLSX file and each item will
3+
The [FlatFileWriter](../src/Writer/FlatFileWriter.php) is a writer that will write to CSV/ODS/XLSX file and each item will
44
written its own line.
55

66
```php
77
<?php
88

9-
use Box\Spout\Common\Type;
10-
use Yokai\Batch\Bridge\Box\Spout\FlatFileWriter;
9+
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
10+
use Yokai\Batch\Bridge\Box\Spout\Writer\FlatFileWriter;
11+
use Yokai\Batch\Bridge\Box\Spout\Writer\Options\CSVOptions;
12+
use Yokai\Batch\Bridge\Box\Spout\Writer\Options\ODSOptions;
13+
use Yokai\Batch\Bridge\Box\Spout\Writer\Options\XLSXOptions;
1114
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
1215

1316
// Write items to .xlsx file
14-
// That File will not contain a header line
15-
new FlatFileWriter(Type::XLSX, new StaticValueParameterAccessor('/path/to/file.xlsx'));
17+
// That file will not contain a header line
18+
new FlatFileWriter(new StaticValueParameterAccessor('/path/to/file.xlsx'), new XLSXOptions());
1619

1720
// Write items to .csv file
18-
// That File will not contain a header line
21+
// That file will not contain a header line
1922
// The CSV delimiter and enclosure has been changed from default (respectively ',' & '"')
20-
new FlatFileWriter(Type::CSV, new StaticValueParameterAccessor('/path/to/file.csv'), [], ['delimiter' => ';', 'enclosure' => '|']);
23+
new FlatFileWriter(
24+
new StaticValueParameterAccessor('/path/to/file.csv'),
25+
new CSVOptions(';', '|')
26+
);
2127

2228
// Write items to .ods file
23-
// That File will contain a header line with : static | header | keys
24-
new FlatFileWriter(Type::ODS, new StaticValueParameterAccessor('/path/to/file.ods'), ['static', 'header', 'keys']);
29+
// That file will contain a header line with : static | header | keys
30+
// Change the sheet name data will be written
31+
// Change the default style of each cell
32+
new FlatFileWriter(
33+
new StaticValueParameterAccessor('/path/to/file.ods'),
34+
new ODSOptions('The sheet name', (new StyleBuilder())->setFontBold()->build()),
35+
['static', 'header', 'keys']
36+
);
2537
```
2638

2739
## On the same subject
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Bridge\Box\Spout\Exception;
6+
7+
use Throwable;
8+
use Yokai\Batch\Exception\LogicException;
9+
10+
final class InvalidRowSizeException extends LogicException
11+
{
12+
/**
13+
* @phpstan-var array<int, string>
14+
*/
15+
private array $headers;
16+
/**
17+
* @phpstan-var array<int, string>
18+
*/
19+
private array $row;
20+
21+
/**
22+
* @phpstan-param array<int, string> $headers
23+
* @phpstan-param array<int, string> $row
24+
*/
25+
public function __construct(array $headers, array $row)
26+
{
27+
parent::__construct('Invalid row size');
28+
$this->headers = $headers;
29+
$this->row = $row;
30+
}
31+
32+
/**
33+
* @phpstan-return array<int, string>
34+
*/
35+
public function getHeaders(): array
36+
{
37+
return $this->headers;
38+
}
39+
40+
/**
41+
* @phpstan-return array<int, string>
42+
*/
43+
public function getRow(): array
44+
{
45+
return $this->row;
46+
}
47+
}

src/batch-box-spout/src/FlatFileReader.php

Lines changed: 0 additions & 186 deletions
This file was deleted.

0 commit comments

Comments
 (0)