clqms-be/app/Helpers/json_helper.php
root 2bcdf09b55 chore: repo-wide normalization + rules test coverage
Normalize formatting/line endings across configs, controllers, models, tests, and OpenAPI specs.

Update rule expression/rule engine implementation and remove obsolete RuleAction controller/model.

Add unit tests for rule expression syntax and multi-action behavior, and include docs updates.
2026-03-16 07:24:50 +07:00

48 lines
1.5 KiB
PHP

<?php
if (!function_exists('convert_empty_strings_to_null')) {
/**
* 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
*/
function convert_empty_strings_to_null($data) {
if (is_array($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;
}
}
} 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);
}
}