@@ -84,6 +84,20 @@ function ($match) {
84
84
return $ escapedString ;
85
85
}
86
86
87
+ /**
88
+ * @param array<mixed, mixed> $array
89
+ *
90
+ * @see https://stackoverflow.com/a/173479
91
+ */
92
+ public static function isArrayAssociate (array $ array ): bool
93
+ {
94
+ if ([] === $ array ) {
95
+ return false ;
96
+ }
97
+
98
+ return array_keys ($ array ) !== range (0 , count ($ array ) - 1 );
99
+ }
100
+
87
101
public static function valueToString (mixed $ value ): string
88
102
{
89
103
if (is_string ($ value )) {
@@ -99,11 +113,30 @@ public static function valueToString(mixed $value): string
99
113
return 'null ' ;
100
114
}
101
115
if (is_array ($ value )) {
102
- asort ($ value );
103
116
$ parts = [];
104
- foreach ($ value as $ part ) {
105
- $ parts [] = self ::valueToString ($ part );
117
+ if (self ::isArrayAssociate ($ value )) {
118
+ asort ($ value );
119
+ foreach ($ value as $ name => $ part ) {
120
+ if (self ::mustNameBeEscaped ($ name )) {
121
+ $ parts [] = sprintf (
122
+ "`%s`: %s " ,
123
+ self ::escapeString ($ name ),
124
+ self ::valueToString ($ part )
125
+ );
126
+ } else {
127
+ $ parts [] = sprintf (
128
+ "%s: %s " ,
129
+ $ name ,
130
+ self ::valueToString ($ part )
131
+ );
132
+ }
133
+ }
134
+ } else {
135
+ foreach ($ value as $ part ) {
136
+ $ parts [] = self ::valueToString ($ part );
137
+ }
106
138
}
139
+
107
140
$ parts = implode (', ' , $ parts );
108
141
109
142
return sprintf ("[%s] " , $ parts );
@@ -120,7 +153,7 @@ public static function valueToString(mixed $value): string
120
153
/**
121
154
* @param array<string, mixed> $properties
122
155
*/
123
- public static function propertyArrayToString (array $ properties , bool $ escapeAllNames = false ): string
156
+ public static function propertiesToString (array $ properties , bool $ escapeAllNames = false ): string
124
157
{
125
158
ksort ($ properties );
126
159
$ parts = [];
@@ -177,7 +210,7 @@ public static function nodeToString(NodeInterface $node, bool $identifying = fal
177
210
if ($ identifying ) {
178
211
$ properties = $ node ->getIdentifiers ();
179
212
}
180
- $ propertyString = self ::propertyArrayToString ($ properties );
213
+ $ propertyString = self ::propertiesToString ($ properties );
181
214
if (strlen ($ propertyString ) > 0 ) {
182
215
$ parts [] = sprintf ("{%s} " , $ propertyString );
183
216
}
@@ -207,7 +240,7 @@ public static function relationToString(RelationInterface $relation, bool $ident
207
240
if ($ identifying ) {
208
241
$ properties = $ relation ->getIdentifiers ();
209
242
}
210
- $ propertyString = self ::propertyArrayToString ($ properties );
243
+ $ propertyString = self ::propertiesToString ($ properties );
211
244
if (strlen ($ propertyString ) > 0 ) {
212
245
$ relationParts [] = sprintf ("{%s} " , $ propertyString );
213
246
}
@@ -229,7 +262,7 @@ public static function relationToString(RelationInterface $relation, bool $ident
229
262
*/
230
263
public static function optionsToString (array $ options ): string
231
264
{
232
- return '' ;
265
+ return self :: propertiesToString ( $ options ) ;
233
266
}
234
267
235
268
public static function nodeConstraintToString (NodeConstraintInterface $ nodeConstraint ): string
@@ -280,7 +313,7 @@ public static function nodeConstraintToString(NodeConstraintInterface $nodeConst
280
313
$ options = $ nodeConstraint ->getOptions ();
281
314
if (count ($ options ) > 0 ) {
282
315
$ parts [] = 'OPTIONS ' ;
283
- $ parts [] = self ::optionsToString ($ options );
316
+ $ parts [] = sprintf ( " {%s} " , self ::optionsToString ($ options) );
284
317
}
285
318
286
319
return implode (' ' , $ parts );
@@ -334,7 +367,7 @@ public static function relationConstraintToString(RelationConstraintInterface $r
334
367
$ options = $ relationConstraint ->getOptions ();
335
368
if (count ($ options ) > 0 ) {
336
369
$ parts [] = 'OPTIONS ' ;
337
- $ parts [] = self ::optionsToString ($ options );
370
+ $ parts [] = sprintf ( " {%s} " , self ::optionsToString ($ options) );
338
371
}
339
372
340
373
return implode (' ' , $ parts );
@@ -380,7 +413,7 @@ public static function nodeIndexToString(NodeIndexInterface $nodeIndex): string
380
413
$ options = $ nodeIndex ->getOptions ();
381
414
if (count ($ options ) > 0 ) {
382
415
$ parts [] = 'OPTIONS ' ;
383
- $ parts [] = self ::optionsToString ($ options );
416
+ $ parts [] = sprintf ( " {%s} " , self ::optionsToString ($ options) );
384
417
}
385
418
386
419
return implode (' ' , $ parts );
@@ -426,7 +459,7 @@ public static function relationIndexToString(RelationIndexInterface $relationInd
426
459
$ options = $ relationIndex ->getOptions ();
427
460
if (count ($ options ) > 0 ) {
428
461
$ parts [] = 'OPTIONS ' ;
429
- $ parts [] = self ::optionsToString ($ options );
462
+ $ parts [] = sprintf ( " {%s} " , self ::optionsToString ($ options) );
430
463
}
431
464
432
465
return implode (' ' , $ parts );
0 commit comments