Skip to content

Commit 4593224

Browse files
committed
Adding support for TabularDataProvider and TabularData on Converters
1 parent 2d123e1 commit 4593224

File tree

4 files changed

+90
-22
lines changed

4 files changed

+90
-22
lines changed

src/HTMLConverter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,16 @@ public function __construct()
5454
* @param array<string> $header_record An optional array of headers outputted using the `<thead>` and `<th>` elements
5555
* @param array<string> $footer_record An optional array of footers outputted using the `<tfoot>` and `<th>` elements
5656
*/
57-
public function convert(iterable $records, array $header_record = [], array $footer_record = []): string
57+
public function convert(iterable|TabularData|TabularDataProvider $records, array $header_record = [], array $footer_record = []): string
5858
{
59+
if ($records instanceof TabularDataProvider) {
60+
$records = $records->getTabularData();
61+
}
62+
63+
if ($records instanceof TabularData) {
64+
$records = $records->getRecords();
65+
}
66+
5967
if (null !== $this->formatter) {
6068
$records = MapIterator::fromIterable($records, $this->formatter);
6169
}

src/JsonConverter.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,13 @@ public function when(callable|bool $condition, callable $onSuccess, ?callable $o
396396
*.
397397
* Returns the number of characters read from the handle and passed through to the output.
398398
*
399-
* @param iterable<T> $records
399+
* @param TabularDataProvider|TabularData|iterable<T> $records
400400
* @param array<string> $header
401401
*
402402
* @throws Exception
403403
* @throws JsonException
404404
*/
405-
public function download(iterable|TabularDataProvider $records, ?string $filename = null, array $header = []): int
405+
public function download(TabularDataProvider|TabularData|iterable $records, ?string $filename = null, array $header = []): int
406406
{
407407
if (null !== $filename) {
408408
$mimetype = JsonFormat::Standard === $this->format ? 'application/json' : 'application/x-ndjson';
@@ -419,13 +419,13 @@ public function download(iterable|TabularDataProvider $records, ?string $filenam
419419
/**
420420
* Returns the JSON representation of a tabular data collection.
421421
*
422-
* @param iterable<T> $records
422+
* @param TabularDataProvider|TabularData|iterable<T> $records
423423
* @param array<string> $header
424424
*
425425
* @throws Exception
426426
* @throws JsonException
427427
*/
428-
public function encode(iterable $records, array $header = []): string
428+
public function encode(TabularDataProvider|TabularData|iterable $records, array $header = []): string
429429
{
430430
$stream = Stream::createFromString();
431431
$this->save(records: $records, destination: $stream, header: $header);
@@ -443,7 +443,7 @@ public function encode(iterable $records, array $header = []): string
443443
* required to provide a file with the correct open
444444
* mode.
445445
*
446-
* @param iterable<T> $records
446+
* @param TabularDataProvider|TabularData|iterable<T> $records
447447
* @param SplFileInfo|SplFileObject|Stream|resource|string $destination
448448
* @param resource|null $context
449449
* @param array<string> $header
@@ -453,7 +453,7 @@ public function encode(iterable $records, array $header = []): string
453453
* @throws TypeError
454454
* @throws UnavailableStream
455455
*/
456-
public function save(iterable|TabularDataProvider $records, mixed $destination, $context = null, array $header = []): int
456+
public function save(TabularDataProvider|TabularData|iterable $records, mixed $destination, $context = null, array $header = []): int
457457
{
458458
$stream = match (true) {
459459
$destination instanceof Stream,
@@ -482,22 +482,22 @@ public function save(iterable|TabularDataProvider $records, mixed $destination,
482482
/**
483483
* Returns an Iterator that you can iterate to generate the actual JSON string representation.
484484
*
485-
* @param iterable<T> $records
485+
* @param TabularDataProvider|TabularData|iterable<T> $records
486486
* @param array<string> $header
487487
*
488488
* @throws JsonException
489489
* @throws Exception
490490
*
491491
* @return Iterator<string>
492492
*/
493-
public function convert(iterable|TabularDataProvider $records, array $header = []): Iterator
493+
public function convert(TabularDataProvider|TabularData|iterable $records, array $header = []): Iterator
494494
{
495495
if ($records instanceof TabularDataProvider) {
496-
$tabularData = $records->getTabularData();
497-
$records = $tabularData->getRecords();
498-
if ([] === $header) {
499-
$header = $tabularData->getHeader();
500-
}
496+
$records = $records->getTabularData();
497+
}
498+
499+
if ($records instanceof TabularData) {
500+
$records = $records->getRecords();
501501
}
502502

503503
$iterator = match ($this->formatter) {
@@ -506,10 +506,6 @@ public function convert(iterable|TabularDataProvider $records, array $header = [
506506
};
507507

508508
if (in_array($this->format, [JsonFormat::NdJsonHeader, JsonFormat::NdJsonHeaderLess], true)) {
509-
if ($records instanceof TabularData && [] === $header) {
510-
$header = $records->getHeader();
511-
}
512-
513509
$iterator = self::getList($iterator, $header, $this->format)();
514510
}
515511

@@ -565,7 +561,7 @@ private static function getList(Iterator $data, array $header, JsonFormat $forma
565561
return fn () => yield from new MapIterator($data, fn (array $record): array => array_values($record));
566562
}
567563

568-
[] !== $header || throw new InvalidArgument('the tabular data header is empty.');
564+
[] !== $header || throw new InvalidArgument('A non empty header must be provided when using `JsonFormat::NdJsonHeader`.');
569565

570566
return function () use ($header, $data) {
571567
yield $header;

src/JsonConverterTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,60 @@ public function it_can_tell_which_fornat_it_is_using(): void
237237
self::assertSame(JsonFormat::Standard, $converter->format);
238238
self::assertSame(JsonFormat::NdJson, $newConverter->format);
239239
}
240+
241+
#[Test]
242+
public function it_can_force_generate_ldjson_with_list_headers(): void
243+
{
244+
$csv = Reader::createFromPath(__DIR__.'/../test_files/prenoms.csv')
245+
->setDelimiter(';')
246+
->setHeaderOffset(0);
247+
248+
CharsetConverter::addTo($csv, 'iso-8859-15', 'utf-8');
249+
250+
$data = (new JsonConverter())
251+
->format(JsonFormat::NdJsonHeader)
252+
->encode(
253+
records: $csv->slice(0, 3),
254+
header: $csv->getHeader(),
255+
);
256+
257+
self::assertStringContainsString('["prenoms","nombre","sexe","annee"]', $data);
258+
}
259+
260+
#[Test]
261+
public function it_can_force_generate_ldjson_without_headers(): void
262+
{
263+
$csv = Reader::createFromPath(__DIR__.'/../test_files/prenoms.csv')
264+
->setDelimiter(';')
265+
->setHeaderOffset(0);
266+
267+
CharsetConverter::addTo($csv, 'iso-8859-15', 'utf-8');
268+
269+
$data = (new JsonConverter())
270+
->format(JsonFormat::NdJsonHeaderLess)
271+
->encode(
272+
records: $csv->slice(0, 3),
273+
header: $csv->getHeader(),
274+
);
275+
276+
self::assertStringNotContainsString('["prenoms","nombre","sexe","annee"]', $data);
277+
}
278+
279+
#[Test]
280+
public function it_fails_generating_the_ldjson_if_no_header_is_provided(): void
281+
{
282+
$this->expectException(InvalidArgument::class);
283+
284+
$csv = Reader::createFromPath(__DIR__.'/../test_files/prenoms.csv')
285+
->setDelimiter(';')
286+
->setHeaderOffset(0);
287+
288+
CharsetConverter::addTo($csv, 'iso-8859-15', 'utf-8');
289+
290+
$this->expectException(InvalidArgument::class);
291+
292+
(new JsonConverter())
293+
->format(JsonFormat::NdJsonHeader)
294+
->encode(records: $csv->slice(0, 3));
295+
}
240296
}

src/XMLConverter.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function formatter(?callable $formatter): self
144144
*
145145
* @throws Exception
146146
*/
147-
public function download(iterable $records, ?string $filename = null, string $encoding = 'utf-8', bool $formatOutput = false): int|false
147+
public function download(TabularDataProvider|TabularData|iterable $records, ?string $filename = null, string $encoding = 'utf-8', bool $formatOutput = false): int|false
148148
{
149149
/** @var XMLDocument|DOMDocument $document */
150150
$document = self::newXmlDocument(XMLDocument::class);
@@ -168,8 +168,16 @@ public function download(iterable $records, ?string $filename = null, string $en
168168
*
169169
* **DOES NOT** attach to the DOMDocument
170170
*/
171-
public function import(iterable $records, DOMDocument|XMLDocument $doc): DOMElement|Element
171+
public function import(TabularDataProvider|TabularData|iterable $records, DOMDocument|XMLDocument $doc): DOMElement|Element
172172
{
173+
if ($records instanceof TabularDataProvider) {
174+
$records = $records->getTabularData();
175+
}
176+
177+
if ($records instanceof TabularData) {
178+
$records = $records->getRecords();
179+
}
180+
173181
if (null !== $this->formatter) {
174182
$records = MapIterator::fromIterable($records, $this->formatter);
175183
}
@@ -282,7 +290,7 @@ protected function filterAttributeName(string $value): string
282290
* Converts a Record collection into a DOMDocument.
283291
*/
284292
#[Deprecated(message:'use League\Csv\XMLConverter::impoprt()', since:'league/csv:9.22.0')]
285-
public function convert(iterable $records): DOMDocument
293+
public function convert(TabularDataProvider|TabularData|iterable $records): DOMDocument
286294
{
287295
$document = new DOMDocument(encoding: 'UTF-8');
288296
$document->appendChild($this->import($records, $document));

0 commit comments

Comments
 (0)