@@ -54,7 +54,7 @@ public static function getRouteClassAndMethodNames($routeOrAction)
5454 public static function replaceUrlParameterPlaceholdersWithValues (string $ uri , array $ urlParameters )
5555 {
5656 $ matches = preg_match_all ('/{.+?}/i ' , $ uri , $ parameterPaths );
57- if (! $ matches ) {
57+ if (!$ matches ) {
5858 return $ uri ;
5959 }
6060
@@ -89,7 +89,8 @@ public static function dumpException(\Exception $e)
8989
9090 public static function deleteDirectoryAndContents ($ dir )
9191 {
92- $ adapter = new Local (realpath (__DIR__ .'/../../ ' ));
92+ $ dir = ltrim ($ dir , '/ ' );
93+ $ adapter = new Local (realpath (__DIR__ . '/../../ ' ));
9394 $ fs = new Filesystem ($ adapter );
9495 $ fs ->deleteDir ($ dir );
9596 }
@@ -98,21 +99,76 @@ public static function deleteDirectoryAndContents($dir)
9899 * @param mixed $value
99100 * @param int $indentationLevel
100101 *
102+ * @return string
101103 * @throws \Symfony\Component\VarExporter\Exception\ExceptionInterface
102104 *
103- * @return string
104105 */
105- public static function printPhpValue ($ value , $ indentationLevel = 0 )
106+ public static function printPhpValue ($ value , int $ indentationLevel = 0 ): string
106107 {
107108 $ output = VarExporter::export ($ value );
108109 // Padding with x spaces so they align
109110 $ split = explode ("\n" , $ output );
110111 $ result = '' ;
111112 $ padWith = str_repeat (' ' , $ indentationLevel );
112113 foreach ($ split as $ index => $ line ) {
113- $ result .= ($ index == 0 ? '' : "\n$ padWith " ). $ line ;
114+ $ result .= ($ index == 0 ? '' : "\n$ padWith " ) . $ line ;
114115 }
115116
116117 return $ result ;
117118 }
119+
120+ public static function printQueryParamsAsString (array $ cleanQueryParams ): string
121+ {
122+ $ qs = '' ;
123+ foreach ($ cleanQueryParams as $ parameter => $ value ) {
124+ $ paramName = urlencode ($ parameter );
125+
126+ if (!is_array ($ value )) {
127+ $ qs .= "$ paramName= " . urlencode ($ value ) . "& " ;
128+ } else {
129+ if (array_keys ($ value )[0 ] === 0 ) {
130+ // List query param (eg filter[]=haha should become "filter[]": "haha")
131+ $ qs .= "$ paramName " . '[]= ' . urlencode ($ value [0 ]) . '& ' ;
132+ } else {
133+ // Hash query param (eg filter[name]=john should become "filter[name]": "john")
134+ foreach ($ value as $ item => $ itemValue ) {
135+ $ qs .= "$ paramName " . '[ ' . urlencode ($ item ) . ']= ' . urlencode ($ itemValue ) . '& ' ;
136+ }
137+ }
138+ }
139+ }
140+
141+ return rtrim ($ qs , '& ' );
142+ }
143+
144+ public static function printQueryParamsAsKeyValue (
145+ array $ cleanQueryParams ,
146+ string $ quote = "\"" ,
147+ string $ delimiter = ": " ,
148+ int $ spacesIndentation = 4 ,
149+ string $ braces = "{} " ,
150+ int $ closingBraceIndentation = 0
151+ ): string {
152+ $ output = "{$ braces [0 ]}\n" ;
153+ foreach ($ cleanQueryParams as $ parameter => $ value ) {
154+ if (!is_array ($ value )) {
155+ $ output .= str_repeat (" " , $ spacesIndentation );
156+ $ output .= "$ quote$ parameter$ quote$ delimiter $ quote$ value$ quote, \n" ;
157+ } else {
158+ if (array_keys ($ value )[0 ] === 0 ) {
159+ // List query param (eg filter[]=haha should become "filter[]": "haha")
160+ $ output .= str_repeat (" " , $ spacesIndentation );
161+ $ output .= "$ quote$ parameter " . "[] $ quote$ delimiter $ quote$ value [0 ]$ quote, \n" ;
162+ } else {
163+ // Hash query param (eg filter[name]=john should become "filter[name]": "john")
164+ foreach ($ value as $ item => $ itemValue ) {
165+ $ output .= str_repeat (" " , $ spacesIndentation );
166+ $ output .= "$ quote$ parameter " . "[ $ item] $ quote$ delimiter $ quote$ itemValue$ quote, \n" ;
167+ }
168+ }
169+ }
170+ }
171+
172+ return $ output . str_repeat (" " , $ closingBraceIndentation ) . "{$ braces [1 ]}" ;
173+ }
118174}
0 commit comments