Skip to content

Commit 47af28b

Browse files
committed
added type casting methods to cypher list
1 parent 426c64c commit 47af28b

File tree

2 files changed

+180
-4
lines changed

2 files changed

+180
-4
lines changed

src/TypeCaster.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ final class TypeCaster
2626
{
2727
/**
2828
* @param mixed $value
29+
*
30+
* @pure
2931
*/
3032
public static function toString($value): ?string
3133
{
@@ -38,6 +40,8 @@ public static function toString($value): ?string
3840

3941
/**
4042
* @param mixed $value
43+
*
44+
* @pure
4145
*/
4246
public static function toFloat($value): ?float
4347
{
@@ -51,6 +55,8 @@ public static function toFloat($value): ?float
5155

5256
/**
5357
* @param mixed $value
58+
*
59+
* @pure
5460
*/
5561
public static function toInt($value): ?int
5662
{
@@ -64,6 +70,8 @@ public static function toInt($value): ?int
6470

6571
/**
6672
* @return null
73+
*
74+
* @pure
6775
*/
6876
public static function toNull()
6977
{
@@ -72,6 +80,8 @@ public static function toNull()
7280

7381
/**
7482
* @param mixed $value
83+
*
84+
* @pure
7585
*/
7686
public static function toBool($value): ?bool
7787
{
@@ -90,6 +100,8 @@ public static function toBool($value): ?bool
90100
* @param class-string<T> $class
91101
*
92102
* @return T|null
103+
*
104+
* @pure
93105
*/
94106
public static function toClass($value, string $class): ?object
95107
{
@@ -102,6 +114,8 @@ public static function toClass($value, string $class): ?object
102114

103115
/**
104116
* @param mixed $value
117+
*
118+
* @pure
105119
*/
106120
public static function toArray($value): ?array
107121
{
@@ -119,6 +133,8 @@ public static function toArray($value): ?array
119133

120134
/**
121135
* @param mixed $value
136+
*
137+
* @pure
122138
*/
123139
public static function toCypherList($value): ?CypherList
124140
{
@@ -131,6 +147,8 @@ public static function toCypherList($value): ?CypherList
131147

132148
/**
133149
* @param mixed $value
150+
*
151+
* @pure
134152
*/
135153
public static function toCypherMap($value): ?CypherMap
136154
{

src/Types/CypherList.php

Lines changed: 162 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
use function array_key_last;
1818
use function array_slice;
1919
use function is_int;
20+
use Laudis\Neo4j\Exception\RuntimeTypeException;
21+
use Laudis\Neo4j\TypeCaster;
2022
use OutOfBoundsException;
21-
use function is_string;
2223
use function sort;
2324
use function usort;
2425

@@ -152,9 +153,166 @@ public function get(int $key)
152153

153154
public function getAsString(int $key): string
154155
{
155-
$mixed = $this->get($key);
156-
if (!is_string($mixed)) {
157-
throw new
156+
$value = $this->get($key);
157+
$tbr = TypeCaster::toString($value);
158+
if ($tbr === null) {
159+
throw new RuntimeTypeException($value, 'string');
158160
}
161+
162+
return $tbr;
163+
}
164+
165+
public function getAsInt(int $key): int
166+
{
167+
$value = $this->get($key);
168+
$tbr = TypeCaster::toInt($value);
169+
if ($tbr === null) {
170+
throw new RuntimeTypeException($value, 'int');
171+
}
172+
173+
return $tbr;
174+
}
175+
176+
public function getAsFloat(int $key): float
177+
{
178+
$value = $this->get($key);
179+
$tbr = TypeCaster::toFloat($value);
180+
if ($tbr === null) {
181+
throw new RuntimeTypeException($value, 'float');
182+
}
183+
184+
return $tbr;
185+
}
186+
187+
public function getAsBool(int $key): bool
188+
{
189+
$value = $this->get($key);
190+
$tbr = TypeCaster::toBool($value);
191+
if ($tbr === null) {
192+
throw new RuntimeTypeException($value, 'bool');
193+
}
194+
195+
return $tbr;
196+
}
197+
198+
/**
199+
* @return null
200+
*/
201+
public function getAsNull(int $key)
202+
{
203+
$this->get($key);
204+
205+
return TypeCaster::toNull();
206+
}
207+
208+
/**
209+
* @template U
210+
*
211+
* @param class-string<U> $class
212+
*
213+
* @return U
214+
*/
215+
public function getAsObject(int $key, string $class): bool
216+
{
217+
$value = $this->get($key);
218+
$tbr = TypeCaster::toClass($value, $class);
219+
if ($tbr === null) {
220+
throw new RuntimeTypeException($value, $class);
221+
}
222+
223+
return $tbr;
224+
}
225+
226+
/**
227+
* @return CypherMap<mixed>
228+
*/
229+
public function getAsCypherMap(int $key): CypherMap
230+
{
231+
$value = $this->get($key);
232+
$tbr = TypeCaster::toCypherMap($value);
233+
if ($tbr === null) {
234+
throw new RuntimeTypeException($value, CypherMap::class);
235+
}
236+
237+
return $tbr;
238+
}
239+
240+
/**
241+
* @return CypherList<mixed>
242+
*/
243+
public function getAsCypherList(int $key): CypherList
244+
{
245+
$value = $this->get($key);
246+
$tbr = TypeCaster::toCypherList($value);
247+
if ($tbr === null) {
248+
throw new RuntimeTypeException($value, CypherList::class);
249+
}
250+
251+
return $tbr;
252+
}
253+
254+
public function getAsDate(int $key): Date
255+
{
256+
return $this->getAsObject($key, Date::class);
257+
}
258+
259+
public function getAsDateTime(int $key): DateTime
260+
{
261+
return $this->getAsObject($key, DateTime::class);
262+
}
263+
264+
public function getAsDuration(int $key): Duration
265+
{
266+
return $this->getAsObject($key, Duration::class);
267+
}
268+
269+
public function getAsLocalDateTime(int $key): LocalDateTime
270+
{
271+
return $this->getAsObject($key, LocalDateTime::class);
272+
}
273+
274+
public function getAsLocalTime(int $key): LocalTime
275+
{
276+
return $this->getAsObject($key, LocalTime::class);
277+
}
278+
279+
public function getAsTime(int $key): Time
280+
{
281+
return $this->getAsObject($key, Time::class);
282+
}
283+
284+
public function getAsNode(int $key): Node
285+
{
286+
return $this->getAsObject($key, Node::class);
287+
}
288+
289+
public function getAsRelationship(int $key): Relationship
290+
{
291+
return $this->getAsObject($key, Relationship::class);
292+
}
293+
294+
public function getAsPath(int $key): Path
295+
{
296+
return $this->getAsObject($key, Path::class);
297+
}
298+
299+
public function getAsCartesian3DPoint(int $key): Cartesian3DPoint
300+
{
301+
return $this->getAsObject($key, Cartesian3DPoint::class);
302+
}
303+
304+
public function getAsCartesianPoint(int $key): CartesianPoint
305+
{
306+
return $this->getAsObject($key, CartesianPoint::class);
307+
}
308+
309+
public function getAsWGS84Point(int $key): WGS84Point
310+
{
311+
return $this->getAsObject($key, WGS84Point::class);
312+
}
313+
314+
public function getAsWGS843DPoint(int $key): WGS843DPoint
315+
{
316+
return $this->getAsObject($key, WGS843DPoint::class);
159317
}
160318
}

0 commit comments

Comments
 (0)