From 425595f5c017951dbc7fd12e878689c5ca5c3ded Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Wed, 18 Feb 2026 10:15:47 +0700 Subject: [PATCH] feat: Implement custom ResponseTrait with automatic empty string to null conversion - Create App\Traits\ResponseTrait that wraps CodeIgniter\API\ResponseTrait - Add json_helper with convert_empty_strings_to_null() and prepare_json_response() functions - Replace all imports of CodeIgniter\API\ResponseTrait with App\Traits\ResponseTrait across all controllers - Add 'json' helper to BaseController helpers array - Ensure consistent API response formatting across the application --- app/Controllers/AreaGeoController.php | 2 +- app/Controllers/AuthController.php | 2 +- app/Controllers/AuthV2Controller.php | 2 +- app/Controllers/BaseController.php | 4 +- app/Controllers/Contact/ContactController.php | 2 +- .../Contact/MedicalSpecialtyController.php | 2 +- .../Contact/OccupationController.php | 2 +- app/Controllers/CounterController.php | 2 +- app/Controllers/DashboardController.php | 2 +- app/Controllers/EdgeController.php | 2 +- app/Controllers/HomeController.php | 2 +- app/Controllers/LocationController.php | 2 +- app/Controllers/OrderTestController.php | 2 +- .../Organization/AccountController.php | 2 +- .../Organization/DepartmentController.php | 2 +- .../Organization/DisciplineController.php | 2 +- .../Organization/SiteController.php | 2 +- .../Organization/WorkstationController.php | 2 +- app/Controllers/PatVisitController.php | 2 +- app/Controllers/Patient/PatientController.php | 2 +- .../Result/ResultValueSetController.php | 2 +- app/Controllers/ResultController.php | 2 +- app/Controllers/SampleController.php | 2 +- .../Specimen/ContainerDefController.php | 2 +- .../Specimen/SpecimenCollectionController.php | 2 +- .../Specimen/SpecimenController.php | 2 +- .../Specimen/SpecimenPrepController.php | 2 +- .../Specimen/SpecimenStatusController.php | 2 +- app/Controllers/Test/DemoOrderController.php | 2 +- app/Controllers/Test/TestMapController.php | 2 +- app/Controllers/TestsController.php | 2 +- app/Controllers/ValueSetController.php | 2 +- app/Controllers/ValueSetDefController.php | 2 +- app/Controllers/ZonesController.php | 2 +- app/Helpers/json_helper.php | 47 +++++++++++++++++++ app/Traits/ResponseTrait.php | 31 ++++++++++++ 36 files changed, 113 insertions(+), 35 deletions(-) create mode 100644 app/Helpers/json_helper.php create mode 100644 app/Traits/ResponseTrait.php diff --git a/app/Controllers/AreaGeoController.php b/app/Controllers/AreaGeoController.php index 009311c..b316c9f 100644 --- a/app/Controllers/AreaGeoController.php +++ b/app/Controllers/AreaGeoController.php @@ -1,7 +1,7 @@ */ - protected $helpers = []; + protected $helpers = ['json']; /** * Be sure to declare properties for any property fetch you initialized. diff --git a/app/Controllers/Contact/ContactController.php b/app/Controllers/Contact/ContactController.php index 22bcfcf..f3286c6 100644 --- a/app/Controllers/Contact/ContactController.php +++ b/app/Controllers/Contact/ContactController.php @@ -1,7 +1,7 @@ $value) { + if (is_array($value)) { + $data[$key] = convert_empty_strings_to_null($value); + } elseif (is_object($value)) { + $data[$key] = convert_empty_strings_to_null($value); + } elseif ($value === '') { + $data[$key] = null; + } + } + } elseif (is_object($data)) { + foreach ($data as $key => $value) { + if (is_array($value)) { + $data->$key = convert_empty_strings_to_null($value); + } elseif (is_object($value)) { + $data->$key = convert_empty_strings_to_null($value); + } elseif ($value === '') { + $data->$key = null; + } + } + } + return $data; + } +} + +if (!function_exists('prepare_json_response')) { + /** + * Prepare data for JSON response by converting empty strings to null + * This is a convenience wrapper around convert_empty_strings_to_null + * + * @param mixed $data The data to prepare for JSON response + * @return mixed The processed data ready for JSON encoding + */ + function prepare_json_response($data) { + return convert_empty_strings_to_null($data); + } +} diff --git a/app/Traits/ResponseTrait.php b/app/Traits/ResponseTrait.php new file mode 100644 index 0000000..ecfe63f --- /dev/null +++ b/app/Traits/ResponseTrait.php @@ -0,0 +1,31 @@ +baseRespond($data, $status, $message); + } +}