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

Associative Array created in Collection Representation #190

Open
wodka opened this issue Mar 24, 2015 · 5 comments
Open

Associative Array created in Collection Representation #190

wodka opened this issue Mar 24, 2015 · 5 comments

Comments

@wodka
Copy link

wodka commented Mar 24, 2015

Hi,

when in PagerfantaFactory::createRepresentation() a new CollectionRepresentation for the current results is created.

If the current results are a Traversabe Object then it will use the function iterator_to_array -> which used the second default parameter true to include the object keys.

{
  "total": 3,
  "page": 1,
  "pages": 1,
  "limit": 100,
  "_embedded": {
    "items": {
      "document_id_1": "serialized document 1",
      "document_id_2": "serialized document 2",
      "document_id_3": "serialized document 3"
    }
  }
}

whereas I expected it to return the following:

{
  "total": 3,
  "page": 1,
  "pages": 1,
  "limit": 100,
  "_embedded": {
    "items": [
      "serialized document 1",
      "serialized document 2",
      "serialized document 3"
    ]
  }
}

change would be fairly simple:

// Hateoas/Representation/CollectionRepresentation.php:60-62
        if ($resources instanceof \Traversable) {
            $resources = iterator_to_array($resources, false);
        }
@binidini
Copy link

binidini commented Jul 6, 2015

I agree with this suggestion.

@willdurand
Copy link
Owner

👍

@adrienbrault
Copy link
Contributor

I think the current behaviour is fine. If you pass an associate array, you get an json object.
If you need a json array, you can do array_values(iterator_to_array($collection)).

For example, I think this will break the code of people using pomm https://github.com/chanmix51/Pomm/blob/master/Pomm/Object/BaseObject.php

@wodka
Copy link
Author

wodka commented Jul 8, 2015

Ok, then the other way round would be to check in PagerfantaFactory:

// Hateoas/Representation/Factory/PagerfantaFactory.php:40-42
        if (null === $inline) {
            $data = $pager->getCurrentPageResults();
            if ($data instanceof \Traversable) {
                $data = iterator_to_array($data, false);
            }
            $inline = new CollectionRepresentation($data);
        }

@lopsided
Copy link

Has there been any progress on this issue? I'm hitting this problem at the moment. Is there a good workaround?

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

No branches or pull requests

5 participants