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

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

Open
troccoli opened this issue Feb 14, 2017 · 3 comments
Open

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

troccoli opened this issue Feb 14, 2017 · 3 comments

Comments

@troccoli
Copy link

troccoli commented Feb 14, 2017

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
Copy link
Contributor

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
Copy link

chris-faulkner commented Feb 24, 2017

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
Copy link
Contributor

danhunsaker commented Feb 24, 2017

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.

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

No branches or pull requests

3 participants