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

Feature | Added isJson and isXML functions to Response class #455

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from

Conversation

mstfblci
Copy link

Two new functions have been added to check if the response return is json or xml

@SRWieZ
Copy link

SRWieZ commented Sep 17, 2024

This could be really useful; in a lot of my Connectors, I do things like this:

public function hasRequestFailed(Response $response): ?bool
{
    // if psr response is json
    $contentType = $response->getPsrResponse()->getHeader('Content-Type');

    if (in_array('application/json', $contentType)) {
        return ! empty($response->json('errors'));
    }

    return null;
}

This PR would simplify it like this:

public function hasRequestFailed(Response $response): ?bool
{
    if ($response->isJson()) {
        return ! empty($response->json('errors'));
    }

    return null;
}

I would argue, though, that I prefer a more strict comparison than str_contains() proposed in this PR.

@mstfblci
Copy link
Author

@SRWieZ Thank you for your support.

Copy link
Member

@Sammyjo20 Sammyjo20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this! Super simple as well. Would you be able to write two tests for these functions just to prove coverage? Many thanks

@Sammyjo20 Sammyjo20 changed the title Added isJson and isXML functions to Response class Feature | Added isJson and isXML functions to Response class Dec 3, 2024
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

Successfully merging this pull request may close these issues.

3 participants