Skip to content

Commit 1e591c4

Browse files
committed
used correc sort function, json serialisation and exception messages
1 parent cf73eda commit 1e591c4

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/Types/CypherMap.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use function array_keys;
2020
use function array_reverse;
2121
use function array_slice;
22-
use BadMethodCallException;
2322
use function func_num_args;
2423
use InvalidArgumentException;
2524
use function is_int;
@@ -29,9 +28,9 @@
2928
use Laudis\Neo4j\Databags\Pair;
3029
use function method_exists;
3130
use OutOfBoundsException;
32-
use function sort;
31+
use stdClass;
32+
use function uasort;
3333
use function uksort;
34-
use function usort;
3534

3635
/**
3736
* An immutable ordered map of items.
@@ -84,7 +83,7 @@ public function first(): Pair
8483
{
8584
$key = array_key_first($this->sequence);
8685
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');
8887
}
8988

9089
return new Pair($key, $this->sequence[$key]);
@@ -99,7 +98,7 @@ public function last(): Pair
9998
{
10099
$key = array_key_last($this->sequence);
101100
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');
103102
}
104103

105104
return new Pair($key, $this->sequence[$key]);
@@ -120,7 +119,7 @@ public function skip(int $position): Pair
120119
return new Pair($key, $this->sequence[$key]);
121120
}
122121

123-
throw new OutOfBoundsException();
122+
throw new OutOfBoundsException(sprintf('Cannot skip to a pair at position: %s', $position));
124123
}
125124

126125
/**
@@ -193,8 +192,10 @@ public function xor(iterable $map): CypherMap
193192
* @var mixed $key
194193
*/
195194
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)) {
197196
unset($tbr[$key]);
197+
} else {
198+
$tbr[$key] = $value;
198199
}
199200
}
200201

@@ -270,9 +271,7 @@ public function diff($map): CypherMap
270271

271272
/** @psalm-suppress UnusedForeachValue */
272273
foreach ($map as $key => $value) {
273-
if (array_key_exists($key, $tbr)) {
274-
unset($tbr[$key]);
275-
}
274+
unset($tbr[$key]);
276275
}
277276

278277
return new self($tbr);
@@ -303,9 +302,9 @@ public function sorted(?callable $comparator = null): self
303302
{
304303
$tbr = $this->sequence;
305304
if ($comparator === null) {
306-
sort($tbr);
305+
asort($tbr);
307306
} else {
308-
usort($tbr, $comparator);
307+
uasort($tbr, $comparator);
309308
}
310309

311310
return new self($tbr);
@@ -326,12 +325,21 @@ public function get(string $key, $default = null)
326325
{
327326
if (func_num_args() === 1) {
328327
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));
330329
}
331330

332331
return $this->sequence[$key];
333332
}
334333

335334
return $this->sequence[$key] ?? $default;
336335
}
336+
337+
public function jsonSerialize()
338+
{
339+
if ($this->isEmpty()) {
340+
return new stdClass();
341+
}
342+
343+
return parent::jsonSerialize();
344+
}
337345
}

0 commit comments

Comments
 (0)