tinyqc/app/Helpers/stringcase_helper.php
2026-02-12 09:01:59 +07:00

48 lines
1.2 KiB
PHP
Executable File

<?php
if (!function_exists('camel_to_snake')) {
function camel_to_snake(string $string): string
{
$pattern = '/([a-z])([A-Z])/';
return strtolower(preg_replace($pattern, '$1_$2', $string));
}
}
if (!function_exists('camel_to_snake_array')) {
function camel_to_snake_array(array $data): array
{
$converted = [];
foreach ($data as $key => $value) {
$snakeKey = camel_to_snake($key);
$converted[$snakeKey] = $value;
}
return $converted;
}
}
if (!function_exists('snake_to_camel')) {
function snake_to_camel(string $string): string
{
$parts = explode('_', $string);
$camel = array_map(function ($part, $index) {
if ($index === 0) {
return $part;
}
return ucfirst($part);
}, $parts, array_keys($parts));
return implode('', $camel);
}
}
if (!function_exists('snake_to_camel_array')) {
function snake_to_camel_array(array $data): array
{
$converted = [];
foreach ($data as $key => $value) {
$camelKey = snake_to_camel($key);
$converted[$camelKey] = $value;
}
return $converted;
}
}