Skip to content

Commit

Permalink
Fixed a bug with renaming keys in similar cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Helldar committed Jul 24, 2020
1 parent 55b11d8 commit 9a173df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
44 changes: 38 additions & 6 deletions src/Concerns/HasCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait HasCases
protected $case = self::NO_CASE;

/**
* @param int $type
* @param int $type
*
* @throws \Helldar\PrettyArray\Exceptions\UnknownCaseTypeException
*/
Expand All @@ -23,10 +23,10 @@ public function setCase(int $type = self::NO_CASE): void
$this->case = $type;
}

protected function convertKeysCase(array &$array): void
protected function convertKeysCase(array $array): array
{
if ($this->case === static::NO_CASE) {
return;
return $array;
}

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

$array = $result;
return $result;
}

protected function convertKeyCase($key)
Expand All @@ -47,12 +47,44 @@ protected function convertKeyCase($key)
}

switch ($this->case) {
case static::KEBAB_CASE:
return $this->caseTo(
$this->caseTo($key, static::PASCAL_CASE),
static::KEBAB_CASE
);
case static::CAMEL_CASE:
return Str::camel($key);
return $this->caseTo(
$this->caseTo($key, static::KEBAB_CASE),
static::CAMEL_CASE
);
case static::SNAKE_CASE:
return Str::snake($key);
return $this->caseTo(
$this->caseTo($key, static::PASCAL_CASE),
static::SNAKE_CASE
);
case static::PASCAL_CASE:
return $this->caseTo(
$this->caseTo($key, static::KEBAB_CASE),
static::PASCAL_CASE
);
default:
return $key;
}
}

protected function caseTo($key, int $case = 0)
{
if (! is_string($key)) {
return $key;
}

switch ($case) {
case static::CAMEL_CASE:
return Str::camel($key);
case static::KEBAB_CASE:
return Str::snake($key, '-');
case static::SNAKE_CASE:
return Str::snake($key);
case static::PASCAL_CASE:
return Str::studly($key);
default:
Expand Down
5 changes: 3 additions & 2 deletions src/Services/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

final class Formatter implements Caseable
{
use HasCases, HasCastable;
use HasCases;
use HasCastable;

protected $key_as_string = false;

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

public function raw(array $array, int $pad = 1): string
{
$this->convertKeysCase($array);
$array = $this->convertKeysCase($array);

$keys_size = $this->sizeKeys($array);
$pad_length = $this->pad_length * $pad;
Expand Down

0 comments on commit 9a173df

Please sign in to comment.