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

Response collection to plain array conversion method #40

Open
aTanCS opened this issue Aug 29, 2018 · 4 comments
Open

Response collection to plain array conversion method #40

aTanCS opened this issue Aug 29, 2018 · 4 comments

Comments

@aTanCS
Copy link

aTanCS commented Aug 29, 2018

Often I don't need to get a particular property using $response->getProperty('name'), but need all rows as a plain array. There is toArray() method, but it just returns array of Response objects, which has $apttributes property protected. For now I have to use this helper, but it is a quite dirty hack. Is there any better way to get a plain array of response data, or could you please add such method to a ResponseCollection (and also to a Response)?

    $plain = [];
    /** @var \PEAR2\Net\RouterOS\ResponseCollection $response */
    $props = array_keys($response->getPropertyMap());
    foreach ($response as $row)
    {
      $tmp = [];
      foreach ($props as $prop)
      {
        $tmp[$prop] = $row->getProperty($prop);
      }
      $plain[] = $tmp;
    }
@boenrobot
Copy link
Member

boenrobot commented Sep 7, 2018

The properties in each response are also traversable. i.e. you can do

$plain = [];
/**
 * @var \PEAR2\Net\RouterOS\ResponseCollection $response
 * @var \PEAR2\Net\RouterOS\Response $row
 */
foreach ($response as $i => $row) {
    $plain[$i] = [];
    foreach ($row as $name => $value) {
        $plain[$i][$name] = $value;
    }
}

And with that in mind, I'm not sure I see a good use case for when a plain array is required... In the rare case where it might be needed, you can extract it out of the traversable, e.g.

$plain = [];
/** @var \ArrayObject $iterator */
$iterator = $response[0]->getIterator();
$plain[0] = $iterator->getArrayCopy();

The typical cases where you may need an array, like indexing based on a property value or ordering based on one or more properties are possible with the methods in ResponseCollection.

@aTanCS
Copy link
Author

aTanCS commented Sep 14, 2018

I use plain arrays for saving a result as a json to a DB. And I use it a lot for debugging, when I just need to dump a result. With the current state I can't see a result in a readable format, when I use breakpoints. I have to convert it to an array.

@boenrobot
Copy link
Member

boenrobot commented Oct 5, 2018

Well, I've made 0a8e77c in which I've added __debugInfo() support to answer this request, and also an option in ResponseCollection::toArray() to call it on all responses in the collection.

The JSON serialization is an interesting use case, and my first thought was to use the JsonSerializable interface, but that one would require that the minimum required PHP version is bumped to PHP 5.4. __debugInfo() on the other hand has an effect in PHP 5.6 and later, but can be called like a normal function in all earlier PHP versions.

The upcoming phar will include it, but in the meantime, if you want to try it out now, you can use composer, and specify the version you want as "dev-develop".

@aTanCS
Copy link
Author

aTanCS commented Oct 5, 2018

Thanks a lot!

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

2 participants