Skip to content

Commit f661a7a

Browse files
authored
Merge pull request #128 from spatie/openspout-v4
V4 with PHP 8.2 support
2 parents ed70470 + 952b15e commit f661a7a

11 files changed

Lines changed: 350 additions & 102 deletions

.github/workflows/php-cs-fixer.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@ jobs:
88

99
steps:
1010
- name: Checkout code
11-
uses: actions/checkout@v1
11+
uses: actions/checkout@v3
1212

13-
- name: Fix style
13+
- name: Run PHP CS Fixer
1414
uses: docker://oskarstark/php-cs-fixer-ga
1515
with:
1616
args: --config=.php-cs-fixer.dist.php --allow-risky=yes
1717

18-
- name: Extract branch name
19-
shell: bash
20-
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
21-
id: extract_branch
22-
2318
- name: Commit changes
24-
uses: stefanzweifel/git-auto-commit-action@v2.3.0
19+
uses: stefanzweifel/git-auto-commit-action@v4
2520
with:
2621
commit_message: Fix styling
27-
branch: ${{ steps.extract_branch.outputs.branch }}
28-
env:
29-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.php-cs-fixer.dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
return (new PhpCsFixer\Config())
1818
->setRules([
19-
'@PSR2' => true,
19+
'@PSR12' => true,
2020
'array_syntax' => ['syntax' => 'short'],
2121
'ordered_imports' => ['sort_algorithm' => 'alpha'],
2222
'no_unused_imports' => true,

UPGRADE.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Upgrade guide
2+
3+
## Upgrading from 2.x to 3.0
4+
5+
6+
### Most notable changes
7+
8+
1. Add support for openspout/openspout v4
9+
2. Drop support for openspout/openspout v3
10+
3. Add type hinting
11+
4. Removed `useDelimiter` on SimpleExcelWriter
12+
5. Removed `headerRowFormatter` on SimpleExcelReader
13+
14+
### Classes have been moved
15+
16+
- `\Box\Spout\Common\Entity\Row` should be replaced with `\OpenSpout\Common\Entity\Row`
17+
- `\Box\Spout\Common\Entity\Style\Style` should be replaced with `OpenSpout\Common\Entity\Style\Style`
18+
19+
### Removed `useDelimiter()` on SimpleExcelWriter
20+
21+
In v3 there was a method to set a delimiter. Now you should pass this as parameter to the constructor.
22+
23+
Change
24+
```php
25+
$reader = SimpleExcelWriter::create($file)->useDelimiter(';');
26+
```
27+
28+
To
29+
```php
30+
$writer = SimpleExcelWriter::create(file: $file, delimiter: ';');
31+
```
32+
33+
### Deprecated setting the type manually
34+
35+
In v4 of openspout/openspout it is no longer possible to explicitly set the type.
36+
We still have support for this, but we'll deprecate the method.
37+
38+
```php
39+
$reader = SimpleExcelReader::create('php://input', 'csv');
40+
```
41+
42+
```php
43+
$writer = SimpleExcelWriter::create('php://output', 'csv');
44+
```

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^8.0",
20-
"openspout/openspout": "^3.0",
20+
"openspout/openspout": "^4.8",
2121
"illuminate/support": "^8.71|^9.0"
2222
},
2323
"require-dev": {
@@ -50,4 +50,4 @@
5050
},
5151
"minimum-stability": "dev",
5252
"prefer-stable": true
53-
}
53+
}

