ApiResponse
in package
Api Respnse support JSON format only.
Table of Contents
Properties
- $errors : array<string|int, mixed>
- Errors list
- $prettyFormat : bool
- pretty format json
- $raw : bool
- Raw json response
- $response : ResponseInterface
- Request response object
- $result : array<string|int, mixed>
- response result
Methods
- __construct() : mixed
- Constructor
- addErrors() : void
- Add errors
- clearErrors() : void
- Clear all errors.
- createErrorResponse() : self
- Create error response
- createFromArray() : self|null
- Create api response
- createFromJson() : self|null
- Create from json text
- field() : ApiResponse
- Set result field
- getErrorCount() : int
- Return errors count
- getField() : mixed
- Get field
- getResponse() : ResponseInterface
- Return request response
- getResponseJson() : string
- Return json
- getResult() : mixed
- Get result
- hasError() : bool
- Return true if response have error
- message() : ApiResponse
- Set result message
- setClientResponse() : void
- Set client response
- setCode() : void
- Set status
- setError() : void
- Set error message
- setErrors() : void
- Set errors
- setResponse() : mixed
- Set response
- setResult() : ApiResponse
- Set response result
- setResultField() : void
- Set field to result array
- setStatus() : void
- Set status
- toArray() : array<string|int, mixed>
- Convert to array
- useJsonPrettyformat() : self
- Set json pretty format to true
- withError() : ApiResponse
- Set error message
Properties
$errors
Errors list
protected
array<string|int, mixed>
$errors
$prettyFormat
pretty format json
protected
bool
$prettyFormat
$raw
Raw json response
protected
bool
$raw
$response
Request response object
protected
ResponseInterface
$response
$result
response result
protected
array<string|int, mixed>
$result
Methods
__construct()
Constructor
public
__construct([ResponseInterface|null $response = null ]) : mixed
Parameters
- $response : ResponseInterface|null = null
addErrors()
Add errors
public
addErrors(array<string|int, mixed> $errors) : void
Parameters
- $errors : array<string|int, mixed>
clearErrors()
Clear all errors.
public
clearErrors() : void
createErrorResponse()
Create error response
public
static createErrorResponse([string $error = '' ][, int $errorCode = 404 ][, ResponseInterface|null $response = null ]) : self
Parameters
- $error : string = ''
- $errorCode : int = 404
- $response : ResponseInterface|null = null
Return values
selfcreateFromArray()
Create api response
public
static createFromArray(array<string|int, mixed> $data[, ResponseInterface|null $response = null ]) : self|null
Parameters
- $data : array<string|int, mixed>
- $response : ResponseInterface|null = null
Return values
self|nullcreateFromJson()
Create from json text
public
static createFromJson(string $json[, ResponseInterface|null $response = null ]) : self|null
Parameters
- $json : string
- $response : ResponseInterface|null = null
Return values
self|nullfield()
Set result field
public
field(string $name, mixed $value) : ApiResponse
Parameters
- $name : string
- $value : mixed
Return values
ApiResponsegetErrorCount()
Return errors count
public
getErrorCount() : int
Return values
intgetField()
Get field
public
getField(string $name[, mixed $default = null ]) : mixed
Parameters
- $name : string
- $default : mixed = null
getResponse()
Return request response
public
getResponse([bool $raw = false ]) : ResponseInterface
Parameters
- $raw : bool = false
Return values
ResponseInterfacegetResponseJson()
Return json
public
getResponseJson() : string
Return values
stringgetResult()
Get result
public
getResult() : mixed
hasError()
Return true if response have error
public
hasError() : bool
Return values
boolmessage()
Set result message
public
message(string $message) : ApiResponse
Parameters
- $message : string
Return values
ApiResponsesetClientResponse()
Set client response
public
setClientResponse([ResponseInterface|null $response = null ]) : void
Parameters
- $response : ResponseInterface|null = null
setCode()
Set status
public
setCode(mixed $code) : void
Parameters
- $code : mixed
setError()
Set error message
public
setError(string $errorMessage[, bool $condition = true ]) : void
Parameters
- $errorMessage : string
- $condition : bool = true
setErrors()
Set errors
public
setErrors(array<string|int, mixed> $errors) : void
Parameters
- $errors : array<string|int, mixed>
setResponse()
Set response
public
setResponse(bool $condition, array<string|int, mixed>|string|Closure $data, string|Closure $error) : mixed
Parameters
- $condition : bool
- $data : array<string|int, mixed>|string|Closure
- $error : string|Closure
setResult()
Set response result
public
setResult(mixed $data) : ApiResponse
Parameters
- $data : mixed
Return values
ApiResponsesetResultField()
Set field to result array
public
setResultField(string $name, mixed $value) : void
Parameters
- $name : string
- $value : mixed
setStatus()
Set status
public
setStatus(string $status) : void
Parameters
- $status : string
toArray()
Convert to array
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>useJsonPrettyformat()
Set json pretty format to true
public
useJsonPrettyformat() : self
Return values
selfwithError()
Set error message
public
withError(string $errorMessage[, bool $condition = true ]) : ApiResponse
Parameters
- $errorMessage : string
- $condition : bool = true