diff --git a/CHANGELOG.md b/CHANGELOG.md index e138a9da..1bd2112d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All Notable changes to `Csv` will be documented in this file - `Comparison::CONTAINS` must check the value is a string before calling `str_compare` [#548](https://github.com/thephpleague/csv/pull/548) by [cage-is](https://github.com/cage-is) - Fix testing to improve Debian integration [#549](https://github.com/thephpleague/csv/pull/549) by [David Prévot and tenzap](https://github.com/tenzap) +- `Bom::tryFromSequence` and `Bom::fromSequence` supports the `Reader` and `Writer` class. ### Removed diff --git a/docs/_layouts/homepage.html b/docs/_layouts/homepage.html index c7b8f9f5..a32b5ae5 100644 --- a/docs/_layouts/homepage.html +++ b/docs/_layouts/homepage.html @@ -62,15 +62,15 @@

Highlights

-

Simple API

+

Simple and straightforward API

-

Read and Write to CSV documents in
a memory efficient and scalable way

+

Read from and Write to CSV documents
in a memory efficient and scalable way

-

Flexible and powerful query features and object-mapping.

+

Flexible and powerful query features
and array to object-mapping.

diff --git a/docs/index.md b/docs/index.md index cb405edb..fd524e73 100644 --- a/docs/index.md +++ b/docs/index.md @@ -66,6 +66,9 @@ $csv->setHeaderOffset(0); //build a statement $stmt = Statement::create() + ->select('firstname', 'lastname', 'email') + ->andWhere('firstname', 'starts with', 'A') + ->orderByAsc('email') ->offset(10) ->limit(25); @@ -124,14 +127,13 @@ PHP stream filters can directly be used to ease manipulating CSV document ```php use League\Csv\Reader; +use League\Csv\Bom; $csv = Reader::createFromPath('/path/to/your/csv/file.csv', 'r'); $csv->setHeaderOffset(0); -$input_bom = $csv->getInputBOM(); - -if ($input_bom === Reader::BOM_UTF16_LE || $input_bom === Reader::BOM_UTF16_BE) { - $csv->addStreamFilter('convert.iconv.UTF-16/UTF-8'); +if (Bom::tryFromSequence($csv)?->isUtf16() ?? false) { + $csv->appendStreamFilterOnRead('convert.iconv.UTF-16/UTF-8'); } foreach ($csv as $record) { diff --git a/src/Bom.php b/src/Bom.php index f57c9b91..3c422627 100644 --- a/src/Bom.php +++ b/src/Bom.php @@ -38,6 +38,7 @@ public static function tryFromSequence(mixed $sequence): ?self $sequence instanceof SplFileObject, $sequence instanceof Stream => self::getContents($sequence, 4, 0), is_resource($sequence) => stream_get_contents($sequence, 4, 0), + $sequence instanceof AbstractCsv => $sequence->getInputBOM(), $sequence instanceof Stringable, is_string($sequence) => substr((string) $sequence, 0, 4), default => $sequence, diff --git a/src/BomTest.php b/src/BomTest.php index b0ca4f98..ddcdc2c2 100644 --- a/src/BomTest.php +++ b/src/BomTest.php @@ -21,7 +21,7 @@ final class BomTest extends TestCase { #[DataProvider('BomSequencesProvider')] - public function test_bom_detection_from_sequence(string $sequence, ?Bom $expected): void + public function test_bom_detection_from_sequence(string|AbstractCsv $sequence, ?Bom $expected): void { self::assertSame($expected, Bom::tryFromSequence($sequence)); } @@ -54,6 +54,10 @@ public static function BomSequencesProvider(): array 'sequence' => chr(255).chr(254).chr(0).chr(0), 'expected' => Bom::Utf32Le, ], + 'UTF32 LE BOM sequence in Reader class' => [ + 'sequence' => Reader::createFromString(chr(255).chr(254).chr(0).chr(0)), + 'expected' => Bom::Utf32Le, + ], ]; }