Skip to content

Commit

Permalink
Update documentation landing page
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 11, 2025
1 parent af3b95a commit 6011f6b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,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) {
Expand Down
1 change: 1 addition & 0 deletions src/Bom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/BomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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,
],
];
}

Expand Down

0 comments on commit 6011f6b

Please sign in to comment.