2222
2323final class ArrayStringifier implements Stringifier
2424{
25+ private const LIMIT_EXCEEDED_PLACEHOLDER = '... ' ;
26+
2527 public function __construct (
2628 private readonly Stringifier $ stringifier ,
2729 private readonly Quoter $ quoter ,
2830 private readonly int $ maximumDepth ,
29- private readonly int $ itemsLimit
31+ private readonly int $ maximumNumberOfItems
3032 ) {
3133 }
3234
@@ -37,30 +39,40 @@ public function stringify(mixed $raw, int $depth): ?string
3739 }
3840
3941 if (empty ($ raw )) {
40- return $ this ->quoter ->quote ('{ } ' , $ depth );
42+ return $ this ->quoter ->quote ('[] ' , $ depth );
4143 }
4244
4345 if ($ depth >= $ this ->maximumDepth ) {
44- return ' ... ' ;
46+ return $ this -> quoter -> quote ( self :: LIMIT_EXCEEDED_PLACEHOLDER , $ depth ) ;
4547 }
4648
4749 $ items = [];
48- $ itemsCount = 0 ;
4950 $ isSequential = $ this ->isSequential ($ raw );
5051 foreach ($ raw as $ key => $ value ) {
51- if (++ $ itemsCount > $ this ->itemsLimit ) {
52- $ items [$ itemsCount ] = ' ... ' ;
52+ if (count ( $ items ) >= $ this ->maximumNumberOfItems ) {
53+ $ items [] = self :: LIMIT_EXCEEDED_PLACEHOLDER ;
5354 break ;
5455 }
5556
56- $ items [$ itemsCount ] = '' ;
57- if ($ isSequential === false ) {
58- $ items [$ itemsCount ] .= sprintf ('%s: ' , $ this ->stringifier ->stringify ($ key , $ depth + 1 ));
57+ $ stringifiedValue = $ this ->stringifyKeyValue ($ value , $ depth + 1 );
58+ if ($ isSequential === true ) {
59+ $ items [] = $ stringifiedValue ;
60+ continue ;
5961 }
60- $ items [$ itemsCount ] .= $ this ->stringifier ->stringify ($ value , $ depth + 1 );
62+
63+ $ items [] = sprintf ('%s: %s ' , $ this ->stringifier ->stringify ($ key , $ depth + 1 ), $ stringifiedValue );
64+ }
65+
66+ return $ this ->quoter ->quote (sprintf ('[%s] ' , implode (', ' , $ items )), $ depth );
67+ }
68+
69+ private function stringifyKeyValue (mixed $ value , int $ depth ): ?string
70+ {
71+ if (is_array ($ value )) {
72+ return $ this ->stringify ($ value , $ depth );
6173 }
6274
63- return $ this ->quoter -> quote ( sprintf ( ' { %s } ' , implode ( ' , ' , $ items )) , $ depth );
75+ return $ this ->stringifier -> stringify ( $ value , $ depth );
6476 }
6577
6678 /**
0 commit comments