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

Request macros #6

Open
mtvbrianking opened this issue May 17, 2021 · 1 comment
Open

Request macros #6

mtvbrianking opened this issue May 17, 2021 · 1 comment

Comments

@mtvbrianking
Copy link
Owner

$request->isXml();

Should validate the content type to be XML as well as check the validity of the content to be valid XML

$request->sentXml();

Should only validate the content type

@mtvbrianking
Copy link
Owner Author

mtvbrianking commented Nov 15, 2022

Users usually forget the accept header but, if a user sends content-type XML, they expect XML in return unless they explicitly state otherwise.

Determine if a user wants XML by checking both the request Accept and Content Type headers...

Request::macro('wantsXml', function (bool $checkContentType = false) {
    $acceptableTypes = $this->getAcceptableContentTypes();

    $accepts = array_pop($acceptableTypes) ?? '';

    $acceptsXml = Str::contains($accepts, ['/xml', '+xml']);

    if ($acceptsXml) {
        return true;
    }

    if (! $checkContentType) {
        return false;
    }

    return $this->getContentType() == 'xml';
});

// ...

$request->wantsXml(true);

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

1 participant