13
13
14
14
namespace Laudis \Neo4j \Types ;
15
15
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 ;
16
23
use BadMethodCallException ;
17
- use Ds \Vector ;
24
+ use function array_sum ;
25
+ use function count ;
26
+ use function in_array ;
18
27
use Laudis \Neo4j \Contracts \CypherContainerInterface ;
19
- use Traversable ;
28
+ use function sort ;
29
+ use function usort ;
20
30
21
31
/**
22
32
* @template T
25
35
*/
26
36
final class CypherList implements CypherContainerInterface
27
37
{
28
- /** @var Vector <T> */
29
- private Vector $ vector ;
38
+ /** @var list <T> */
39
+ private array $ array ;
30
40
31
41
/**
32
- * @param Vector <T> $vector
42
+ * @param iterable <T> $array
33
43
*/
34
- public function __construct (Vector $ vector )
44
+ public function __construct (iterable $ array )
35
45
{
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
+ }
37
54
}
38
55
39
56
public function count (): int
40
57
{
41
- return $ this ->vector -> count ( );
58
+ return count ( $ this ->array );
42
59
}
43
60
44
61
/**
45
62
* @return CypherList<T>
46
63
*/
47
64
public function copy (): CypherList
48
65
{
49
- return new CypherList ($ this ->vector ->copy ());
66
+ $ tbr = $ this ->array ;
67
+
68
+ return new CypherList ($ tbr );
50
69
}
51
70
52
71
public function isEmpty (): bool
53
72
{
54
- return $ this ->vector -> isEmpty () ;
73
+ return count ( $ this ->array ) === 0 ;
55
74
}
56
75
57
76
/**
58
77
* @return list<T>
59
78
*/
60
79
public function toArray (): array
61
80
{
62
- return $ this ->vector -> toArray () ;
81
+ return $ this ->array ;
63
82
}
64
83
65
84
public function getIterator ()
66
85
{
67
- /** @var Traversable<int, T> */
68
- return $ this ->vector ->getIterator ();
86
+ return new ArrayIterator ($ this ->array );
69
87
}
70
88
71
89
public function offsetExists ($ offset ): bool
72
90
{
73
- return $ this ->vector -> offsetExists ( $ offset );
91
+ return isset ( $ this ->array [ $ offset] );
74
92
}
75
93
76
94
/**
@@ -82,8 +100,7 @@ public function offsetExists($offset): bool
82
100
*/
83
101
public function offsetGet ($ offset )
84
102
{
85
- /** @psalm-suppress InvalidReturnStatement */
86
- return $ this ->vector ->offsetGet ($ offset );
103
+ return $ this ->array [$ offset ];
87
104
}
88
105
89
106
/**
@@ -105,20 +122,36 @@ public function offsetUnset($offset)
105
122
106
123
/**
107
124
* @param T ...$values
125
+ *
126
+ * @deprecated Use hasValue instead
108
127
*/
109
128
public function contains (...$ values ): bool
110
129
{
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 );
112
145
}
113
146
114
147
/**
115
- * @param (callable(T):bool)|null $callback
148
+ * @param (callable(T):bool) $callback
116
149
*
117
150
* @return CypherList<T>
118
151
*/
119
- public function filter (callable $ callback = null ): CypherList
152
+ public function filter (callable $ callback ): CypherList
120
153
{
121
- return new CypherList ($ this ->vector -> filter ( $ callback ));
154
+ return new CypherList (array_filter ( $ this ->array , $ callback ));
122
155
}
123
156
124
157
/**
@@ -128,40 +161,42 @@ public function filter(callable $callback = null): CypherList
128
161
*/
129
162
public function find ($ value )
130
163
{
131
- return $ this ->vector -> find ( $ value );
164
+ return array_search ( $ value , $ this ->array , true );
132
165
}
133
166
134
167
/**
135
- * @return T
168
+ * @return T|null
136
169
*/
137
170
public function first ()
138
171
{
139
- return $ this ->vector -> first () ;
172
+ return $ this ->array [ 0 ] ?? null ;
140
173
}
141
174
142
175
/**
143
176
* @return T
144
177
*/
145
178
public function get (int $ index )
146
179
{
147
- return $ this ->vector -> get ( $ index) ;
180
+ return $ this ->array [ $ index] ;
148
181
}
149
182
150
183
public function join (?string $ glue = null ): string
151
184
{
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 );
157
187
}
158
188
159
189
/**
160
190
* @return T
161
191
*/
162
192
public function last ()
163
193
{
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 ];
165
200
}
166
201
167
202
/**
@@ -173,7 +208,7 @@ public function last()
173
208
*/
174
209
public function map (callable $ callback ): CypherList
175
210
{
176
- return new CypherList ($ this ->vector -> map ( $ callback ));
211
+ return new CypherList (array_map ( $ callback , $ this ->array ));
177
212
}
178
213
179
214
/**
@@ -183,31 +218,39 @@ public function map(callable $callback): CypherList
183
218
*/
184
219
public function merge ($ values ): CypherList
185
220
{
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 );
187
227
}
188
228
189
229
/**
190
- * @param callable(T, T|null):T $callback
191
- * @param T|null $initial
230
+ * @template U
192
231
*
193
- * @return T|null
232
+ * @param callable(U|null, T):U $callback
233
+ * @param U|null $initial
234
+ *
235
+ * @return U|null
194
236
*/
195
237
public function reduce (callable $ callback , $ initial = null )
196
238
{
197
- return $ this ->vector ->reduce ($ callback , $ initial );
239
+ /** @var U|null */
240
+ return array_reduce ($ this ->array , $ callback , $ initial );
198
241
}
199
242
200
243
/**
201
244
* @return CypherList<T>
202
245
*/
203
246
public function reversed (): CypherList
204
247
{
205
- return new CypherList ($ this ->vector -> reversed ( ));
248
+ return new CypherList (array_reverse ( $ this ->array ));
206
249
}
207
250
208
251
public function slice (int $ index , int $ length = null ): CypherList
209
252
{
210
- return new CypherList ($ this ->vector -> slice ( $ index , $ length ));
253
+ return new CypherList (array_slice ( $ this ->array , $ index , $ length ));
211
254
}
212
255
213
256
/**
@@ -217,15 +260,22 @@ public function slice(int $index, int $length = null): CypherList
217
260
*/
218
261
public function sorted (callable $ comparator = null ): CypherList
219
262
{
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 );
221
271
}
222
272
223
273
/**
224
274
* @return float|int
225
275
*/
226
276
public function sum ()
227
277
{
228
- return $ this ->vector -> sum ( );
278
+ return array_sum ( $ this ->array );
229
279
}
230
280
231
281
/**
@@ -234,14 +284,14 @@ public function sum()
234
284
public function jsonSerialize (): array
235
285
{
236
286
/** @var array<int, T> */
237
- return $ this ->vector -> jsonSerialize () ;
287
+ return $ this ->array ;
238
288
}
239
289
240
290
/**
241
291
* @return list<T>
242
292
*/
243
293
public function __debugInfo ()
244
294
{
245
- return $ this ->vector -> toArray () ;
295
+ return $ this ->array ;
246
296
}
247
297
}
0 commit comments