-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from TheDragonCode/4.x
Added JSON formatting
- Loading branch information
Showing
16 changed files
with
401 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "dragon-code/pretty-array" project. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Andrey Helldar <[email protected]> | ||
* | ||
* @copyright 2023 Andrey Helldar | ||
* | ||
* @license MIT | ||
* | ||
* @see https://github.com/TheDragonCode/pretty-array | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DragonCode\PrettyArray\Services\Formatters; | ||
|
||
use DragonCode\Contracts\Pretty\Arr\Caseable; | ||
use DragonCode\PrettyArray\Concerns\HasCases; | ||
use DragonCode\PrettyArray\Concerns\HasCastable; | ||
use DragonCode\Support\Concerns\Makeable; | ||
|
||
abstract class Base implements Caseable | ||
{ | ||
use HasCases; | ||
use HasCastable; | ||
use Makeable; | ||
|
||
protected bool $key_as_string = false; | ||
|
||
protected bool $is_simple = false; | ||
|
||
abstract public function get(array $array, int $pad = 1): string; | ||
|
||
abstract protected function key(mixed $key, int $size = 0): string; | ||
|
||
public function setKeyAsString(bool $as): static | ||
{ | ||
$this->key_as_string = $as; | ||
|
||
return $this; | ||
} | ||
|
||
public function setSimple(bool $simple): static | ||
{ | ||
$this->is_simple = $simple; | ||
|
||
return $this; | ||
} | ||
|
||
protected function pad(string $value, int $pad = 1, $type = STR_PAD_LEFT): string | ||
{ | ||
$pad += $type === STR_PAD_LEFT ? strlen($value) : 2; | ||
|
||
return str_pad($value, $pad, ' ', $type); | ||
} | ||
|
||
protected function isStringKey($key): bool | ||
{ | ||
return $this->key_as_string || ! is_numeric($key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "dragon-code/pretty-array" project. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Andrey Helldar <[email protected]> | ||
* | ||
* @copyright 2023 Andrey Helldar | ||
* | ||
* @license MIT | ||
* | ||
* @see https://github.com/TheDragonCode/pretty-array | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DragonCode\PrettyArray\Services\Formatters; | ||
|
||
class Json extends Base | ||
{ | ||
protected int $flags = JSON_UNESCAPED_SLASHES ^ JSON_PRETTY_PRINT ^ JSON_UNESCAPED_UNICODE; | ||
|
||
public function get(array $array, int $pad = 1): string | ||
{ | ||
if (empty($array)) { | ||
return $this->encode($array); | ||
} | ||
|
||
$array = $this->convertKeysCase($array); | ||
|
||
return $this->encode($this->prepare($array)); | ||
} | ||
|
||
protected function key(mixed $key, int $size = 0): string | ||
{ | ||
return (string) $key; | ||
} | ||
|
||
protected function prepare(array $array): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($array as $key => $value) { | ||
$key = $this->key($key); | ||
$value = $this->value($value); | ||
|
||
match ($this->is_simple) { | ||
true => $result[] = $value, | ||
false => $result[$key] = $value | ||
}; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
protected function value($value): mixed | ||
{ | ||
if (! empty($value) && (is_array($value) || is_object($value))) { | ||
return $this->prepare($value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
protected function encode(mixed $value): string | ||
{ | ||
return json_encode($value, $this->flags); | ||
} | ||
} |
Oops, something went wrong.