Neuron-PHP

ProblemDetails implements JsonSerializable

RFC 9457 Problem Details for HTTP APIs implementation.

This class represents a problem detail as defined in RFC 9457, providing a standardized format for API error responses. It enables consistent, machine-readable error information across all API endpoints.

RFC 9457 defines a standard format for error responses in HTTP APIs, replacing the older RFC 7807. The format provides both required and optional fields to convey error information in a structured way.

Required fields:

  • type: A URI reference that identifies the problem type
  • title: A short, human-readable summary of the problem type
  • status: The HTTP status code for this occurrence of the problem

Optional fields:

  • detail: A human-readable explanation specific to this occurrence
  • instance: A URI reference that identifies the specific occurrence

Extension fields:

  • Any additional fields can be included to provide more context
Tags
example
// Create a validation error problem detail
$problem = new ProblemDetails(
    type: '/errors/validation',
    title: 'Validation Failed',
    status: 400,
    detail: 'The request contains invalid fields',
    extensions: [
        'errors' => [
            'email' => 'Invalid email format',
            'password' => 'Must be at least 8 characters'
        ]
    ]
);

// Convert to JSON for API response
$json = json_encode($problem);

Table of Contents

Interfaces

JsonSerializable

Properties

$detail  : string|null
$extensions  : array<string|int, mixed>
$instance  : string|null
$status  : int
$title  : string
$type  : string

Methods

__construct()  : mixed
Create a new Problem Details instance.
fromArray()  : self
Create a ProblemDetails instance from an array.
getDetail()  : string|null
Get the problem detail.
getExtension()  : mixed|null
Get a specific extension field value.
getExtensions()  : array<string|int, mixed>
Get extension fields.
getInstance()  : string|null
Get the problem instance URI.
getStatus()  : int
Get the HTTP status code.
getTitle()  : string
Get the problem title.
getType()  : string
Get the problem type URI.
jsonSerialize()  : array<string|int, mixed>
Specify data which should be serialized to JSON.
toArray()  : array<string|int, mixed>
Convert to array representation.
validate()  : void
Validate the problem details fields.

Properties

Methods

__construct()

Create a new Problem Details instance.

public __construct(string $type, string $title, int $status[, string|null $detail = null ][, string|null $instance = null ][, array<string|int, mixed> $extensions = [] ]) : mixed
Parameters
$type : string

URI reference identifying the problem type

$title : string

Short, human-readable summary of the problem

$status : int

HTTP status code for this occurrence

$detail : string|null = null

Human-readable explanation specific to this occurrence

$instance : string|null = null

URI reference identifying the specific occurrence

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

Additional fields to include in the response

fromArray()

Create a ProblemDetails instance from an array.

public static fromArray(array<string|int, mixed> $data) : self
Parameters
$data : array<string|int, mixed>

The problem details data

Return values
self

getDetail()

Get the problem detail.

public getDetail() : string|null
Return values
string|null

getExtension()

Get a specific extension field value.

public getExtension(string $key) : mixed|null
Parameters
$key : string

The extension field name

Return values
mixed|null

The field value or null if not set

getExtensions()

Get extension fields.

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

getInstance()

Get the problem instance URI.

public getInstance() : string|null
Return values
string|null

getStatus()

Get the HTTP status code.

public getStatus() : int
Return values
int

getTitle()

Get the problem title.

public getTitle() : string
Return values
string

getType()

Get the problem type URI.

public getType() : string
Return values
string

jsonSerialize()

Specify data which should be serialized to JSON.

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

Data to be JSON encoded

toArray()

Convert to array representation.

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

The problem details as an associative array

validate()

Validate the problem details fields.

private validate() : void
Tags
throws
InvalidArgumentException

If required fields are invalid


        
On this page

Search results