Skip to content

Commit 6c84e24

Browse files
Merge pull request #28 from TheDragonCode/4.x
Added JSON formatting
2 parents 8876866 + 0d4b6f6 commit 6c84e24

File tree

16 files changed

+401
-89
lines changed

16 files changed

+401
-89
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,44 @@ return [
399399
];
400400
```
401401

402+
#### As JSON
403+
404+
```php
405+
use DragonCode\PrettyArray\Services\File;
406+
use DragonCode\PrettyArray\Services\Formatter;
407+
408+
$service = Formatter::make();
409+
410+
$service->asJson();
411+
412+
$formatted = $service->raw($array);
413+
414+
File::make($formatted)
415+
->store('foo.json');
416+
```
417+
418+
Result in stored file `foo.json`:
419+
420+
```json
421+
{
422+
"foo": 1,
423+
"bar": 2,
424+
"baz": 3,
425+
"qwerty": "qaz",
426+
"baq": {
427+
"0": "qwe",
428+
"1": "rty",
429+
"asd": "zxc"
430+
},
431+
"asdfgh": {
432+
"foobarbaz": "qwe",
433+
"2": "rty",
434+
"qawsed": "zxc"
435+
},
436+
"2": "'iop'"
437+
}
438+
```
439+
402440
## License
403441

404442
This package is licensed under the [MIT License](LICENSE).

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
"php": "^8.0",
4545
"ext-dom": "*",
4646
"ext-mbstring": "*",
47-
"dragon-code/contracts": "^2.6",
48-
"dragon-code/support": "^6.0"
47+
"dragon-code/contracts": "^2.20",
48+
"dragon-code/support": "^6.11.2"
4949
},
5050
"require-dev": {
51-
"phpunit/phpunit": "^9.0 || ^10.0"
51+
"phpunit/phpunit": "^9.6 || ^10.2"
5252
},
5353
"suggest": {
5454
"symfony/thanks": "Give thanks (in the form of a GitHub) to your fellow PHP package maintainers"

src/Concerns/HasCases.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ trait HasCases
2828
* @param int $type
2929
*
3030
* @throws \DragonCode\PrettyArray\Exceptions\UnknownCaseTypeException
31+
*
32+
* @return \DragonCode\PrettyArray\Concerns\HasCases
3133
*/
32-
public function setCase(int $type = self::NO_CASE): void
34+
public function setCase(int $type = self::NO_CASE): static
3335
{
3436
if ($type < 0 || $type > 4) {
3537
throw new UnknownCaseTypeException($type);
3638
}
3739

3840
$this->case = $type;
41+
42+
return $this;
3943
}
4044

4145
protected function convertKeysCase(array $array): array

src/Services/File.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use DragonCode\Support\Concerns\Makeable;
2121
use DragonCode\Support\Facades\Filesystem\File as Storage;
22+
use DragonCode\Support\Facades\Helpers\Str;
2223
use DragonCode\Support\Facades\Tools\Stub;
2324
use DragonCode\Support\Tools\Stub as StubTool;
2425

@@ -39,12 +40,36 @@ public function load(string $filename): array
3940
return Storage::load($filename);
4041
}
4142

42-
public function store(string $path, string $stub = StubTool::PHP_ARRAY): void
43+
public function store(string $path, ?string $stub = null): void
4344
{
44-
$content = Stub::replace($stub, [
45+
Storage::store($path, $this->resolveContent($path, $stub));
46+
}
47+
48+
protected function resolveContent(string $path, ?string $stub): string
49+
{
50+
return $this->content(
51+
$this->stub($stub, $path)
52+
);
53+
}
54+
55+
protected function content(string $stub): string
56+
{
57+
return Stub::replace($stub, [
4558
'{{slot}}' => $this->content,
4659
]);
60+
}
61+
62+
protected function stub(?string $stub, string $path): string
63+
{
64+
if ($stub) {
65+
return $stub;
66+
}
4767

48-
Storage::store($path, $content);
68+
return $this->isJson($path) ? StubTool::JSON : StubTool::PHP_ARRAY;
69+
}
70+
71+
protected function isJson(string $path): bool
72+
{
73+
return Str::of($path)->lower()->endsWith('.json');
4974
}
5075
}

src/Services/Formatter.php

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,22 @@
1919

2020
use DragonCode\Contracts\Pretty\Arr\Caseable;
2121
use DragonCode\PrettyArray\Concerns\HasCases;
22-
use DragonCode\PrettyArray\Concerns\HasCastable;
22+
use DragonCode\PrettyArray\Services\Formatters\Json;
23+
use DragonCode\PrettyArray\Services\Formatters\Php;
2324
use DragonCode\Support\Concerns\Makeable;
24-
use DragonCode\Support\Facades\Helpers\Arr;
2525

2626
class Formatter implements Caseable
2727
{
28-
use HasCases;
29-
use HasCastable;
3028
use Makeable;
29+
use HasCases;
3130

3231
protected bool $key_as_string = false;
3332

3433
protected bool $equals_align = false;
3534

3635
protected bool $is_simple = false;
3736

38-
protected int $pad_length = 4;
39-
40-
protected string $line_break = PHP_EOL;
37+
protected bool $as_json = false;
4138

4239
public function setKeyAsString(): void
4340
{
@@ -54,81 +51,26 @@ public function setSimple(): void
5451
$this->is_simple = true;
5552
}
5653

57-
public function raw(array $array, int $pad = 1): string
58-
{
59-
if (empty($array)) {
60-
return '[]';
61-
}
62-
63-
$array = $this->convertKeysCase($array);
64-
65-
$keys_size = $this->sizeKeys($array);
66-
$pad_length = $this->pad_length * $pad;
67-
68-
$formatted = '[' . $this->line_break;
69-
70-
foreach ($array as $key => $value) {
71-
$key = $this->key($key, $keys_size);
72-
$value = $this->value($value, $pad + 1);
73-
74-
$row = $this->is_simple
75-
? "$value," . $this->line_break
76-
: "$key => $value," . $this->line_break;
77-
78-
$formatted .= $this->pad($row, $pad_length);
79-
}
80-
81-
return $formatted . $this->pad(']', $pad_length - $this->pad_length);
82-
}
83-
84-
protected function pad(string $value, int $pad = 1, $type = STR_PAD_LEFT): string
85-
{
86-
$pad += $type === STR_PAD_LEFT ? strlen($value) : 2;
87-
88-
return str_pad($value, $pad, ' ', $type);
89-
}
90-
91-
protected function value($value, int $pad = 1): mixed
54+
public function asJson(bool $json = true): void
9255
{
93-
if (! empty($value) && (is_array($value) || is_object($value))) {
94-
return $this->raw($value, $pad);
95-
}
96-
97-
return $this->castValue($value);
56+
$this->as_json = $json;
9857
}
9958

100-
protected function key(mixed $key, int $size = 0): string
59+
public function raw(array $array, int $pad = 1): string
10160
{
102-
$key = $this->isStringKey($key) ? "'{$key}'" : $key;
103-
104-
if (! $this->equals_align) {
105-
return $key;
61+
if ($this->as_json) {
62+
return Json::make()
63+
->setCase($this->case)
64+
->setKeyAsString($this->key_as_string)
65+
->setSimple($this->is_simple)
66+
->get($array, $pad);
10667
}
10768

108-
return $this->pad($key, $this->keySizeCollision($key, $size), STR_PAD_RIGHT);
109-
}
110-
111-
protected function sizeKeys(array $array): int
112-
{
113-
$sizes = Arr::of($array)->keys()->longestStringLength();
114-
115-
return $this->key_as_string ? $sizes + 2 : $sizes;
116-
}
117-
118-
protected function keySizeCollision($key, int $size): int
119-
{
120-
$collision = is_numeric($key) ? 0 : ($this->isAlignAndString() ? -2 : 0);
121-
122-
return $size + $collision;
123-
}
124-
125-
protected function isStringKey($key): bool
126-
{
127-
return $this->key_as_string || ! is_numeric($key);
128-
}
129-
130-
protected function isAlignAndString(): bool
131-
{
132-
return $this->equals_align && $this->key_as_string;
69+
return Php::make()
70+
->setCase($this->case)
71+
->setKeyAsString($this->key_as_string)
72+
->setSimple($this->is_simple)
73+
->setEqualsAlign($this->equals_align)
74+
->get($array, $pad);
13375
}
13476
}

src/Services/Formatters/Base.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the "dragon-code/pretty-array" project.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* @author Andrey Helldar <[email protected]>
10+
*
11+
* @copyright 2023 Andrey Helldar
12+
*
13+
* @license MIT
14+
*
15+
* @see https://github.com/TheDragonCode/pretty-array
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace DragonCode\PrettyArray\Services\Formatters;
21+
22+
use DragonCode\Contracts\Pretty\Arr\Caseable;
23+
use DragonCode\PrettyArray\Concerns\HasCases;
24+
use DragonCode\PrettyArray\Concerns\HasCastable;
25+
use DragonCode\Support\Concerns\Makeable;
26+
27+
abstract class Base implements Caseable
28+
{
29+
use HasCases;
30+
use HasCastable;
31+
use Makeable;
32+
33+
protected bool $key_as_string = false;
34+
35+
protected bool $is_simple = false;
36+
37+
abstract public function get(array $array, int $pad = 1): string;
38+
39+
abstract protected function key(mixed $key, int $size = 0): string;
40+
41+
public function setKeyAsString(bool $as): static
42+
{
43+
$this->key_as_string = $as;
44+
45+
return $this;
46+
}
47+
48+
public function setSimple(bool $simple): static
49+
{
50+
$this->is_simple = $simple;
51+
52+
return $this;
53+
}
54+
55+
protected function pad(string $value, int $pad = 1, $type = STR_PAD_LEFT): string
56+
{
57+
$pad += $type === STR_PAD_LEFT ? strlen($value) : 2;
58+
59+
return str_pad($value, $pad, ' ', $type);
60+
}
61+
62+
protected function isStringKey($key): bool
63+
{
64+
return $this->key_as_string || ! is_numeric($key);
65+
}
66+
}

src/Services/Formatters/Json.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the "dragon-code/pretty-array" project.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* @author Andrey Helldar <[email protected]>
10+
*
11+
* @copyright 2023 Andrey Helldar
12+
*
13+
* @license MIT
14+
*
15+
* @see https://github.com/TheDragonCode/pretty-array
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace DragonCode\PrettyArray\Services\Formatters;
21+
22+
class Json extends Base
23+
{
24+
protected int $flags = JSON_UNESCAPED_SLASHES ^ JSON_PRETTY_PRINT ^ JSON_UNESCAPED_UNICODE;
25+
26+
public function get(array $array, int $pad = 1): string
27+
{
28+
if (empty($array)) {
29+
return $this->encode($array);
30+
}
31+
32+
$array = $this->convertKeysCase($array);
33+
34+
return $this->encode($this->prepare($array));
35+
}
36+
37+
protected function key(mixed $key, int $size = 0): string
38+
{
39+
return (string) $key;
40+
}
41+
42+
protected function prepare(array $array): array
43+
{
44+
$result = [];
45+
46+
foreach ($array as $key => $value) {
47+
$key = $this->key($key);
48+
$value = $this->value($value);
49+
50+
match ($this->is_simple) {
51+
true => $result[] = $value,
52+
false => $result[$key] = $value
53+
};
54+
}
55+
56+
return $result;
57+
}
58+
59+
protected function value($value): mixed
60+
{
61+
if (! empty($value) && (is_array($value) || is_object($value))) {
62+
return $this->prepare($value);
63+
}
64+
65+
return $value;
66+
}
67+
68+
protected function encode(mixed $value): string
69+
{
70+
return json_encode($value, $this->flags);
71+
}
72+
}

0 commit comments

Comments
 (0)