Skip to content

[CsvEncoder] Impossible to use with Traversable #60141

@dmitryuk

Description

@dmitryuk

Symfony version(s) affected

7.2

Description

CsvEncoder accepts iterator to encode function, but it doesn't work properly with Traversable or any non-simple-array iterators

How to reproduce

class Example
{
    public function build()
    {
        $encoder = new CsvEncoder();
        $csv = $encoder->encode($this->getLineIterator(), CsvEncoder::FORMAT);
    }

    private function getLineIterator(): Traversable
    {
        for ($i=0;$i< 1000; $i++) {
            yield [(string) $i];
        }
    }
}

I get an exception
Cannot traverse an already closed generator

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions