Class Response
A response to a HTTP request.
-
ICanBoogie\HTTP\Response
implements
ICanBoogie\HTTP\ResponseStatus
uses
ICanBoogie\Accessor\AccessorTrait (not available)
Namespace: ICanBoogie\
HTTP
See:
http://tools.ietf.org/html/rfc2616
Located at Response.php
Methods summary
public
__construct( mixed $body
= null, integer|ICanBoogie\HTTP\Status
$status
= self::STATUS_OK, ICanBoogie\HTTP\Headers
|array $headers
= [] )
Initializes the $body
, $status
, $headers
, and $date
properties.
Initializes the $body
, $status
, $headers
, and $date
properties.
Parameters
$body
- The body of the response.
$status
- The status code of the response.
$headers
- The initial header fields of the response.
public
__clone( void )
Clones the $headers
and $status
properties.
Clones the $headers
and $status
properties.
public
__toString( void )
: string
Renders the response as an HTTP string.
Renders the response as an HTTP string.
Returns
string
public
__invoke( void )
Issues the HTTP response.
protected
finalize( ICanBoogie\HTTP\Headers
& $headers
, mixed & $body
)
Finalize the body.
Subclasses might want to override this method if they wish to alter the header or the body
before the response is sent or transformed into a string.
Parameters
$headers
- Reference to the final header.
$body
- Reference to the final body.
protected
send_body( mixed $body
)
Sends response body.
Parameters
protected
set_status( integer|ICanBoogie\HTTP\Status
$status
)
Sets response status code and optionally status message.
Sets response status code and optionally status message.
Parameters
$status
- HTTP status code or HTTP status code and HTTP status message.
protected
get_status( void )
: ICanBoogie\HTTP\Status
Returns the response status.
Returns the response status.
Returns
protected
set_body( string|Closure|null $body
)
Set the response body.
The body can be any data type that can be converted into a string. This includes numeric
and objects implementing the ICanBoogie\HTTP\Response::__toString()
method.
Parameters
Throws
UnexpectedValueException
when the body cannot be converted to a string.
protected
assert_body_is_valid( $body
)
Assert that a body is valid.
Assert that a body is valid.
Parameters
Throws
UnexpectedValueException
if the specified body doesn't meet the requirements.
protected
get_body( void )
: string
Returns the response body.
Returns the response body.
Returns
string
protected
set_location( string|null $url
)
Sets the value of the Location
header field.
Sets the value of the Location
header field.
Parameters
protected
get_location( void )
: string
Returns the value of the Location
header field.
Returns the value of the Location
header field.
Returns
string
protected
set_content_type( string $content_type
)
Sets the value of the Content-Type
header field.
Sets the value of the Content-Type
header field.
Parameters
protected
get_content_type( void )
: ICanBoogie\HTTP\Headers\ContentType
Returns the value of the Content-Type
header field.
Returns the value of the Content-Type
header field.
Returns
protected
set_content_length( integer $length
)
Sets the value of the Content-Length
header field.
Sets the value of the Content-Length
header field.
Parameters
protected
get_content_length( void )
: integer
Returns the value of the Content-Length
header field.
Returns the value of the Content-Length
header field.
Returns
integer
protected
set_date( mixed $time
)
Sets the value of the Date
header field.
Sets the value of the Date
header field.
Parameters
protected
get_date( void )
: ICanBoogie\HTTP\Headers\Date
Returns the value of the Date
header field.
Returns the value of the Date
header field.
Returns
protected
set_age( integer $age
)
Sets the value of the Age
header field.
Sets the value of the Age
header field.
Parameters
protected
get_age( void )
: integer
Returns the age of the response.
Returns the age of the response.
Returns
integer
protected
set_last_modified( mixed $time
)
Sets the value of the Last-Modified
header field.
Sets the value of the Last-Modified
header field.
Parameters
protected
get_last_modified( void )
: ICanBoogie\HTTP\Headers\Date
Returns the value of the Last-Modified
header field.
Returns the value of the Last-Modified
header field.
Returns
protected
set_expires( mixed $time
)
Sets the value of the Expires
header field.
Sets the value of the Expires
header field.
The method updates the max-age
Cache Control directive accordingly.
Parameters
protected
get_expires( void )
: ICanBoogie\HTTP\Headers\Date
Returns the value of the Expires
header field.
Returns the value of the Expires
header field.
Returns
protected
set_etag( string|null $value
)
Sets the value of the ETag
header field.
Sets the value of the ETag
header field.
Parameters
protected
get_etag( void )
: string|null
Returns the value of the ETag
header field.
Returns the value of the ETag
header field.
Returns
string|null
protected
set_cache_control( string $cache_directives
)
Sets the directives of the Cache-Control
header field.
Sets the directives of the Cache-Control
header field.
Parameters
protected
set_ttl( integer|null $seconds
)
Sets the response's time-to-live for shared caches.
Sets the response's time-to-live for shared caches.
This method adjusts the Cache-Control/s-maxage directive.
Parameters
$seconds
- The number of seconds.
protected
get_ttl( void )
: integer|null
Returns the response's time-to-live in seconds.
Returns the response's time-to-live in seconds.
When the responses TTL is <= 0, the response may not be served from cache without first
re-validating with the origin.
Returns
integer|null
The number of seconds to live, or null
is no freshness information
is present.
protected
get_is_validateable( void )
: boolean
Whether the response includes header fields that can be used to validate the response
with the origin server using a conditional GET request.
Whether the response includes header fields that can be used to validate the response
with the origin server using a conditional GET request.
Returns
boolean
protected
get_is_cacheable( void )
: boolean
Whether the response is worth caching under any circumstance.
Whether the response is worth caching under any circumstance.
Responses marked private with an explicit Cache-Control
directive are considered
not cacheable.
Responses with neither a freshness lifetime (Expires, max-age) nor cache validator
(Last-Modified
, ETag
) are considered not cacheable.
Returns
boolean
protected
get_is_fresh( void )
: boolean
Whether the response is fresh.
Whether the response is fresh.
Returns
boolean
Constants summary
STATUS_ACCEPTED
,
STATUS_BAD_GATEWAY
,
STATUS_BAD_REQUEST
,
STATUS_CONFLICT
,
STATUS_CONTINUE
,
STATUS_CREATED
,
STATUS_EXPECTATION_FAILED
,
STATUS_FORBIDDEN
,
STATUS_FOUND
,
STATUS_GATEWAY_TIMEOUT
,
STATUS_GONE
,
STATUS_HTTP_VERSION_NOT_SUPPORTED
,
STATUS_INTERNAL_SERVER_ERROR
,
STATUS_I_M_A_TEAPOT
,
STATUS_LENGTH_REQUIRED
,
STATUS_METHOD_NOT_ALLOWED
,
STATUS_MOVED_PERMANENTLY
,
STATUS_MULTIPLE_CHOICES
,
STATUS_NON_AUTHORITATIVE_INFORMATION
,
STATUS_NOT_ACCEPTABLE
,
STATUS_NOT_FOUND
,
STATUS_NOT_IMPLEMENTED
,
STATUS_NOT_MODIFIED
,
STATUS_NO_CONTENT
,
STATUS_OK
,
STATUS_PARTIAL_CONTENT
,
STATUS_PAYMENT_REQUIRED
,
STATUS_PRECONDITION_FAILED
,
STATUS_PROXY_AUTHENTICATION_REQUIRED
,
STATUS_REQUESTED_RANGE_NOT_SATISFIABLE
,
STATUS_REQUEST_ENTITY_TOO_LARGE
,
STATUS_REQUEST_TIMEOUT
,
STATUS_REQUEST_URI_TOO_LONG
,
STATUS_RESET_CONTENT
,
STATUS_SEE_OTHER
,
STATUS_SERVICE_UNAVAILABLE
,
STATUS_SWITCHING_PROTOCOLS
,
STATUS_TEMPORARY_REDIRECT
,
STATUS_UNAUTHORIZED
,
STATUS_UNSUPPORTED_MEDIA_TYPE
,
STATUS_USE_PROXY
Properties summary
public
$version
: string
The HTTP protocol version (1.0 or 1.1), defaults to '1.1'
The HTTP protocol version (1.0 or 1.1), defaults to '1.1'
'1.1'
Magic properties
public
$body
: mixed
The body of the response.
public
$ttl
: integer|null
Adjusts the s-maxage
part of the Cache-Control
header field definition according to the Age
header field definition.
public
$age
: integer|null
Shortcut to the Age
header field definition.
public
$content_length
: integer
Shortcut to the Content-Length
header field definition.
public
$content_type
: ICanBoogie\HTTP\Headers\ContentType
Shortcut to the Content-Type
header field definition.
public
$etag
: string|null
Shortcut to the ETag
header field definition.
public
$location
: string|null
Shortcut to the Location
header field definition.
public read-only
$is_cacheable
: boolean
public read-only
$is_fresh
: boolean
public read-only
$is_validateable
: boolean