nathanmac / Parser

Simple PHP Parser Library for API Development
MIT License
213 stars 53 forks source link

How do I filter the result of the parser? #42

Open troccoli opened 7 years ago

troccoli commented 7 years ago

I'm writing an API that calls an external API. The external API returns data as XML, and my API needs to return data as JSON. I have the following code that works fine

$client = new Client(['base_uri' => $this->baseUri]);
$response = $client->request(
     'GET',
     'GetCAPMan',
     $this->buildQuery([
         'Database'                 => 'CAR',
         'JustCurrentManufacturers' => 'true',
         'BodyStyleFilter'          => '',
    ])
);

    if ($response->getStatusCode() != 200) {
        return false;
    }

    return $this->parser->xml($response->getBody());

I then include this data into my JSON response like the following

return response()->json(['status' => 'success', 'brands' => $brands]);

This, as I said, works fine.

Now, however, I want to filter the XML response from the external API as I'm not interested in all the fields that it returns. I thought I could use the mask() method but I don't know how.

Since the methods are not fluent I thought I set the mask before parsing the payload, thinking that would apply the mask

$this->parser->mask(['Returned_DataSet' => '*']);

return $this->parser->xml($response->getBody());

But that doesn't work.

This is one reason I want to use this package, the ability to filter the output, but how to do it?

danhunsaker commented 7 years ago

The helper methods only operate against the request body (that is, the contents of php://input). I see how it would be useful to be able to apply them against other data, too, though. The trick is to figure out how to make that work. PRs are welcome, of course. A mechanism that doesn't break BC would probably be preferred, I imagine.

chris-faulkner commented 7 years ago

I've been trying to use the mask method as it looks really useful but couldn't figure it out using the readme. I guess you're saying it's designed to use an uploaded file and doesn't accept data directly?

Would it be difficult to allow the mask method to accept extra data?

$parser->mask($mask, $data);

Sorry if I'm way off the mark here, still loving this package without the mask!

danhunsaker commented 7 years ago

That is correct, yes. (Essentially. The "uploaded file" is actually the request body, but close enough.)

One thing to note is that Laravel (via illuminate/support) includes helpers for doing much more advanced operations on arrays and similar, so the helper methods here are largely superfluous for Laravel (and Lumen) users. While they are by no means the only target users, Laravel/Lumen users are the primary contributors, so we sometimes overlook uses like these.

A second argument would probably work. There are other approaches that could work, too. Not sure which approach @nathanmac would prefer in this case, though, so it certainly warrants further conversation.