clqms-be/app/Traits/ResponseTrait.php
mahdahar 30c0c538d6 refactor: Remove redundant ValueSet call and convert global function to private method
- TestsController: Remove duplicate rangeTypeOptions assignment that was being
  set inside the NUM condition block, causing it to be set unnecessarily
  when it's also handled elsewhere

- ResponseTrait: Convert global helper function convert_empty_strings_to_null()
  to private class method convertEmptyStringsToNull() for better encapsulation
  and to avoid dependency on global functions

- Database schema: Update clqms_database.dbml with accurate table structure
  derived from app/Models/ directory, reorganizing tables by functional
  categories (Patient, Visit, Organization, Location, Test, Specimen,
  Order, Contact management)
2026-02-18 11:07:00 +07:00

64 lines
2.0 KiB
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 = $this->convertEmptyStringsToNull($data);
}
return $this->baseRespond($data, $status, $message);
}
/**
* Recursively convert empty strings to null in arrays or objects
*
* @param mixed $data The data to process (array, object, or scalar)
* @return mixed The processed data with empty strings converted to null
*/
private function convertEmptyStringsToNull($data)
{
if (is_array($data)) {
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = $this->convertEmptyStringsToNull($value);
} elseif (is_object($value)) {
$data[$key] = $this->convertEmptyStringsToNull($value);
} elseif ($value === '') {
$data[$key] = null;
}
}
} elseif (is_object($data)) {
foreach ($data as $key => $value) {
if (is_array($value)) {
$data->$key = $this->convertEmptyStringsToNull($value);
} elseif (is_object($value)) {
$data->$key = $this->convertEmptyStringsToNull($value);
} elseif ($value === '') {
$data->$key = null;
}
}
}
return $data;
}
}