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: add decode_body() method #559

Merged
merged 12 commits into from
Oct 11, 2021

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Sep 18, 2021

Recreation of PR #214 as updating that PR failed.

Props @ccrims0n

Add json() for json responses

  • Add json() which returns decoded json from the response body
    if the response body is a valid json else throws an exception

Fix json_decode() for php 5.2 and 5.3

Fix json() for php5.4

Response::json(): revert changes for PHP 5.2 -5.4 compatibility

PHP 5.6 is the new minimum PHP version, so these accommodations for PHP 5.2 - 5.4 can be reverted again.

This reverts commit 8ea4334 and 2d2ecf3.

Response::json(): update to make code runnable again

  • Use namespaced names.
  • Namespace test class and rename test file to match.
  • Use expectException() method instead of annotation.

Response::json(): update to comply with coding standards

Response::json(): rename the method

... to be more descriptive.

Response::decode_body(): rename parameter

... to be in-line with the parameter name used in PHP itself.

Ref: https://www.php.net/json-decode

Response::decode_body(): modernize the error handling

As of PHP 5.5.0, PHP contains the json_last_error_msg() function which will return the last error message.

This removes the need for the error decoding array, which was outdated by now anyway due to new error codes having been introduced in PHP since the PR was originally pulled.

Ref: https://www.php.net/manual/en/function.json-last-error-msg.php

Response::decode_body(): improve the method documentation

... to be in-line with the documentation for the same parameters in the PHP native function.

Ref: https://www.php.net/json-decode

Response::decode_body(): improve the tests

  • Use type safe assertSame() instead of assertEquals() and remove the unnecessary foreach.
  • Rework the "invalid JSON" test to a data provider and add some adidtional test cases.
  • Add @covers tags.
  • Add @requires tags for the JSON extension as while that extension has been bundled with PHP for a long time, it could still be disabled until PHP 8.0.
  • Add documentation.

Composer: make the dependency on the JSON extension explicit

Fixes #167
Closes #214

@jrfnl jrfnl added this to the 2.0.0 milestone Sep 18, 2021
@jrfnl jrfnl changed the title Ccrims0n/feature/add json response Response: add decode_body()` method Sep 18, 2021
@jrfnl jrfnl changed the title Response: add decode_body()` method Response: add decode_body() method Sep 18, 2021
@jrfnl jrfnl force-pushed the ccrims0n/feature/add_json_response branch 2 times, most recently from b4d528a to cfe76e8 Compare September 18, 2021 02:20
ccrims0n and others added 12 commits October 5, 2021 00:57
* Add json() which returns decoded json from the response body
  if the response body is a valid json else throws an exception
PHP 5.6 is the new minimum PHP version, so these accommodations for PHP 5.2 - 5.4 can be reverted again.

This reverts commit 8ea4334 and 2d2ecf3.
* Use namespaced names.
* Namespace test class and rename test file to match.
* Use `expectException()` method instead of annotation.
... to be more descriptive.
... to be in-line with the parameter name used in PHP itself.

Ref: https://www.php.net/json-decode
As of PHP 5.5.0, PHP contains the `json_last_error_msg()` function which will return the last error message.

This removes the need for the error decoding array, which was outdated by now anyway due to new error codes having been introduced in PHP since the PR was originally pulled.

Ref: https://www.php.net/manual/en/function.json-last-error-msg.php
... to be in-line with the documentation for the same parameters in the PHP native function.

Ref: https://www.php.net/json-decode
* Use type safe `assertSame()` instead of `assertEquals()` and remove the unnecessary `foreach`.
* Rework the "invalid JSON" test to a data provider and add some adidtional test cases.
* Add `@covers` tags.
* Add `@requires` tags for the JSON extension as while that extension has been bundled with PHP for a long time, it could still be disabled until PHP 8.0.
* Add documentation.
@jrfnl jrfnl force-pushed the ccrims0n/feature/add_json_response branch from cfe76e8 to f454910 Compare October 4, 2021 22:58
@schlessera schlessera merged commit 26d64c0 into develop Oct 11, 2021
@schlessera schlessera deleted the ccrims0n/feature/add_json_response branch October 11, 2021 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

decode json response
3 participants