32 lines
788 B
PHP
32 lines
788 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Traits;
|
||
|
|
|
||
|
|
use CodeIgniter\API\ResponseTrait as BaseResponseTrait;
|
||
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
||
|
|
|
||
|
|
trait ResponseTrait
|
||
|
|
{
|
||
|
|
use BaseResponseTrait {
|
||
|
|
respond as baseRespond;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a response with automatic conversion of empty strings to null
|
||
|
|
*
|
||
|
|
* @param mixed $data
|
||
|
|
* @param int $status
|
||
|
|
* @param string $message
|
||
|
|
* @return ResponseInterface
|
||
|
|
*/
|
||
|
|
public function respond($data = null, int $status = 200, string $message = '')
|
||
|
|
{
|
||
|
|
// Convert empty strings to null in the data
|
||
|
|
if ($data !== null && (is_array($data) || is_object($data))) {
|
||
|
|
$data = convert_empty_strings_to_null($data);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->baseRespond($data, $status, $message);
|
||
|
|
}
|
||
|
|
}
|