@@ -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 ;
0 commit comments