src/Helpers/MicroServiceHelper.php
public static function jsonResponseFormatter($type, $data, $code = 200, $status = Status::OK, $message = '')
    {
        // Validate the arguments.
        if ($status == Status::OK && empty($type)) {
            throw new Exception('Cannot prepare response. No type specified');
        }
        else {
            $type = preg_replace('/[^A-Za-z]/si', '_', strtolower($type));
        }

        // Ensure the data is in an array.
        if (is_object($data)) {
            $data = [(array) $data];
        }

        // Prepare the response object.
        $response = new stdClass();
        $response->status = $status;
        $response->code = $code;
        $response->message = $message;

        // Return data in an array.
        if (!empty($data)) {
            $response->data = new stdClass();
            $response->data->{$type} = $data;
        } elseif (!empty($type)) {
            // Return an empty array even if there are no data.
            $response->data = new stdClass();
            $response->data->{$type} = [];
        }

        return $response;
    }
public static function jsonResponseFormatter($type, $data, $code = 200, $status = Status::OK, $message = '')
    {
        // Validate the arguments.
        if ($status == Status::OK && empty($type)) {
            throw new Exception('Cannot prepare response. No type specified');
        }
        else {
            $type = preg_replace('/[^A-Za-z]/si', '_', strtolower($type));
        }

        // Ensure the data is in an array.
        if (is_object($data)) {
            $data = [(array) $data];
        }

        // Prepare the response object.
        $response = new stdClass();
        $response->status = $status;
        $response->code = $code;
        $response->message = $message;

        // Return data in an array.
        if (!empty($data)) {
            $response->data = new stdClass();
            $response->data->{$type} = $data;
        } elseif (!empty($type)) {
            // Return an empty array even if there are no data.
            $response->data = new stdClass();
            $response->data->{$type} = [];
        }

        return $response;
    }
public static function jsonResponseFormatter ( $ type , $ data , $ code = 200 , $ status = Status :: OK , $ message = '' ) { // Validate the arguments. if ( $ status == Status :: OK && empty ( $ type ) ) { throw new Exception ( 'Cannot prepare response. No type specified' ) ; } else { $ type = preg_replace ( '/[^A-Za-z]/si' , '_' , strtolower ( $ type ) ) ; } // Ensure the data is in an array. if ( is_object ( $ data ) ) { $ data = [ ( array ) $ data ] ; } // Prepare the response object. $ response = new stdClass ( ) ; $ response -> status = $ status ; $ response -> code = $ code ; $ response -> message = $ message ; // Return data in an array. if ( ! empty ( $ data ) ) { $ response -> data = new stdClass ( ) ; $ response -> data -> { $ type } = $ data ; } elseif ( ! empty ( $ type ) ) { // Return an empty array even if there are no data. $ response -> data = new stdClass ( ) ; $ response -> data -> { $ type } = [ ] ; } return $ response ; }
Prepare a response an endpoint.

This ensures that all API endpoints return data in a standardised format:

{
"status": "ok", - Can contain any string. Usually 'ok', 'error' etc.
"code": 200, - A HTTP status code.
"message": "", - A message string elaborating on the status.
"data": {[ - A collection of return data. Can be omitted in the event
]}           an error occurred.
}

@param string $type
The type of data being returned. Will be used to name the collection.
@param object|array|NULL $data
The data to return. Will always be parsed into a collection.
@param int $code
HTTP status code for the response.
@param string $status
A short status message. Examples: 'OK', 'Bad Request', 'Not Found'.
@param string $message
A more detailed status message.

@return object
The formatted response object.

@throws Exception
Prepare a response an endpoint .