Skip to content

Commit f34d7e2

Browse files
committed
used arrays for CypherList
1 parent d361610 commit f34d7e2

File tree

1 file changed

+92
-42
lines changed

1 file changed

+92
-42
lines changed

src/Types/CypherList.php

Lines changed: 92 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@
1313

1414
namespace Laudis\Neo4j\Types;
1515

16+
use function array_filter;
17+
use function array_key_last;
18+
use function array_map;
19+
use function array_reduce;
20+
use function array_search;
21+
use function array_slice;
22+
use ArrayIterator;
1623
use BadMethodCallException;
17-
use Ds\Vector;
24+
use function array_sum;
25+
use function count;
26+
use function in_array;
1827
use Laudis\Neo4j\Contracts\CypherContainerInterface;
19-
use Traversable;
28+
use function sort;
29+
use function usort;
2030

2131
/**
2232
* @template T
@@ -25,52 +35,60 @@
2535
*/
2636
final class CypherList implements CypherContainerInterface
2737
{
28-
/** @var Vector<T> */
29-
private Vector $vector;
38+
/** @var list<T> */
39+
private array $array;
3040

3141
/**
32-
* @param Vector<T> $vector
42+
* @param iterable<T> $array
3343
*/
34-
public function __construct(Vector $vector)
44+
public function __construct(iterable $array)
3545
{
36-
$this->vector = new Vector($vector);
46+
if ($array instanceof self) {
47+
$this->array = $array->array;
48+
} else {
49+
$this->array = [];
50+
foreach ($array as $value) {
51+
$this->array[] = $value;
52+
}
53+
}
3754
}
3855

3956
public function count(): int
4057
{
41-
return $this->vector->count();
58+
return count($this->array);
4259
}
4360

4461
/**
4562
* @return CypherList<T>
4663
*/
4764
public function copy(): CypherList
4865
{
49-
return new CypherList($this->vector->copy());
66+
$tbr = $this->array;
67+
68+
return new CypherList($tbr);
5069
}
5170

5271
public function isEmpty(): bool
5372
{
54-
return $this->vector->isEmpty();
73+
return count($this->array) === 0;
5574
}
5675

5776
/**
5877
* @return list<T>
5978
*/
6079
public function toArray(): array
6180
{
62-
return $this->vector->toArray();
81+
return $this->array;
6382
}
6483

6584
public function getIterator()
6685
{
67-
/** @var Traversable<int, T> */
68-
return $this->vector->getIterator();
86+
return new ArrayIterator($this->array);
6987
}
7088

7189
public function offsetExists($offset): bool
7290
{
73-
return $this->vector->offsetExists($offset);
91+
return isset($this->array[$offset]);
7492
}
7593

7694
/**
@@ -82,8 +100,7 @@ public function offsetExists($offset): bool
82100
*/
83101
public function offsetGet($offset)
84102
{
85-
/** @psalm-suppress InvalidReturnStatement */
86-
return $this->vector->offsetGet($offset);
103+
return $this->array[$offset];
87104
}
88105

89106
/**
@@ -105,20 +122,36 @@ public function offsetUnset($offset)
105122

106123
/**
107124
* @param T ...$values
125+
*
126+
* @deprecated Use hasValue instead
108127
*/
109128
public function contains(...$values): bool
110129
{
111-
return $this->vector->contains(...$values);
130+
foreach ($values as $value) {
131+
if (!in_array($value, $this->array, true)) {
132+
return false;
133+
}
134+
}
135+
136+
return true;
137+
}
138+
139+
/**
140+
* @param T $value
141+
*/
142+
public function hasValue($value): bool
143+
{
144+
return in_array($value, $this->array, true);
112145
}
113146

114147
/**
115-
* @param (callable(T):bool)|null $callback
148+
* @param (callable(T):bool) $callback
116149
*
117150
* @return CypherList<T>
118151
*/
119-
public function filter(callable $callback = null): CypherList
152+
public function filter(callable $callback): CypherList
120153
{
121-
return new CypherList($this->vector->filter($callback));
154+
return new CypherList(array_filter($this->array, $callback));
122155
}
123156

124157
/**
@@ -128,40 +161,42 @@ public function filter(callable $callback = null): CypherList
128161
*/
129162
public function find($value)
130163
{
131-
return $this->vector->find($value);
164+
return array_search($value, $this->array, true);
132165
}
133166

134167
/**
135-
* @return T
168+
* @return T|null
136169
*/
137170
public function first()
138171
{
139-
return $this->vector->first();
172+
return $this->array[0] ?? null;
140173
}
141174

142175
/**
143176
* @return T
144177
*/
145178
public function get(int $index)
146179
{
147-
return $this->vector->get($index);
180+
return $this->array[$index];
148181
}
149182

150183
public function join(?string $glue = null): string
151184
{
152-
if ($glue === null) {
153-
return $this->vector->join();
154-
}
155-
156-
return $this->vector->join($glue);
185+
/** @psalm-suppress MixedArgumentTypeCoercion */
186+
return implode($glue ?? '', $this->array);
157187
}
158188

159189
/**
160190
* @return T
161191
*/
162192
public function last()
163193
{
164-
return $this->vector->last();
194+
$key = array_key_last($this->array);
195+
if ($key === null) {
196+
return null;
197+
}
198+
199+
return $this->array[$key];
165200
}
166201

167202
/**
@@ -173,7 +208,7 @@ public function last()
173208
*/
174209
public function map(callable $callback): CypherList
175210
{
176-
return new CypherList($this->vector->map($callback));
211+
return new CypherList(array_map($callback, $this->array));
177212
}
178213

179214
/**
@@ -183,31 +218,39 @@ public function map(callable $callback): CypherList
183218
*/
184219
public function merge($values): CypherList
185220
{
186-
return new CypherList($this->vector->merge($values));
221+
$tbr = $this->array;
222+
foreach ($values as $value) {
223+
$tbr[] = $value;
224+
}
225+
226+
return new CypherList($tbr);
187227
}
188228

189229
/**
190-
* @param callable(T, T|null):T $callback
191-
* @param T|null $initial
230+
* @template U
192231
*
193-
* @return T|null
232+
* @param callable(U|null, T):U $callback
233+
* @param U|null $initial
234+
*
235+
* @return U|null
194236
*/
195237
public function reduce(callable $callback, $initial = null)
196238
{
197-
return $this->vector->reduce($callback, $initial);
239+
/** @var U|null */
240+
return array_reduce($this->array, $callback, $initial);
198241
}
199242

200243
/**
201244
* @return CypherList<T>
202245
*/
203246
public function reversed(): CypherList
204247
{
205-
return new CypherList($this->vector->reversed());
248+
return new CypherList(array_reverse($this->array));
206249
}
207250

208251
public function slice(int $index, int $length = null): CypherList
209252
{
210-
return new CypherList($this->vector->slice($index, $length));
253+
return new CypherList(array_slice($this->array, $index, $length));
211254
}
212255

213256
/**
@@ -217,15 +260,22 @@ public function slice(int $index, int $length = null): CypherList
217260
*/
218261
public function sorted(callable $comparator = null): CypherList
219262
{
220-
return new CypherList($this->vector->sorted($comparator));
263+
$tbr = $this->array;
264+
if ($comparator === null) {
265+
sort($tbr);
266+
} else {
267+
usort($tbr, $comparator);
268+
}
269+
270+
return new CypherList($tbr);
221271
}
222272

223273
/**
224274
* @return float|int
225275
*/
226276
public function sum()
227277
{
228-
return $this->vector->sum();
278+
return array_sum($this->array);
229279
}
230280

231281
/**
@@ -234,14 +284,14 @@ public function sum()
234284
public function jsonSerialize(): array
235285
{
236286
/** @var array<int, T> */
237-
return $this->vector->jsonSerialize();
287+
return $this->array;
238288
}
239289

240290
/**
241291
* @return list<T>
242292
*/
243293
public function __debugInfo()
244294
{
245-
return $this->vector->toArray();
295+
return $this->array;
246296
}
247297
}

0 commit comments

Comments
 (0)