Crell / ApiProblem

A simple implementation of the api-problem specification. Includes PSR-15 support.
MIT License
239 stars 21 forks source link

Add an "asArray()" method to get the compiled data as a native PHP array #4

Closed cpliakas closed 9 years ago

cpliakas commented 10 years ago

Awesome project.

It would be great to add an ApiProblem::asArray() method to get the compiled data as a native PHP array. The use case I have is to more easily integrate this project into a Silex app, where JSON data is returned by adding the following statement to your callback:

$app->error(function (\Exception $e, $code) use ($app) {
  // ...
  return $app->json($data);
});

Without a method to get a native PHP array, I have to re-implement some header-setting in the json() method in order for things to be treated the same way as JSON in the rest of the application.

Thanks in advance for any attention you can give to this, Chris

clemens-tolboom commented 10 years ago

I ran into similar problem testing for Drupal Rest

return new ResourceResponse(json_decode($problem->asJson(), TRUE));
Crell commented 9 years ago

I made a few other changes meanwhile to update for draft 7, which broke this PR. :-( I like the addition, though, so I readded it myself. Thanks, Chris!

cpliakas commented 9 years ago

Great! Thanks for incorporating it, much appreciated.