ContentHandler
in package
uses
BaseTraits
Defines a class that is responsible for decoding request content from remote clients, and encoding response content to be sent to the remote client. Children of this class must correspond with the valid HTTP MIME type this ContentHandler is designed to interact with.
Tags
Table of Contents
Constants
- DEFAULT_ENCODE_MIME_TYPE = 'application/json'
Properties
- $mime_type : string
Methods
- can_decode() : bool
- Checks if this ContentHandler object is capable of decoding content.
- can_encode() : bool
- Checks if this ContentHandler object is capable of encoding content.
- decode() : mixed
- Obtains and decodes the content from the format corresponding with this ContentHandler's MIME type.
- encode() : mixed
- Encodes the provided content into the format corresponding with this ContentHandler's MIME type and sets the Content-Type and Content-Length response headers accordingly.
- get_class_fqn() : string
- Obtains the fully qualified name of the called class.
- get_class_shortname() : string
- Obtains the shortname of the called class.
- get_classes_in_namespace() : array<string|int, mixed>
- Obtains all classes associated with this class's current namespace.
- get_content() : mixed
- Obtains the content from the remote client in its unprocessed form.
- get_decode_handler() : ContentHandler
- Obtains a decode-capable ContentHandler for the MIME type provided via the HTTP Content-Type header.
- get_decode_mime_type() : string
- Obtains the MIME type from the HTTP Content-Type header. The Content-Type header controls which ContentHandler will be used to decode request data sent by the remote client. If no content-header was specified, the API will assume a default of application/x-www-form-urlencoded for GET and DELETE requests and a default of application/json for POST, PUT and PATCH requests.
- get_encode_handler() : ContentHandler
- Obtains an encode-capable ContentHandler for the MIME type provided via the HTTP Accept header.
- get_encode_mime_type() : string
- Obtains the MIME type from the HTTP Accept header or assumes the default if no Accept header is specified. The Accept header controls which ContentHandler will be used to encode API responses.
- log_error() : void
- Logs an error to the syslog.
- to_openapi_schema() : array<string|int, mixed>
- Converts this ContentHandler into an OpenAPI 'content' schema. This schema details which MIME types are allowed by the Accept header and also provides a reference to the schema of the Response object.
- _decode() : mixed
- Defines the steps necessary to decode the given content into a PHP native format. This method is generally used to decode the remote client's request from the format indicated in the remote client's request.
- _encode() : mixed
- Defines the steps necessary to encode the given content into the correct format. This method is generally used to encode the remote client's response in the format requested by the client.
Constants
DEFAULT_ENCODE_MIME_TYPE
public
mixed
DEFAULT_ENCODE_MIME_TYPE
= 'application/json'
Properties
$mime_type
public
string
$mime_type
The unique name for this ContentHandler. This name MUST correspond to a valid HTTP Content-Type and/or Accept type. This value is case-insensitive!
Methods
can_decode()
Checks if this ContentHandler object is capable of decoding content.
public
final can_decode() : bool
Tags
Return values
bool —Returns true if this ContentHandler can decode content, otherwise false.
can_encode()
Checks if this ContentHandler object is capable of encoding content.
public
final can_encode() : bool
Tags
Return values
bool —Returns true if this ContentHandler can encode content, otherwise false.
decode()
Obtains and decodes the content from the format corresponding with this ContentHandler's MIME type.
public
final decode([mixed $content = null ]) : mixed
Parameters
- $content : mixed = null
-
The content to be decoded in this ContentHandler's format. If no value is specified, the content will automatically be populated using the
get_content()
method.
Return values
mixed —The content in this ContentHandler's respective format.
encode()
Encodes the provided content into the format corresponding with this ContentHandler's MIME type and sets the Content-Type and Content-Length response headers accordingly.
public
final encode(mixed $content[, Response|null $context = null ]) : mixed
Parameters
- $content : mixed
-
The content to be encoded in this ContentHandler's format. This generally the array representation of the Response object, but can be any content that needs to be encoded.
- $context : Response|null = null
-
The Response object that this content is being encoded for. This useful for ContentHandlers that need more context from the Response object that is being encoded.
Return values
mixed —The content in this ContentHandler's respective format.
get_class_fqn()
Obtains the fully qualified name of the called class.
public
get_class_fqn() : string
Return values
string —The FQN for this object's class.
get_class_shortname()
Obtains the shortname of the called class.
public
get_class_shortname() : string
Return values
string —The shortname for this object's class.
get_classes_in_namespace()
Obtains all classes associated with this class's current namespace.
public
get_classes_in_namespace([bool $shortnames = false ]) : array<string|int, mixed>
Parameters
- $shortnames : bool = false
Return values
array<string|int, mixed> —An array of classes currently in this object's namespace
get_content()
Obtains the content from the remote client in its unprocessed form.
public
get_content() : mixed
Tags
Return values
mixed —The unprocessed content from the remote client.
get_decode_handler()
Obtains a decode-capable ContentHandler for the MIME type provided via the HTTP Content-Type header.
public
static get_decode_handler() : ContentHandler
Tags
Return values
ContentHandlerget_decode_mime_type()
Obtains the MIME type from the HTTP Content-Type header. The Content-Type header controls which ContentHandler will be used to decode request data sent by the remote client. If no content-header was specified, the API will assume a default of application/x-www-form-urlencoded for GET and DELETE requests and a default of application/json for POST, PUT and PATCH requests.
public
static get_decode_mime_type() : string
Return values
string —The MIME type to be used as requested in the HTTP Content-Header header.
get_encode_handler()
Obtains an encode-capable ContentHandler for the MIME type provided via the HTTP Accept header.
public
static get_encode_handler() : ContentHandler
Tags
Return values
ContentHandlerget_encode_mime_type()
Obtains the MIME type from the HTTP Accept header or assumes the default if no Accept header is specified. The Accept header controls which ContentHandler will be used to encode API responses.
public
static get_encode_mime_type() : string
Return values
string —The MIME type to be used as requested in the HTTP Accept header.
log_error()
Logs an error to the syslog.
public
static log_error(string $message) : void
Parameters
- $message : string
-
The error message to write to the syslog
to_openapi_schema()
Converts this ContentHandler into an OpenAPI 'content' schema. This schema details which MIME types are allowed by the Accept header and also provides a reference to the schema of the Response object.
public
to_openapi_schema(Response $response, Endpoint $endpoint) : array<string|int, mixed>
Parameters
- $response : Response
-
The Response object this content schema describes.
- $endpoint : Endpoint
-
The Endpoint object this content schema is being generated for.
Return values
array<string|int, mixed> —This ContentHandler as an OpenAPI 'content' schema for this MIME type in a given Response object.
_decode()
Defines the steps necessary to decode the given content into a PHP native format. This method is generally used to decode the remote client's request from the format indicated in the remote client's request.
protected
_decode([mixed $content = null ]) : mixed
Parameters
- $content : mixed = null
-
The content to be decoded in this ContentHandler's format.
Tags
Return values
mixed —The content in this ContentHandler's respective format.
_encode()
Defines the steps necessary to encode the given content into the correct format. This method is generally used to encode the remote client's response in the format requested by the client.
protected
_encode([mixed $content = null ][, Response|null $context = null ]) : mixed
Parameters
- $content : mixed = null
-
The content to be encoded in this ContentHandler's format. This generally the array representation of the Response object, but can be any content that needs to be encoded.
- $context : Response|null = null
-
The Response object that this content is being encoded for. This useful for ContentHandlers that need more context from the Response object that is being encoded.
Tags
Return values
mixed —The content in this ContentHandler's respective format.