Skip to content

Commit 1eef478

Browse files
committed
fix: added studly case response option
1 parent f7a3ffc commit 1eef478

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
],
1717
"require": {
1818
"php": "^8.1",
19-
"laravel/framework": "^9.0|^10.0"
19+
"laravel/framework": "^9.0|^10.0|^11.0"
2020
},
2121
"require-dev": {
2222
"doctrine/dbal": ">=2.3",
2323
"mockery/mockery": "^1.0",
2424
"nunomaduro/larastan": "^2.0",
25-
"orchestra/testbench": "^7.0|^8.0",
25+
"orchestra/testbench": "^7.0|^8.0|^9.0",
2626
"phpunit/phpunit": "^9.5|^10.0"
2727
},
2828
"autoload": {

src/Helpers.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ public static function camelCaseArrayKeys(array $array): array
6161
return $array;
6262
}
6363

64+
65+
public static function studlyCaseArrayKeys(array $array): array
66+
{
67+
$keys = array_keys($array);
68+
foreach ($keys as $key) {
69+
$value = &$array[$key]; //reference and not copy so that keeps any modifiers
70+
unset($array[$key]);
71+
72+
if (is_array($value)) {
73+
$value = self::studlyCaseArrayKeys($value);
74+
}
75+
76+
$newKey = Str::of($key)->studly()->ucfirst()->toString();
77+
$array[$newKey] = $value;
78+
unset($value); //cleanup
79+
}
80+
81+
return $array;
82+
}
83+
6484
/**
6585
* Snake cases array keys.
6686
*
@@ -124,6 +144,16 @@ public static function camel($value)
124144
return Str::camel($value);
125145
}
126146

147+
public static function studly($value)
148+
{
149+
// Preserve all caps
150+
if (strtoupper($value) === $value) {
151+
return $value;
152+
}
153+
154+
return Str::studly($value);
155+
}
156+
127157

128158
protected static function filterExtraFields(array $fields, ?string $fieldKey): array
129159
{
@@ -141,7 +171,7 @@ protected static function filterExtraFields(array $fields, ?string $fieldKey): a
141171

142172
protected static function fetchRequestFieldValues($request, string $field, ?string $fieldKey = null): ?array
143173
{
144-
if(!$request->has($field)){
174+
if (! $request->has($field)) {
145175
return null;
146176
}
147177
return self::filterExtraFields(explode(",", $request->input($field)), $fieldKey);

src/Http/Resources/Contracts/CaseFormat.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ trait CaseFormat
1010
protected function caseFormat(Request $request, array $data): array
1111
{
1212
$case = $request->header('X-Accept-Case-Type');
13-
if (!is_string($case)) {
13+
if (! is_string($case)) {
1414
return $data;
1515
}
16-
return match (str($case)->lower()) {
16+
return match (str($case)->lower()->toString()) {
1717
'camel', 'camel-case' => Helpers::camelCaseArrayKeys($data),
1818
'snake', 'snake-case' => Helpers::snakeCaseArrayKeys($data),
19+
'studly', 'studly-case' => Helpers::studlyCaseArrayKeys($data),
1920
default => $data,
2021
};
2122
}

0 commit comments

Comments
 (0)