Skip to content

Commit e58fbb9

Browse files
author
Andrey Helldar
committed
Add the ability to convert key names to different cases
See #3
1 parent 584c4b1 commit e58fbb9

30 files changed

+792
-41
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ Simple conversion of an array to a pretty view.
1717
</p>
1818

1919

20+
## Content
21+
22+
* [Installation](#installation)
23+
* [Introduction](#introduction)
24+
* [Using](#using)
25+
* [Saving numeric keys without alignment](#saving-numeric-keys-without-alignment)
26+
* [Saving string keys without alignment](#saving-string-keys-without-alignment)
27+
* [Saving numeric keys with alignment](#saving-numeric-keys-with-alignment)
28+
* [Saving string keys with alignment](#saving-string-keys-with-alignment)
29+
* [Copyright and License](#copyright-and-license)
30+
31+
2032
## Installation
2133

2234
To get the latest version of `Pretty Array` package, simply require the project using [Composer](https://getcomposer.org):
@@ -247,6 +259,7 @@ Result:
247259
]
248260
```
249261

262+
250263
### Saving string keys with alignment
251264

252265
```php
@@ -281,6 +294,50 @@ Result:
281294
```
282295

283296

297+
### Change key case
298+
299+
```php
300+
use Helldar\PrettyArray\Contracts\Caseable;
301+
use Helldar\PrettyArray\Services\Formatter;
302+
303+
$service = Formatter::make();
304+
$service->setCase(Caseable::PASCAL_CASE);
305+
306+
return $service->raw($array);
307+
```
308+
309+
Result:
310+
```text
311+
[
312+
'Foo' => 1,
313+
'Bar' => 2,
314+
'Baz' => 3,
315+
'QweRty' => 'qaz',
316+
'Baq' => [
317+
0 => 'qwe',
318+
1 => 'rty',
319+
'Asd' => 'zxc',
320+
],
321+
'AsdFgh' => [
322+
'FooBarBaz' => 'qwe',
323+
2 => 'rty',
324+
'QawSed' => 'zxc',
325+
],
326+
2 => 'iop',
327+
]
328+
```
329+
330+
The following options are available:
331+
332+
* camelCase (`Helldar\PrettyArray\Contracts\Caseable::CAMEL_CASE`);
333+
* kebab-case (`Helldar\PrettyArray\Contracts\Caseable::KEBAB_CASE`);
334+
* PascalCase (`Helldar\PrettyArray\Contracts\Caseable::PASCAL_CASE`);
335+
* snake_case (`Helldar\PrettyArray\Contracts\Caseable::SNAKE_CASE`);
336+
* no case (`Helldar\PrettyArray\Contracts\Caseable::NO_CASE`). By default;
337+
338+
`NO_CASE` means that key register processing will not be performed.
339+
340+
284341
### Storing file
285342
```php
286343
use Helldar\PrettyArray\Services\File;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"php": "^7.1.3",
1919
"ext-dom": "*",
2020
"ext-mbstring": "*",
21-
"andrey-helldar/support": "^1.15.2"
21+
"andrey-helldar/support": "^1.16.0"
2222
},
2323
"require-dev": {
2424
"phpstan/phpstan": "^0.12.7",

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Concerns/HasCases.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Helldar\PrettyArray\Concerns;
4+
5+
use Helldar\PrettyArray\Exceptions\UnknownCaseTypeException;
6+
use Helldar\Support\Facades\Str;
7+
8+
trait HasCases
9+
{
10+
protected $case = self::NO_CASE;
11+
12+
/**
13+
* @param int $type
14+
*
15+
* @throws \Helldar\PrettyArray\Exceptions\UnknownCaseTypeException
16+
*/
17+
public function setCase(int $type = self::NO_CASE): void
18+
{
19+
if ($type < 0 || $type > 4) {
20+
throw new UnknownCaseTypeException($type);
21+
}
22+
23+
$this->case = $type;
24+
}
25+
26+
protected function convertKeysCase(array &$array): void
27+
{
28+
if ($this->case === static::NO_CASE) {
29+
return;
30+
}
31+
32+
$result = [];
33+
34+
array_walk($array, function ($value, $key) use (&$result) {
35+
$key = $this->convertKeyCase($key);
36+
37+
$result[$key] = $value;
38+
});
39+
40+
$array = $result;
41+
}
42+
43+
protected function convertKeyCase($key)
44+
{
45+
if (! is_string($key)) {
46+
return $key;
47+
}
48+
49+
switch ($this->case) {
50+
case static::CAMEL_CASE:
51+
return Str::camel($key);
52+
case static::SNAKE_CASE:
53+
return Str::snake($key);
54+
case static::KEBAB_CASE:
55+
return Str::snake($key, '-');
56+
case static::PASCAL_CASE:
57+
return Str::studly($key);
58+
default:
59+
return $key;
60+
}
61+
}
62+
}

src/Contracts/Caseable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Helldar\PrettyArray\Contracts;
4+
5+
interface Caseable
6+
{
7+
const CAMEL_CASE = 1;
8+
9+
const KEBAB_CASE = 3;
10+
11+
const NO_CASE = 0;
12+
13+
const PASCAL_CASE = 4;
14+
15+
const SNAKE_CASE = 2;
16+
17+
public function setCase(int $type = self::NO_CASE): void;
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Helldar\PrettyArray\Exceptions;
4+
5+
class UnknownCaseTypeException extends \Exception
6+
{
7+
public function __construct(string $type)
8+
{
9+
parent::__construct("Unknown conversion type: {$type}", 500);
10+
}
11+
}

src/Services/Formatter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Helldar\PrettyArray\Services;
44

5+
use Helldar\PrettyArray\Concerns\HasCases;
6+
use Helldar\PrettyArray\Contracts\Caseable;
57
use Helldar\Support\Facades\Arr;
68

7-
final class Formatter
9+
final class Formatter implements Caseable
810
{
11+
use HasCases;
12+
913
protected $key_as_string = false;
1014

1115
protected $equals_align = false;
@@ -31,6 +35,8 @@ public function setEqualsAlign(): void
3135

3236
public function raw(array $array, int $pad = 1): string
3337
{
38+
$this->convertKeysCase($array);
39+
3440
$keys_size = $this->sizeKeys($array);
3541
$pad_length = $this->pad_length * $pad;
3642
$formatted = '[' . $this->line_break;

0 commit comments

Comments
 (0)