Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV array of objects (de)-serialization #22

Open
mlavrinenko opened this issue Jun 1, 2017 · 2 comments
Open

CSV array of objects (de)-serialization #22

mlavrinenko opened this issue Jun 1, 2017 · 2 comments
Labels

Comments

@mlavrinenko
Copy link

Hi!

With small fix the lib can serialize array of objects to CSV:
src/Visitor/Csv/CsvSerializationVisitor.php:133

} elseif ($headers !== count($result)) {

to

} elseif ((is_numeric($headers) ? $headers : count($headers)) !== count($result)) {

(after this all tests is OK)

But deserialization returns only one object (last line of CSV).

Example that illustrates this:

<?php

use Ivory\Serializer\Format;
use Ivory\Serializer\Serializer;

class Something
{
    /**
     * @var string
     */
    public $foo;

    public function __toString()
    {
        return (string)$this->foo;
    }
}

class TempTest extends \PHPUnit_Framework_TestCase
{
    function testTemp()
    {
        $serializer = new Serializer();

        $something = new Something;
        $something->foo = 'bar';

        $somethingElse = new Something;
        $somethingElse->foo = 'baz';

        $csv = $serializer->serialize([$something, $somethingElse], Format::CSV);
        $this->assertSame("foo\nbar\nbaz\n", $csv);

        $somethingDeserialized = $serializer->deserialize($csv, Something::class, Format::CSV);
        $this->assertSame((string)$something, (string)$somethingDeserialized);
//        Failed asserting that two strings are identical.
//        --- Expected
//        +++ Actual
//        @@ @@
//        -bar
//        +baz
    }
}

Looking through biult-in tests, I came to a conclusion, that it's not supposed to save more then one object in CSV.
Is it so?
I would like to make it possible, but I dont want to break logic integrity of this lib.

@egeloen
Copy link
Owner

egeloen commented Jun 3, 2017

@mlavrinenko Thanks for your report! The CSV visitors should be able to serialize/deserialize an array of whatever (only restriction: all generated rows must have the same dimension) so, it sounds like a bug to me...

@egeloen egeloen added the bug label Jun 3, 2017
@egeloen
Copy link
Owner

egeloen commented Jun 4, 2017

For your interest, I have started to work on your issue (see https://github.com/egeloen/ivory-serializer/compare/csv-array). It fixes most of your issue but there is still one use case I would like to support and it still does not work (an array with only one item is always unserialized as an object instead of an array...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants