API
extends RESTful
in package
[$api = $this->core->loadClass('RESTful');] Class CFI to handle APIs. It is been used in Core APIs last_update: 20200502
Table of Contents
Properties
- $apiName : mixed
- $auth : mixed
- $code : mixed
- $codeLib : mixed
- $codeLibError : mixed
- $contentTypeReturn : mixed
- $core : Core7|null
- $dv : DataValidation
- $error : mixed
- $errorMsg : mixed
- $extra_headers : mixed
- $formParams : mixed
- $message : mixed
- $method : mixed
- $ok : mixed
- $org_id : mixed
- $params : mixed
- $rawData : mixed
- $referer : mixed
- $requestHeaders : mixed
- $returnData : mixed
- $rewrite : mixed
- $service : mixed
- $serviceParam : mixed
- $url : mixed
- $urlParams : mixed
Methods
- __construct() : mixed
- addCodeLib() : mixed
- Add a code for JSON output
- addHeader() : mixed
- addReturnData() : mixed
- Add a new record to be returned
- checkBasicAuthSecurity() : bool
- Check if an Authorization-Header Basic has been sent and match with any core.system.authorizations config var.
- checkCloudFrameWorkSecurity() : mixed
- checkFormParamsFromModel() : bool|void
- Evaluate $data and if it is correct using $model. It uses the class DataValidation
- checkMandatoryFormParam() : false|mixed|string
- Validate a $this->formParams[$key] value as mandatory
- checkMandatoryFormParams() : bool|void
- Check if it is received mandatory params
- checkMandatoryParam() : bool
- Validate that a specific parameter exist: /{end-point}/parameter[0]/parameter[1]/parameter[2]/.
- checkMethod() : mixed
- existBasicAuth() : mixed
- getCloudFrameWorkSecurityInfo() : mixed
- getCodeLib() : mixed
- Return the structure of $code in $this->codeLib if it exists
- getCodeLibError() : int|mixed
- Return the structure of $code in $this->codeLibError if it exists
- getFormParamater() : mixed
- Return the value of $this->formParams[$var]. Return null if $var does not exist
- getHeader() : string
- Return the user header key value. In PHP those are $_SERVER["HTTP_$headerKey}"].
- getHeaderAuthorization() : null|string
- Returns the info of the header Authorization if it exists, otherwise null
- getHeaders() : array<string|int, mixed>
- Return all the header keys sent by the user. In PHP those are $_SERVER['HTTP_{varname}']
- getHeadersToResend() : mixed
- getRequestHeader() : mixed
- getResponseHeader() : mixed
- Translate Error codes into headers.
- getResponseHeaders() : mixed
- getReturnCode() : int|string
- Return the code applied with setError and defined in __codes
- getReturnStatus() : mixed
- getUrlPathParamater() : mixed
- Return the value of $this->params[$var]. Return null if $var does not exist
- main() : mixed
- rewriteReturnResponse() : mixed
- send() : mixed
- Echo the result
- sendCorsHeaders() : mixed
- Setup CORS Headers to allow Ajax Web Calls
- sendHeaders() : mixed
- setAuth() : mixed
- setError() : mixed
- Set and error to be return in JSON format with status $returnStatus
- setErrorFromCodelib() : mixed
- Set an error using $code
- setReturnCode() : mixed
- Assign a code
- setReturnData() : mixed
- Init the data to be returned
- setReturnFormat() : mixed
- setReturnResponse() : mixed
- setReturnStatus() : mixed
- updateReturnResponse() : mixed
- useFunction() : bool
- Execute a method if $method is defined.
- validatePostData() : bool
- Check the form paramters received based in a json model
- validatePutData() : bool|void
- Validate data received in $this->formParams to update a record
- validateValue() : mixed
Properties
$apiName
public
mixed
$apiName
= ''
$auth
public
mixed
$auth
= \true
$code
public
mixed
$code
= \null
$codeLib
public
mixed
$codeLib
= []
$codeLibError
public
mixed
$codeLibError
= []
$contentTypeReturn
public
mixed
$contentTypeReturn
= 'JSON'
$core
public
Core7|null
$core
= \null
$dv
public
DataValidation
$dv
$error
public
mixed
$error
= 0
$errorMsg
public
mixed
$errorMsg
= []
$extra_headers
public
mixed
$extra_headers
= []
$formParams
public
mixed
$formParams
= array()
$message
public
mixed
$message
= ""
$method
public
mixed
$method
= ''
$ok
public
mixed
$ok
= 200
$org_id
public
mixed
$org_id
= ''
$params
public
mixed
$params
= array()
$rawData
public
mixed
$rawData
= array()
$referer
public
mixed
$referer
= \null
$requestHeaders
public
mixed
$requestHeaders
= array()
$returnData
public
mixed
$returnData
= \null
$rewrite
public
mixed
$rewrite
= []
$service
public
mixed
$service
= ''
$serviceParam
public
mixed
$serviceParam
= ''
$url
public
mixed
$url
= ''
$urlParams
public
mixed
$urlParams
= ''
Methods
__construct()
public
__construct(Core7 &$core[, mixed $apiUrl = '/h/api' ]) : mixed
Parameters
- $core : Core7
- $apiUrl : mixed = '/h/api'
addCodeLib()
Add a code for JSON output
public
addCodeLib( $code, $msg[, int $error = 400 ][, null $model = null ]) : mixed
Parameters
addHeader()
public
addHeader(mixed $key, mixed $value) : mixed
Parameters
- $key : mixed
- $value : mixed
addReturnData()
Add a new record to be returned
public
addReturnData( $value) : mixed
Parameters
checkBasicAuthSecurity()
Check if an Authorization-Header Basic has been sent and match with any core.system.authorizations config var.
public
checkBasicAuthSecurity([string $id = '' ]) : bool
Parameters
- $id : string = ''
Return values
boolcheckCloudFrameWorkSecurity()
public
checkCloudFrameWorkSecurity([mixed $time = 0 ][, mixed $id = '' ]) : mixed
Parameters
- $time : mixed = 0
- $id : mixed = ''
checkFormParamsFromModel()
Evaluate $data and if it is correct using $model. It uses the class DataValidation
public
checkFormParamsFromModel( &$model[, bool $all = true ][, string $codelibbase = '' ][, null &$data = null ][, array<string|int, mixed> &$dictionaries = [] ]) : bool|void
Parameters
- $model :
- $all : bool = true
- $codelibbase : string = ''
- $data : null = null
- $dictionaries : array<string|int, mixed> = []
Return values
bool|voidcheckMandatoryFormParam()
Validate a $this->formParams[$key] value as mandatory
public
checkMandatoryFormParam( $key[, string $msg = '' ][, array<string|int, mixed> $values = [] ][, int $min_length = 1 ][, null $code = null ]) : false|mixed|string
Parameters
- $key :
- $msg : string = ''
- $values : array<string|int, mixed> = []
- $min_length : int = 1
- $code : null = null
Return values
false|mixed|stringcheckMandatoryFormParams()
Check if it is received mandatory params
public
checkMandatoryFormParams(array<string|int, mixed> $keys) : bool|void
Parameters
- $keys : array<string|int, mixed>
-
with format: [key1,kye2,..] or [[key1,msg,[allowed,values],min_length],[]]
Return values
bool|voidcheckMandatoryParam()
Validate that a specific parameter exist: /{end-point}/parameter[0]/parameter[1]/parameter[2]/.
public
checkMandatoryParam( $pos[, string $msg = '' ][, array<string|int, mixed> $validation = [] ][, null $code = null ]) : bool
.
Parameters
Return values
boolcheckMethod()
public
checkMethod(mixed $methods[, mixed $msg = '' ]) : mixed
Parameters
- $methods : mixed
- $msg : mixed = ''
existBasicAuth()
public
existBasicAuth() : mixed
getCloudFrameWorkSecurityInfo()
public
getCloudFrameWorkSecurityInfo() : mixed
getCodeLib()
Return the structure of $code in $this->codeLib if it exists
public
getCodeLib( $code) : mixed
Parameters
getCodeLibError()
Return the structure of $code in $this->codeLibError if it exists
public
getCodeLibError( $code) : int|mixed
Parameters
Return values
int|mixedgetFormParamater()
Return the value of $this->formParams[$var]. Return null if $var does not exist
public
getFormParamater(string $var) : mixed
Parameters
- $var : string
Tags
getHeader()
Return the user header key value. In PHP those are $_SERVER["HTTP_$headerKey}"].
public
getHeader(string $headerKey) : string
Parameters
- $headerKey : string
-
The name of the header. Vars like X-VAR or X_VAR are treated like the same
Return values
string —If the key does not exist return empty string
getHeaderAuthorization()
Returns the info of the header Authorization if it exists, otherwise null
public
getHeaderAuthorization() : null|string
Return values
null|stringgetHeaders()
Return all the header keys sent by the user. In PHP those are $_SERVER['HTTP_{varname}']
public
getHeaders() : array<string|int, mixed>
Return values
array<string|int, mixed>getHeadersToResend()
public
getHeadersToResend([mixed $extra_headers = null ]) : mixed
Parameters
- $extra_headers : mixed = null
getRequestHeader()
public
getRequestHeader(mixed $str) : mixed
Parameters
- $str : mixed
getResponseHeader()
Translate Error codes into headers.
public
getResponseHeader() : mixed
Useful info: https://restfulapi.net/http-status-codes/
getResponseHeaders()
public
getResponseHeaders() : mixed
getReturnCode()
Return the code applied with setError and defined in __codes
public
getReturnCode() : int|string
Return values
int|stringgetReturnStatus()
public
getReturnStatus() : mixed
getUrlPathParamater()
Return the value of $this->params[$var]. Return null if $var does not exist
public
getUrlPathParamater(int $var) : mixed
Parameters
- $var : int
Tags
main()
public
main() : mixed
rewriteReturnResponse()
public
rewriteReturnResponse(mixed $response) : mixed
Parameters
- $response : mixed
send()
Echo the result
public
send([bool $pretty = false ][, bool $return = false ][, array<string|int, mixed> $argv = [] ]) : mixed
Parameters
- $pretty : bool = false
-
if true, returns the JSON string with JSON_PRETTY_PRINT
- $return : bool = false
-
if true, instead to echo then return the output.
- $argv : array<string|int, mixed> = []
-
if we are running from a terminal it will receive the command line args.
sendCorsHeaders()
Setup CORS Headers to allow Ajax Web Calls
public
sendCorsHeaders([string $methods = 'GET,POST,PUT' ][, string $origin = '' ][, string $allow_extra_headers = '' ]) : mixed
Parameters
- $methods : string = 'GET,POST,PUT'
- $origin : string = ''
- $allow_extra_headers : string = ''
-
If you need to add headers to be allowed use this variable.
sendHeaders()
public
sendHeaders() : mixed
setAuth()
public
setAuth(mixed $val[, mixed $msg = '' ]) : mixed
Parameters
- $val : mixed
- $msg : mixed = ''
setError()
Set and error to be return in JSON format with status $returnStatus
public
setError(mixed $error[, int $returnStatus = 400 ][, mixed $returnCode = null ][, string $message = '' ]) : mixed
Parameters
- $error : mixed
-
Error to show. It can be a string or and array
- $returnStatus : int = 400
-
integer >=200. 400 by default
- $returnCode : mixed = null
-
optional returnCode
- $message : string = ''
setErrorFromCodelib()
Set an error using $code
public
setErrorFromCodelib( $code[, string $extramsg = '' ]) : mixed
Parameters
Tags
setReturnCode()
Assign a code
public
setReturnCode( $code) : mixed
Parameters
setReturnData()
Init the data to be returned
public
setReturnData( $data) : mixed
Parameters
setReturnFormat()
public
setReturnFormat(mixed $method) : mixed
Parameters
- $method : mixed
setReturnResponse()
public
setReturnResponse(mixed $response) : mixed
Parameters
- $response : mixed
setReturnStatus()
public
setReturnStatus(mixed $status) : mixed
Parameters
- $status : mixed
updateReturnResponse()
public
updateReturnResponse(mixed $response) : mixed
Parameters
- $response : mixed
useFunction()
Execute a method if $method is defined.
public
useFunction(string $method) : bool
Parameters
- $method : string
-
name of the method
Return values
boolvalidatePostData()
Check the form paramters received based in a json model
public
validatePostData(array<string|int, mixed> $model[, string $codelibbase = 'error-form-params' ][, null &$data = null ][, mixed &$dictionaries = [] ]) : bool
Parameters
- $model : array<string|int, mixed>
- $codelibbase : string = 'error-form-params'
- $data : null = null
- $dictionaries : mixed = []
Return values
boolvalidatePutData()
Validate data received in $this->formParams to update a record
public
validatePutData( $model[, string $codelibbase = 'error-form-params' ][, null &$data = null ][, array<string|int, mixed> &$dictionaries = [] ]) : bool|void
Parameters
- $model :
- $codelibbase : string = 'error-form-params'
- $data : null = null
- $dictionaries : array<string|int, mixed> = []
Return values
bool|voidvalidateValue()
public
validateValue(string $field, mix $value, string $type[, string $validation = '' ][, string $msg_error = '' ]) : mixed
Parameters
- $field : string
- $value : mix
- $type : string
- $validation : string = ''
- $msg_error : string = ''