1919
2020use DragonCode \Contracts \Pretty \Arr \Caseable ;
2121use DragonCode \PrettyArray \Concerns \HasCases ;
22- use DragonCode \PrettyArray \Concerns \HasCastable ;
22+ use DragonCode \PrettyArray \Services \Formatters \Json ;
23+ use DragonCode \PrettyArray \Services \Formatters \Php ;
2324use DragonCode \Support \Concerns \Makeable ;
24- use DragonCode \Support \Facades \Helpers \Arr ;
2525
2626class 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}
0 commit comments