Neuron-PHP

ProblemDetailsResponse
in package

Helper class for creating RFC 9457 Problem Details API responses.

This class handles the formatting and headers for Problem Details responses, ensuring that API errors are returned with the correct content type and structure as defined in RFC 9457.

The class sets the appropriate Content-Type header (application/problem+json) to distinguish error responses from regular JSON responses, allowing clients to handle them differently.

Tags
example
// In an API controller
$problem = new ProblemDetails(
    type: '/errors/validation',
    title: 'Validation Failed',
    status: 400,
    detail: 'Invalid email format'
);

$response = new ProblemDetailsResponse($problem);
return $response->send();

// Or use the static helper
return ProblemDetailsResponse::create($problem)->send();

Table of Contents

Properties

$headers  : array<string|int, mixed>
$problemDetails  : ProblemDetails

Methods

__construct()  : mixed
Create a new Problem Details response.
create()  : self
Static factory method for creating a response.
getHeaders()  : array<string|int, mixed>
Get the response headers.
getProblemDetails()  : ProblemDetails
Get the problem details.
notFound()  : self
Create a response for not found errors.
rateLimitExceeded()  : self
Create a response for specific rate limiting scenarios.
send()  : string
Send the response with appropriate headers and status code.
toArray()  : array<string|int, mixed>
Get the response without sending headers.
toJson()  : string
Get the JSON representation of the problem details.
validationError()  : self
Create a response for validation errors.
withHeader()  : self
Add an additional header to the response.
withHeaders()  : self
Add multiple headers to the response.

Properties

Methods

__construct()

Create a new Problem Details response.

public __construct(ProblemDetails $problemDetails[, array<string|int, mixed> $additionalHeaders = [] ]) : mixed
Parameters
$problemDetails : ProblemDetails

The problem details to send

$additionalHeaders : array<string|int, mixed> = []

Optional additional HTTP headers

create()

Static factory method for creating a response.

public static create(ProblemDetails $problemDetails[, array<string|int, mixed> $additionalHeaders = [] ]) : self
Parameters
$problemDetails : ProblemDetails

The problem details

$additionalHeaders : array<string|int, mixed> = []

Optional additional headers

Return values
self

getHeaders()

Get the response headers.

public getHeaders() : array<string|int, mixed>
Return values
array<string|int, mixed>

notFound()

Create a response for not found errors.

public static notFound(string $resource[, string|int|null $identifier = null ]) : self
Parameters
$resource : string

The type of resource that was not found

$identifier : string|int|null = null

The identifier that was searched for

Return values
self

rateLimitExceeded()

Create a response for specific rate limiting scenarios.

public static rateLimitExceeded(int $retryAfter[, string|null $detail = null ]) : self
Parameters
$retryAfter : int

Seconds until the client should retry

$detail : string|null = null

Specific details about the rate limit

Return values
self

send()

Send the response with appropriate headers and status code.

public send() : string

This method sets the HTTP response code, sends headers, and returns the JSON-encoded problem details. It's designed to be the final return value from a controller action.

Return values
string

The JSON response body

toArray()

Get the response without sending headers.

public toArray() : array<string|int, mixed>

This is useful when the framework handles header sending separately, or for testing purposes.

Return values
array<string|int, mixed>

Array with 'status', 'headers', and 'body' keys

toJson()

Get the JSON representation of the problem details.

public toJson([int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ]) : string
Parameters
$options : int = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE

JSON encoding options

Return values
string

JSON string

validationError()

Create a response for validation errors.

public static validationError(array<string|int, mixed> $errors[, string|null $detail = null ]) : self
Parameters
$errors : array<string|int, mixed>

Field-specific validation errors

$detail : string|null = null

Overall error description

Return values
self

withHeader()

Add an additional header to the response.

public withHeader(string $name, string $value) : self
Parameters
$name : string

Header name

$value : string

Header value

Return values
self

withHeaders()

Add multiple headers to the response.

public withHeaders(array<string|int, mixed> $headers) : self
Parameters
$headers : array<string|int, mixed>

Associative array of headers

Return values
self

        
On this page

Search results