Return responses as a ResultSet object#16
Closed
andyvanee wants to merge 2 commits intovarsitynewsnetwork:masterfrom
Closed
Return responses as a ResultSet object#16andyvanee wants to merge 2 commits intovarsitynewsnetwork:masterfrom
andyvanee wants to merge 2 commits intovarsitynewsnetwork:masterfrom
Conversation
ResultSet objects provide access to the original request as well as the total and totalPages headers supplied by the REST API.
Author
|
Just to clarify this change, the results from the API are no longer the basic array returned from $posts = $client->posts()->get();
// Iterate through results as usual
foreach ($posts as $post) {
print_r($post);
}
// Also index, count, sort, and other array-like behaviour
print_r($posts[0]);
echo count($posts);
usort($posts, function($a, $b) {
//...
});
// Access data that was previously unavailable
echo $posts->total;
echo $posts->totalPages;
print_r($posts->request); |
Author
|
One issue I've found is that my previous comment about $posts->uasort(function($a, $b) {
//...
}); |
Author
|
Closing due to inactivity... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
By returning a ResultSet object, which extends ArrayObject, we are able to return other valuable information about the result without breaking array-like functionality. The additional data attached is the WP-Total and WP-TotalPages headers (addresses #15) as well as the original request.
ArrayObject's should behave exactly the same as the response from
json_decode(), with the exception of direct type checking. Although I don't expect type checking the returned data to be common, this could be considered a breaking change because of that.The tests were modified since the response is no longer just an array. This could also have been rewritten by casting the
ResultSetsuch as:Feel free to suggest naming change or coding style changes if you see them.