Skip to content

Commit 701a50c

Browse files
authored
Add openspout/openspout bridge as a replacement of box/spout (#86)
* Add openspout/openspout bridge as a replacement of box/spout * Fix phpstan * Fix checkstyle * Re-add ability to set the sheet name during flat file write * Update docs * Fixed tests
0 parents  commit 701a50c

25 files changed

+1415
-0
lines changed

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.gitattributes export-ignore
2+
.gitignore export-ignore
3+
docs/ export-ignore
4+
tests/ export-ignore
5+
LICENSE export-ignore
6+
*.md export-ignore

.github/workflows/lockdown.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: 'Lock down Pull Requests'
2+
3+
on:
4+
pull_request:
5+
types: opened
6+
7+
jobs:
8+
lockdown:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: dessant/repo-lockdown@v2
12+
with:
13+
github-token: ${{ github.token }}
14+
close-pr: true
15+
lock-pr: true
16+
pr-comment: >
17+
Thanks for your pull request!
18+
19+
However, this repository does not accept pull requests,
20+
see the README for details.
21+
22+
If you want to contribute,
23+
you should instead open a pull request on the main repository:
24+
25+
https://github.com/yokai-php/batch-src
26+
27+
Thank you

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.phpunit.result.cache
2+
/tests/.artifacts/
3+
/vendor/
4+
/composer.lock

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019 Yann Eugoné
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# box/spout bridge for Batch processing library
2+
3+
[![Latest Stable Version](https://img.shields.io/packagist/v/yokai/batch-openspout?style=flat-square)](https://packagist.org/packages/yokai/batch-openspout)
4+
[![Downloads Monthly](https://img.shields.io/packagist/dm/yokai/batch-openspout?style=flat-square)](https://packagist.org/packages/yokai/batch-openspout)
5+
6+
[`openspout/openspout`](https://github.com/openspout/openspout) bridge for [Batch](https://github.com/yokai-php/batch) processing library.
7+
8+
9+
## :warning: BETA
10+
11+
This library is following [semver](https://semver.org/).
12+
However before we reach the first stable version (`v1.0.0`), we may decide to introduce **API changes in minor versions**.
13+
This is why you should stick to a `v0.[minor].*` requirement !
14+
15+
16+
# Installation
17+
18+
```
19+
composer require yokai/batch-openspout
20+
```
21+
22+
23+
## Documentation
24+
25+
This package provides:
26+
27+
- a [item reader](docs/flat-file-item-reader.md) that read from CSV/XLSX/ODS files
28+
- a [item writer](docs/flat-file-item-writer.md) that write to CSV/XLSX/ODS files
29+
30+
31+
## Contribution
32+
33+
This package is a readonly split of a [larger repository](https://github.com/yokai-php/batch-src),
34+
containing all tests and sources for all librairies of the batch universe.
35+
36+
Please feel free to open an [issue](https://github.com/yokai-php/batch-src/issues)
37+
or a [pull request](https://github.com/yokai-php/batch-src/pulls)
38+
in the [main repository](https://github.com/yokai-php/batch-src).
39+
40+
The library was originally created by [Yann Eugoné](https://github.com/yann-eugone).
41+
See the list of [contributors](https://github.com/yokai-php/batch-src/contributors).
42+
43+
44+
## License
45+
46+
This library is under MIT [LICENSE](LICENSE).

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "yokai/batch-openspout",
3+
"description": "openspout/openspout bridge for yokai/batch",
4+
"keywords": ["batch", "job", "reader", "writer", "flat", "csv", "xlsx", "ods"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Yann Eugoné",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": "^8.0",
15+
"openspout/openspout": "^4.0",
16+
"yokai/batch": "^0.5.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Yokai\\Batch\\Bridge\\OpenSpout\\": "src/"
21+
}
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^9.5",
25+
"symfony/filesystem": "^5.0|^6.0"
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Yokai\\Batch\\Tests\\Bridge\\OpenSpout\\": "tests/"
30+
}
31+
}
32+
}

docs/flat-file-item-reader.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Item reader with CSV/ODS/XLSX files
2+
3+
The [FlatFileReader](../src/Reader/FlatFileReader.php) is a reader
4+
that will read from CSV/ODS/XLSX file and return each line as an array.
5+
6+
```php
7+
<?php
8+
9+
use OpenSpout\Reader\CSV\Options as CSVOptions;
10+
use OpenSpout\Reader\ODS\Options as ODSOptions;
11+
use OpenSpout\Reader\XLSX\Options as XLSXOptions;
12+
use Yokai\Batch\Bridge\OpenSpout\Reader\FlatFileReader;
13+
use Yokai\Batch\Bridge\OpenSpout\Reader\HeaderStrategy;
14+
use Yokai\Batch\Bridge\OpenSpout\Reader\SheetFilter;
15+
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
16+
17+
// Read .xlsx file
18+
// Every sheet will be read
19+
// All lines will be read as simple array
20+
new FlatFileReader(new StaticValueParameterAccessor('/path/to/file.xlsx'));
21+
22+
// Read .csv file
23+
// The CSV delimiter and enclosure has been changed from default (respectively ',' & '"')
24+
// Each lines will be read as simple array
25+
$options = new CSVOptions();
26+
$options->FIELD_DELIMITER = ';';
27+
$options->FIELD_ENCLOSURE = '|';
28+
new FlatFileReader(
29+
new StaticValueParameterAccessor('/path/to/file.csv'),
30+
$options,
31+
null,
32+
HeaderStrategy::none(),
33+
);
34+
35+
// Read .ods file
36+
// Only sheet named "Sheet name to read" will be read
37+
// Each item will be an array_combine of first line as key and line as values
38+
new FlatFileReader(
39+
new StaticValueParameterAccessor('/path/to/file.ods'),
40+
null,
41+
SheetFilter::nameIs('Sheet name to read'),
42+
HeaderStrategy::combine(),
43+
);
44+
```
45+
46+
## On the same subject
47+
48+
- [What is an item reader ?](https://github.com/yokai-php/batch/blob/0.x/docs/domain/item-job/item-reader.md)

docs/flat-file-item-writer.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Item writer with CSV/ODS/XLSX files
2+
3+
The [FlatFileWriter](../src/Writer/FlatFileWriter.php) is a writer that will write to CSV/ODS/XLSX file and each item will
4+
written its own line.
5+
6+
```php
7+
<?php
8+
9+
use OpenSpout\Common\Entity\Style\Style;
10+
use OpenSpout\Writer\CSV\Options as CSVOptions;
11+
use OpenSpout\Writer\ODS\Options as ODSOptions;
12+
use OpenSpout\Writer\XLSX\Options as XLSXOptions;
13+
use Yokai\Batch\Bridge\OpenSpout\Writer\FlatFileWriter;
14+
use Yokai\Batch\Bridge\OpenSpout\Writer\Options\CSVOptions;
15+
use Yokai\Batch\Bridge\OpenSpout\Writer\Options\ODSOptions;
16+
use Yokai\Batch\Bridge\OpenSpout\Writer\Options\XLSXOptions;
17+
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
18+
19+
// Write items to .xlsx file
20+
// That file will not contain a header line
21+
new FlatFileWriter(new StaticValueParameterAccessor('/path/to/file.xlsx'));
22+
23+
// Write items to .csv file
24+
// That file will not contain a header line
25+
// The CSV delimiter and enclosure has been changed from default (respectively ',' & '"')
26+
$options = new CSVOptions();
27+
$options->FIELD_DELIMITER = ';';
28+
$options->FIELD_ENCLOSURE = '|';
29+
new FlatFileWriter(
30+
new StaticValueParameterAccessor('/path/to/file.csv'),
31+
$options,
32+
);
33+
34+
// Write items to .ods file
35+
// That file will contain a header line with : static | header | keys
36+
// Change the sheet name data will be written
37+
// Change the default style of each cell
38+
$options = new ODSOptions();
39+
$options->DEFAULT_ROW_STYLE = (new Style())->setFontBold();
40+
new FlatFileWriter(
41+
new StaticValueParameterAccessor('/path/to/file.ods'),
42+
$options,
43+
'The sheet name',
44+
['static', 'header', 'keys'],
45+
);
46+
```
47+
48+
## On the same subject
49+
50+
- [What is an item writer ?](https://github.com/yokai-php/batch/blob/0.x/docs/domain/item-job/item-writer.md)

phpunit.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
bootstrap="tests/bootstrap.php"
9+
>
10+
<php>
11+
<env name="ARTIFACT_DIR" value="tests/.artifacts"/>
12+
</php>
13+
14+
<testsuites>
15+
<testsuite name="Tests">
16+
<directory>./tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<coverage>
21+
<include>
22+
<directory>./src</directory>
23+
</include>
24+
</coverage>
25+
</phpunit>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Bridge\OpenSpout\Exception;
6+
7+
use Yokai\Batch\Exception\LogicException;
8+
9+
final class InvalidRowSizeException extends LogicException
10+
{
11+
public function __construct(
12+
/**
13+
* @var array<int, string>
14+
*/
15+
private array $headers,
16+
/**
17+
* @var array<int, string>
18+
*/
19+
private array $row,
20+
) {
21+
parent::__construct('Invalid row size');
22+
}
23+
24+
/**
25+
* @return array<int, string>
26+
*/
27+
public function getHeaders(): array
28+
{
29+
return $this->headers;
30+
}
31+
32+
/**
33+
* @return array<int, string>
34+
*/
35+
public function getRow(): array
36+
{
37+
return $this->row;
38+
}
39+
}

0 commit comments

Comments
 (0)