phpunit.xml.dist

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage>
4-
<include>
5-
<directory suffix=".php">src/</directory>
6-
</include>
7-
</coverage>
8-
<testsuites>
9-
<testsuite name="Spatie Test Suite">
10-
<directory>tests</directory>
11-
</testsuite>
12-
</testsuites>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
bootstrap="vendor/autoload.php"
8+
colors="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnFailure="false"
14+
executionOrder="random"
15+
failOnWarning="true"
16+
failOnRisky="true"
17+
failOnEmptyTestSuite="true"
18+
beStrictAboutOutputDuringTests="true"
19+
verbose="true"
20+
>
21+
<testsuites>
22+
<testsuite name="Spatie Test Suite">
23+
<directory>tests</directory>
24+
</testsuite>
25+
</testsuites>
26+
<coverage>
27+
<include>
28+
<directory suffix=".php">./src</directory>
29+
</include>
30+
<report>
31+
<html outputDirectory="build/coverage"/>
32+
<text outputFile="build/coverage.txt"/>
33+
<clover outputFile="build/logs/clover.xml"/>
34+
</report>
35+
</coverage>
36+
<logging>
37+
<junit outputFile="build/report.junit.xml"/>
38+
</logging>
1339
</phpunit>

src/ReaderFactory.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Spatie\SimpleExcel;
4+
5+
use OpenSpout\Common\Exception\IOException;
6+
use OpenSpout\Common\Exception\UnsupportedTypeException;
7+
use OpenSpout\Reader\CSV\Options as CSVOptions;
8+
use OpenSpout\Reader\CSV\Reader as CSVReader;
9+
use OpenSpout\Reader\ODS\Options as ODSOptions;
10+
use OpenSpout\Reader\ODS\Reader as ODSReader;
11+
use OpenSpout\Reader\ReaderInterface;
12+
use OpenSpout\Reader\XLSX\Options as XLSXOptions;
13+
use OpenSpout\Reader\XLSX\Reader as XLSXReader;
14+
15+
/**
16+
* @internal overwritten from openspout/openspout so we can pass Options to the Reader classes
17+
* Original: \OpenSpout\Reader\Common\Creator\ReaderFactory
18+
*/
19+
class ReaderFactory
20+
{
21+
/**
22+
* Creates a reader by file extension.
23+
*
24+
* @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
25+
*
26+
* @throws \OpenSpout\Common\Exception\UnsupportedTypeException
27+
*/
28+
public static function createFromFile(
29+
string $path,
30+
CSVOptions|XLSXOptions|ODSOptions|null $options = null
31+
): ReaderInterface {
32+
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
33+
34+
return match ($extension) {
35+
'csv' => new CSVReader($options),
36+
'xlsx' => new XLSXReader($options),
37+
'ods' => new ODSReader($options),
38+
default => throw new UnsupportedTypeException('No readers supporting the given type: '.$extension),
39+
};
40+
}
41+
42+
/**
43+
* Creates a reader by mime type.
44+
*
45+
* @param string $path the path to the spreadsheet file
46+
*
47+
* @throws \OpenSpout\Common\Exception\UnsupportedTypeException
48+
* @throws \OpenSpout\Common\Exception\IOException
49+
*/
50+
public static function createFromFileByMimeType(
51+
string $path,
52+
CSVOptions|XLSXOptions|ODSOptions|null $options = null
53+
): ReaderInterface {
54+
if (! file_exists($path)) {
55+
throw new IOException("Could not open {$path} for reading! File does not exist.");
56+
}
57+
58+
$mime_type = mime_content_type($path);
59+
60+
return match ($mime_type) {
61+
'application/csv', 'text/csv', 'text/plain' => new CSVReader($options),
62+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => new XLSXReader($options),
63+
'application/vnd.oasis.opendocument.spreadsheet' => new ODSReader($options),
64+
default => throw new UnsupportedTypeException('No readers supporting the given type: '.$mime_type),
65+
};
66+
}
67+
68+
/**
69+
* @deprecated use createFromFileByMimeType() or createFromFile() instead
70+
*
71+
* @throws UnsupportedTypeException
72+
*/
73+
public static function createFromType(
74+
string $readerType,
75+
CSVOptions|XLSXOptions|ODSOptions|null $options = null
76+
): ReaderInterface {
77+
return match ($readerType) {
78+
'csv' => new CSVReader($options),
79+
'xlsx' => new XLSXReader($options),
80+
'ods' => new ODSReader($options),
81+
default => throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType),
82+
};
83+
}
84+
}

0 commit comments

Comments
 (0)