Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Cannot cast mixed to string\\.$#"
count: 1
path: src/batch-box-spout/src/FlatFileReader.php

-
message: "#^Method Yokai\\\\Batch\\\\Bridge\\\\Box\\\\Spout\\\\FlatFileReader\\:\\:combine\\(\\) should return array\\<string, string\\>\\|null but returns array\\<string, mixed\\>\\.$#"
count: 1
path: src/batch-box-spout/src/FlatFileReader.php

-
message: "#^Method Yokai\\\\Batch\\\\Bridge\\\\Box\\\\Spout\\\\FlatFileReader\\:\\:rows\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/batch-box-spout/src/FlatFileReader.php

-
message: "#^Offset 'encoding' on array\\{delimiter\\?\\: string, enclosure\\?\\: string\\} in isset\\(\\) does not exist\\.$#"
count: 1
path: src/batch-box-spout/src/FlatFileReader.php

-
message: "#^Cannot cast mixed to string\\.$#"
count: 1
path: src/batch-box-spout/src/FlatFileWriter.php

-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 2
Expand Down
33 changes: 24 additions & 9 deletions src/batch-box-spout/docs/flat-file-item-reader.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
# Item reader with CSV/ODS/XLSX files

The [FlatFileReader](../src/FlatFileReader.php) is a reader
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 Box\Spout\Common\Type;
use Yokai\Batch\Bridge\Box\Spout\FlatFileReader;
use Yokai\Batch\Bridge\Box\Spout\Reader\FlatFileReader;
use Yokai\Batch\Bridge\Box\Spout\Reader\HeaderStrategy;
use Yokai\Batch\Bridge\Box\Spout\Reader\Options\CSVOptions;
use Yokai\Batch\Bridge\Box\Spout\Reader\Options\ODSOptions;
use Yokai\Batch\Bridge\Box\Spout\Reader\Options\SheetFilter;
use Yokai\Batch\Bridge\Box\Spout\Reader\Options\XLSXOptions;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;

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

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

// Read .ods file
// Each item will be an array_combine of headers constructor arg as key and line as values
new FlatFileReader(Type::ODS, new StaticValueParameterAccessor('/path/to/file.ods'), [], FlatFileReader::HEADERS_MODE_SKIP, ['static', 'header', 'keys']);
// 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'),
new ODSOptions(SheetFilter::nameIs('Sheet name to read')),
HeaderStrategy::combine()
);
```

## On the same subject
Expand Down
30 changes: 21 additions & 9 deletions src/batch-box-spout/docs/flat-file-item-writer.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
# Item writer with CSV/ODS/XLSX files

The [FlatFileWriter](../src/FlatFileWriter.php) is a writer that will write to CSV/ODS/XLSX file and each item will
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 Box\Spout\Common\Type;
use Yokai\Batch\Bridge\Box\Spout\FlatFileWriter;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Yokai\Batch\Bridge\Box\Spout\Writer\FlatFileWriter;
use Yokai\Batch\Bridge\Box\Spout\Writer\Options\CSVOptions;
use Yokai\Batch\Bridge\Box\Spout\Writer\Options\ODSOptions;
use Yokai\Batch\Bridge\Box\Spout\Writer\Options\XLSXOptions;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;

// Write items to .xlsx file
// That File will not contain a header line
new FlatFileWriter(Type::XLSX, new StaticValueParameterAccessor('/path/to/file.xlsx'));
// That file will not contain a header line
new FlatFileWriter(new StaticValueParameterAccessor('/path/to/file.xlsx'), new XLSXOptions());

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

// Write items to .ods file
// That File will contain a header line with : static | header | keys
new FlatFileWriter(Type::ODS, new StaticValueParameterAccessor('/path/to/file.ods'), ['static', 'header', 'keys']);
// 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
new FlatFileWriter(
new StaticValueParameterAccessor('/path/to/file.ods'),
new ODSOptions('The sheet name', (new StyleBuilder())->setFontBold()->build()),
['static', 'header', 'keys']
);
```

## On the same subject
Expand Down
47 changes: 47 additions & 0 deletions src/batch-box-spout/src/Exception/InvalidRowSizeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Bridge\Box\Spout\Exception;

use Throwable;
use Yokai\Batch\Exception\LogicException;

final class InvalidRowSizeException extends LogicException
{
/**
* @phpstan-var array<int, string>
*/
private array $headers;
/**
* @phpstan-var array<int, string>
*/
private array $row;

/**
* @phpstan-param array<int, string> $headers
* @phpstan-param array<int, string> $row
*/
public function __construct(array $headers, array $row)
{
parent::__construct('Invalid row size');
$this->headers = $headers;
$this->row = $row;
}

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

/**
* @phpstan-return array<int, string>
*/
public function getRow(): array
{
return $this->row;
}
}
186 changes: 0 additions & 186 deletions src/batch-box-spout/src/FlatFileReader.php

This file was deleted.

Loading