|
17 | 17 | use function array_key_last;
|
18 | 18 | use function array_slice;
|
19 | 19 | use function is_int;
|
| 20 | +use Laudis\Neo4j\Exception\RuntimeTypeException; |
| 21 | +use Laudis\Neo4j\TypeCaster; |
20 | 22 | use OutOfBoundsException;
|
21 |
| -use function is_string; |
22 | 23 | use function sort;
|
23 | 24 | use function usort;
|
24 | 25 |
|
@@ -152,9 +153,166 @@ public function get(int $key)
|
152 | 153 |
|
153 | 154 | public function getAsString(int $key): string
|
154 | 155 | {
|
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'); |
158 | 160 | }
|
| 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); |
159 | 317 | }
|
160 | 318 | }
|
0 commit comments