Skip to content

Commit 9a173df

Browse files
author
Andrey Helldar
committed
Fixed a bug with renaming keys in similar cases
1 parent 55b11d8 commit 9a173df

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/Concerns/HasCases.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trait HasCases
1010
protected $case = self::NO_CASE;
1111

1212
/**
13-
* @param int $type
13+
* @param int $type
1414
*
1515
* @throws \Helldar\PrettyArray\Exceptions\UnknownCaseTypeException
1616
*/
@@ -23,10 +23,10 @@ public function setCase(int $type = self::NO_CASE): void
2323
$this->case = $type;
2424
}
2525

26-
protected function convertKeysCase(array &$array): void
26+
protected function convertKeysCase(array $array): array
2727
{
2828
if ($this->case === static::NO_CASE) {
29-
return;
29+
return $array;
3030
}
3131

3232
$result = [];
@@ -37,7 +37,7 @@ protected function convertKeysCase(array &$array): void
3737
$result[$key] = $value;
3838
}
3939

40-
$array = $result;
40+
return $result;
4141
}
4242

4343
protected function convertKeyCase($key)
@@ -47,12 +47,44 @@ protected function convertKeyCase($key)
4747
}
4848

4949
switch ($this->case) {
50+
case static::KEBAB_CASE:
51+
return $this->caseTo(
52+
$this->caseTo($key, static::PASCAL_CASE),
53+
static::KEBAB_CASE
54+
);
5055
case static::CAMEL_CASE:
51-
return Str::camel($key);
56+
return $this->caseTo(
57+
$this->caseTo($key, static::KEBAB_CASE),
58+
static::CAMEL_CASE
59+
);
5260
case static::SNAKE_CASE:
53-
return Str::snake($key);
61+
return $this->caseTo(
62+
$this->caseTo($key, static::PASCAL_CASE),
63+
static::SNAKE_CASE
64+
);
65+
case static::PASCAL_CASE:
66+
return $this->caseTo(
67+
$this->caseTo($key, static::KEBAB_CASE),
68+
static::PASCAL_CASE
69+
);
70+
default:
71+
return $key;
72+
}
73+
}
74+
75+
protected function caseTo($key, int $case = 0)
76+
{
77+
if (! is_string($key)) {
78+
return $key;
79+
}
80+
81+
switch ($case) {
82+
case static::CAMEL_CASE:
83+
return Str::camel($key);
5484
case static::KEBAB_CASE:
5585
return Str::snake($key, '-');
86+
case static::SNAKE_CASE:
87+
return Str::snake($key);
5688
case static::PASCAL_CASE:
5789
return Str::studly($key);
5890
default:

src/Services/Formatter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
final class Formatter implements Caseable
1111
{
12-
use HasCases, HasCastable;
12+
use HasCases;
13+
use HasCastable;
1314

1415
protected $key_as_string = false;
1516

@@ -36,7 +37,7 @@ public function setEqualsAlign(): void
3637

3738
public function raw(array $array, int $pad = 1): string
3839
{
39-
$this->convertKeysCase($array);
40+
$array = $this->convertKeysCase($array);
4041

4142
$keys_size = $this->sizeKeys($array);
4243
$pad_length = $this->pad_length * $pad;

0 commit comments

Comments
 (0)