Skip to content

Commit

Permalink
Add openspout/openspout bridge as a replacement of box/spout (#86)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yann-eugone authored Jun 27, 2023
0 parents commit 701a50c
Show file tree
Hide file tree
Showing 25 changed files with 1,415 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.gitattributes export-ignore
.gitignore export-ignore
docs/ export-ignore
tests/ export-ignore
LICENSE export-ignore
*.md export-ignore
27 changes: 27 additions & 0 deletions .github/workflows/lockdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: 'Lock down Pull Requests'

on:
pull_request:
types: opened

jobs:
lockdown:
runs-on: ubuntu-latest
steps:
- uses: dessant/repo-lockdown@v2
with:
github-token: ${{ github.token }}
close-pr: true
lock-pr: true
pr-comment: >
Thanks for your pull request!
However, this repository does not accept pull requests,
see the README for details.
If you want to contribute,
you should instead open a pull request on the main repository:
https://github.com/yokai-php/batch-src
Thank you
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.phpunit.result.cache
/tests/.artifacts/
/vendor/
/composer.lock
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019 Yann Eugoné

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# box/spout bridge for Batch processing library

[![Latest Stable Version](https://img.shields.io/packagist/v/yokai/batch-openspout?style=flat-square)](https://packagist.org/packages/yokai/batch-openspout)
[![Downloads Monthly](https://img.shields.io/packagist/dm/yokai/batch-openspout?style=flat-square)](https://packagist.org/packages/yokai/batch-openspout)

[`openspout/openspout`](https://github.com/openspout/openspout) bridge for [Batch](https://github.com/yokai-php/batch) processing library.


## :warning: BETA

This library is following [semver](https://semver.org/).
However before we reach the first stable version (`v1.0.0`), we may decide to introduce **API changes in minor versions**.
This is why you should stick to a `v0.[minor].*` requirement !


# Installation

```
composer require yokai/batch-openspout
```


## Documentation

This package provides:

- a [item reader](docs/flat-file-item-reader.md) that read from CSV/XLSX/ODS files
- a [item writer](docs/flat-file-item-writer.md) that write to CSV/XLSX/ODS files


## Contribution

This package is a readonly split of a [larger repository](https://github.com/yokai-php/batch-src),
containing all tests and sources for all librairies of the batch universe.

Please feel free to open an [issue](https://github.com/yokai-php/batch-src/issues)
or a [pull request](https://github.com/yokai-php/batch-src/pulls)
in the [main repository](https://github.com/yokai-php/batch-src).

The library was originally created by [Yann Eugoné](https://github.com/yann-eugone).
See the list of [contributors](https://github.com/yokai-php/batch-src/contributors).


## License

This library is under MIT [LICENSE](LICENSE).
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "yokai/batch-openspout",
"description": "openspout/openspout bridge for yokai/batch",
"keywords": ["batch", "job", "reader", "writer", "flat", "csv", "xlsx", "ods"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Yann Eugoné",
"email": "eugone.yann@gmail.com"
}
],
"require": {
"php": "^8.0",
"openspout/openspout": "^4.0",
"yokai/batch": "^0.5.0"
},
"autoload": {
"psr-4": {
"Yokai\\Batch\\Bridge\\OpenSpout\\": "src/"
}
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/filesystem": "^5.0|^6.0"
},
"autoload-dev": {
"psr-4": {
"Yokai\\Batch\\Tests\\Bridge\\OpenSpout\\": "tests/"
}
}
}
48 changes: 48 additions & 0 deletions docs/flat-file-item-reader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Item reader with CSV/ODS/XLSX files

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

```php
<?php

use OpenSpout\Reader\CSV\Options as CSVOptions;
use OpenSpout\Reader\ODS\Options as ODSOptions;
use OpenSpout\Reader\XLSX\Options as XLSXOptions;
use Yokai\Batch\Bridge\OpenSpout\Reader\FlatFileReader;
use Yokai\Batch\Bridge\OpenSpout\Reader\HeaderStrategy;
use Yokai\Batch\Bridge\OpenSpout\Reader\SheetFilter;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;

// Read .xlsx file
// Every sheet will be read
// All lines will be read as simple array
new FlatFileReader(new StaticValueParameterAccessor('/path/to/file.xlsx'));

// Read .csv file
// The CSV delimiter and enclosure has been changed from default (respectively ',' & '"')
// Each lines will be read as simple array
$options = new CSVOptions();
$options->FIELD_DELIMITER = ';';
$options->FIELD_ENCLOSURE = '|';
new FlatFileReader(
new StaticValueParameterAccessor('/path/to/file.csv'),
$options,
null,
HeaderStrategy::none(),
);

// Read .ods file
// Only sheet named "Sheet name to read" will be read
// Each item will be an array_combine of first line as key and line as values
new FlatFileReader(
new StaticValueParameterAccessor('/path/to/file.ods'),
null,
SheetFilter::nameIs('Sheet name to read'),
HeaderStrategy::combine(),
);
```

## On the same subject

- [What is an item reader ?](https://github.com/yokai-php/batch/blob/0.x/docs/domain/item-job/item-reader.md)
50 changes: 50 additions & 0 deletions docs/flat-file-item-writer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Item writer with CSV/ODS/XLSX files

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

```php
<?php

use OpenSpout\Common\Entity\Style\Style;
use OpenSpout\Writer\CSV\Options as CSVOptions;
use OpenSpout\Writer\ODS\Options as ODSOptions;
use OpenSpout\Writer\XLSX\Options as XLSXOptions;
use Yokai\Batch\Bridge\OpenSpout\Writer\FlatFileWriter;
use Yokai\Batch\Bridge\OpenSpout\Writer\Options\CSVOptions;
use Yokai\Batch\Bridge\OpenSpout\Writer\Options\ODSOptions;
use Yokai\Batch\Bridge\OpenSpout\Writer\Options\XLSXOptions;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;

// Write items to .xlsx file
// That file will not contain a header line
new FlatFileWriter(new StaticValueParameterAccessor('/path/to/file.xlsx'));

// Write items to .csv file
// That file will not contain a header line
// The CSV delimiter and enclosure has been changed from default (respectively ',' & '"')
$options = new CSVOptions();
$options->FIELD_DELIMITER = ';';
$options->FIELD_ENCLOSURE = '|';
new FlatFileWriter(
new StaticValueParameterAccessor('/path/to/file.csv'),
$options,
);

// Write items to .ods file
// That file will contain a header line with : static | header | keys
// Change the sheet name data will be written
// Change the default style of each cell
$options = new ODSOptions();
$options->DEFAULT_ROW_STYLE = (new Style())->setFontBold();
new FlatFileWriter(
new StaticValueParameterAccessor('/path/to/file.ods'),
$options,
'The sheet name',
['static', 'header', 'keys'],
);
```

## On the same subject

- [What is an item writer ?](https://github.com/yokai-php/batch/blob/0.x/docs/domain/item-job/item-writer.md)
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
>
<php>
<env name="ARTIFACT_DIR" value="tests/.artifacts"/>
</php>

<testsuites>
<testsuite name="Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
</phpunit>
39 changes: 39 additions & 0 deletions src/Exception/InvalidRowSizeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Bridge\OpenSpout\Exception;

use Yokai\Batch\Exception\LogicException;

final class InvalidRowSizeException extends LogicException
{
public function __construct(
/**
* @var array<int, string>
*/
private array $headers,
/**
* @var array<int, string>
*/
private array $row,
) {
parent::__construct('Invalid row size');
}

/**
* @return array<int, string>
*/
public function getHeaders(): array
{
return $this->headers;
}

/**
* @return array<int, string>
*/
public function getRow(): array
{
return $this->row;
}
}
Loading

0 comments on commit 701a50c

Please sign in to comment.