19
19
use function array_keys ;
20
20
use function array_reverse ;
21
21
use function array_slice ;
22
- use BadMethodCallException ;
23
22
use function func_num_args ;
24
23
use InvalidArgumentException ;
25
24
use function is_int ;
29
28
use Laudis \Neo4j \Databags \Pair ;
30
29
use function method_exists ;
31
30
use OutOfBoundsException ;
32
- use function sort ;
31
+ use stdClass ;
32
+ use function uasort ;
33
33
use function uksort ;
34
- use function usort ;
35
34
36
35
/**
37
36
* An immutable ordered map of items.
@@ -84,7 +83,7 @@ public function first(): Pair
84
83
{
85
84
$ key = array_key_first ($ this ->sequence );
86
85
if (!is_string ($ key )) {
87
- throw new BadMethodCallException ('Cannot grab first element from an empty map ' );
86
+ throw new OutOfBoundsException ('Cannot grab first element of an empty map ' );
88
87
}
89
88
90
89
return new Pair ($ key , $ this ->sequence [$ key ]);
@@ -99,7 +98,7 @@ public function last(): Pair
99
98
{
100
99
$ key = array_key_last ($ this ->sequence );
101
100
if (!is_string ($ key )) {
102
- throw new BadMethodCallException ('Cannot grab last element from an empty map ' );
101
+ throw new OutOfBoundsException ('Cannot grab last element of an empty map ' );
103
102
}
104
103
105
104
return new Pair ($ key , $ this ->sequence [$ key ]);
@@ -120,7 +119,7 @@ public function skip(int $position): Pair
120
119
return new Pair ($ key , $ this ->sequence [$ key ]);
121
120
}
122
121
123
- throw new OutOfBoundsException ();
122
+ throw new OutOfBoundsException (sprintf ( ' Cannot skip to a pair at position: %s ' , $ position ) );
124
123
}
125
124
126
125
/**
@@ -193,8 +192,10 @@ public function xor(iterable $map): CypherMap
193
192
* @var mixed $key
194
193
*/
195
194
foreach ($ map as $ key => $ value ) {
196
- if (( is_int ( $ key ) || is_string ( $ key )) && array_key_exists ($ key , $ this ->sequence )) {
195
+ if (array_key_exists ($ key , $ this ->sequence )) {
197
196
unset($ tbr [$ key ]);
197
+ } else {
198
+ $ tbr [$ key ] = $ value ;
198
199
}
199
200
}
200
201
@@ -270,9 +271,7 @@ public function diff($map): CypherMap
270
271
271
272
/** @psalm-suppress UnusedForeachValue */
272
273
foreach ($ map as $ key => $ value ) {
273
- if (array_key_exists ($ key , $ tbr )) {
274
- unset($ tbr [$ key ]);
275
- }
274
+ unset($ tbr [$ key ]);
276
275
}
277
276
278
277
return new self ($ tbr );
@@ -303,9 +302,9 @@ public function sorted(?callable $comparator = null): self
303
302
{
304
303
$ tbr = $ this ->sequence ;
305
304
if ($ comparator === null ) {
306
- sort ($ tbr );
305
+ asort ($ tbr );
307
306
} else {
308
- usort ($ tbr , $ comparator );
307
+ uasort ($ tbr , $ comparator );
309
308
}
310
309
311
310
return new self ($ tbr );
@@ -326,12 +325,21 @@ public function get(string $key, $default = null)
326
325
{
327
326
if (func_num_args () === 1 ) {
328
327
if (!array_key_exists ($ key , $ this ->sequence )) {
329
- throw new OutOfBoundsException ();
328
+ throw new OutOfBoundsException (sprintf ( ' Cannot get item in sequence with key: %s ' , $ key ) );
330
329
}
331
330
332
331
return $ this ->sequence [$ key ];
333
332
}
334
333
335
334
return $ this ->sequence [$ key ] ?? $ default ;
336
335
}
336
+
337
+ public function jsonSerialize ()
338
+ {
339
+ if ($ this ->isEmpty ()) {
340
+ return new stdClass ();
341
+ }
342
+
343
+ return parent ::jsonSerialize ();
344
+ }
337
345
}
0 commit comments