|
26 | 26 | use function is_string;
|
27 | 27 | use function ksort;
|
28 | 28 | use Laudis\Neo4j\Databags\Pair;
|
| 29 | +use Laudis\Neo4j\Exception\RuntimeTypeException; |
| 30 | +use Laudis\Neo4j\TypeCaster; |
29 | 31 | use function method_exists;
|
30 | 32 | use OutOfBoundsException;
|
31 | 33 | use stdClass;
|
@@ -339,4 +341,296 @@ public function jsonSerialize()
|
339 | 341 |
|
340 | 342 | return parent::jsonSerialize();
|
341 | 343 | }
|
| 344 | + |
| 345 | + /** |
| 346 | + * @param mixed $default |
| 347 | + */ |
| 348 | + public function getAsString(string $key, $default = null): string |
| 349 | + { |
| 350 | + if (func_num_args() === 1) { |
| 351 | + $value = $this->get($key); |
| 352 | + } else { |
| 353 | + $value = $this->get($key, $default); |
| 354 | + } |
| 355 | + $tbr = TypeCaster::toString($value); |
| 356 | + if ($tbr === null) { |
| 357 | + throw new RuntimeTypeException($value, 'string'); |
| 358 | + } |
| 359 | + |
| 360 | + return $tbr; |
| 361 | + } |
| 362 | + |
| 363 | + /** |
| 364 | + * @param mixed $default |
| 365 | + */ |
| 366 | + public function getAsInt(string $key, $default): int |
| 367 | + { |
| 368 | + if (func_num_args() === 1) { |
| 369 | + $value = $this->get($key); |
| 370 | + } else { |
| 371 | + $value = $this->get($key, $default); |
| 372 | + } |
| 373 | + $tbr = TypeCaster::toInt($value); |
| 374 | + if ($tbr === null) { |
| 375 | + throw new RuntimeTypeException($value, 'int'); |
| 376 | + } |
| 377 | + |
| 378 | + return $tbr; |
| 379 | + } |
| 380 | + |
| 381 | + /** |
| 382 | + * @param mixed $default |
| 383 | + */ |
| 384 | + public function getAsFloat(int $key, $default = null): float |
| 385 | + { |
| 386 | + if (func_num_args() === 1) { |
| 387 | + $value = $this->get($key); |
| 388 | + } else { |
| 389 | + $value = $this->get($key, $default); |
| 390 | + } |
| 391 | + $tbr = TypeCaster::toFloat($value); |
| 392 | + if ($tbr === null) { |
| 393 | + throw new RuntimeTypeException($value, 'float'); |
| 394 | + } |
| 395 | + |
| 396 | + return $tbr; |
| 397 | + } |
| 398 | + |
| 399 | + /** |
| 400 | + * @param mixed $default |
| 401 | + */ |
| 402 | + public function getAsBool(int $key, $default = null): bool |
| 403 | + { |
| 404 | + if (func_num_args() === 1) { |
| 405 | + $value = $this->get($key); |
| 406 | + } else { |
| 407 | + $value = $this->get($key, $default); |
| 408 | + } |
| 409 | + $tbr = TypeCaster::toBool($value); |
| 410 | + if ($tbr === null) { |
| 411 | + throw new RuntimeTypeException($value, 'bool'); |
| 412 | + } |
| 413 | + |
| 414 | + return $tbr; |
| 415 | + } |
| 416 | + |
| 417 | + /** |
| 418 | + * @param mixed $default |
| 419 | + * |
| 420 | + * @return null |
| 421 | + */ |
| 422 | + public function getAsNull(int $key, $default = null) |
| 423 | + { |
| 424 | + if (func_num_args() === 1) { |
| 425 | + $this->get($key); |
| 426 | + } |
| 427 | + |
| 428 | + return TypeCaster::toNull(); |
| 429 | + } |
| 430 | + |
| 431 | + /** |
| 432 | + * @template U |
| 433 | + * |
| 434 | + * @param class-string<U> $class |
| 435 | + * @param mixed $default |
| 436 | + * |
| 437 | + * @return U |
| 438 | + */ |
| 439 | + public function getAsObject(int $key, string $class, $default = null): object |
| 440 | + { |
| 441 | + if (func_num_args() === 1) { |
| 442 | + $value = $this->get($key); |
| 443 | + } else { |
| 444 | + $value = $this->get($key, $default); |
| 445 | + } |
| 446 | + $tbr = TypeCaster::toClass($value, $class); |
| 447 | + if ($tbr === null) { |
| 448 | + throw new RuntimeTypeException($value, $class); |
| 449 | + } |
| 450 | + |
| 451 | + return $tbr; |
| 452 | + } |
| 453 | + |
| 454 | + /** |
| 455 | + * @param mixed $default |
| 456 | + * |
| 457 | + * @return CypherMap<mixed> |
| 458 | + */ |
| 459 | + public function getAsCypherMap(int $key, $default = null): CypherMap |
| 460 | + { |
| 461 | + if (func_num_args() === 1) { |
| 462 | + $value = $this->get($key); |
| 463 | + } else { |
| 464 | + $value = $this->get($key, $default); |
| 465 | + } |
| 466 | + $tbr = TypeCaster::toCypherMap($value); |
| 467 | + if ($tbr === null) { |
| 468 | + throw new RuntimeTypeException($value, CypherMap::class); |
| 469 | + } |
| 470 | + |
| 471 | + return $tbr; |
| 472 | + } |
| 473 | + |
| 474 | + /** |
| 475 | + * @param mixed $default |
| 476 | + * |
| 477 | + * @return CypherList<mixed> |
| 478 | + */ |
| 479 | + public function getAsCypherList(int $key, $default = null): CypherList |
| 480 | + { |
| 481 | + if (func_num_args() === 1) { |
| 482 | + $value = $this->get($key); |
| 483 | + } else { |
| 484 | + $value = $this->get($key, $default); |
| 485 | + } |
| 486 | + $tbr = TypeCaster::toCypherList($value); |
| 487 | + if ($tbr === null) { |
| 488 | + throw new RuntimeTypeException($value, CypherList::class); |
| 489 | + } |
| 490 | + |
| 491 | + return $tbr; |
| 492 | + } |
| 493 | + |
| 494 | + /** |
| 495 | + * @param mixed $default |
| 496 | + */ |
| 497 | + public function getAsDate(int $key, $default = null): Date |
| 498 | + { |
| 499 | + if (func_num_args() === 1) { |
| 500 | + return $this->getAsObject($key, Date::class); |
| 501 | + } |
| 502 | + return $this->getAsObject($key, Date::class, $default); |
| 503 | + } |
| 504 | + |
| 505 | + /** |
| 506 | + * @param mixed $default |
| 507 | + */ |
| 508 | + public function getAsDateTime(int $key, $default = null): DateTime |
| 509 | + { |
| 510 | + if (func_num_args() === 1) { |
| 511 | + return $this->getAsObject($key, DateTime::class); |
| 512 | + } |
| 513 | + return $this->getAsObject($key, DateTime::class, $default); |
| 514 | + } |
| 515 | + |
| 516 | + /** |
| 517 | + * @param mixed $default |
| 518 | + */ |
| 519 | + public function getAsDuration(int $key, $default = null): Duration |
| 520 | + { |
| 521 | + if (func_num_args() === 1) { |
| 522 | + return $this->getAsObject($key, Duration::class); |
| 523 | + } |
| 524 | + return $this->getAsObject($key, Duration::class, $default); |
| 525 | + } |
| 526 | + |
| 527 | + /** |
| 528 | + * @param mixed $default |
| 529 | + */ |
| 530 | + public function getAsLocalDateTime(int $key, $default = null): LocalDateTime |
| 531 | + { |
| 532 | + if (func_num_args() === 1) { |
| 533 | + return $this->getAsObject($key, LocalDateTime::class); |
| 534 | + } |
| 535 | + return $this->getAsObject($key, LocalDateTime::class, $default); |
| 536 | + } |
| 537 | + |
| 538 | + /** |
| 539 | + * @param mixed $default |
| 540 | + */ |
| 541 | + public function getAsLocalTime(int $key, $default = null): LocalTime |
| 542 | + { |
| 543 | + if (func_num_args() === 1) { |
| 544 | + return $this->getAsObject($key, LocalTime::class); |
| 545 | + } |
| 546 | + return $this->getAsObject($key, LocalTime::class, $default); |
| 547 | + } |
| 548 | + |
| 549 | + /** |
| 550 | + * @param mixed $default |
| 551 | + */ |
| 552 | + public function getAsTime(int $key, $default = null): Time |
| 553 | + { |
| 554 | + if (func_num_args() === 1) { |
| 555 | + return $this->getAsObject($key, Time::class); |
| 556 | + } |
| 557 | + return $this->getAsObject($key, Time::class, $default); |
| 558 | + } |
| 559 | + |
| 560 | + /** |
| 561 | + * @param mixed $default |
| 562 | + */ |
| 563 | + public function getAsNode(int $key, $default = null): Node |
| 564 | + { |
| 565 | + if (func_num_args() === 1) { |
| 566 | + return $this->getAsObject($key, Node::class); |
| 567 | + } |
| 568 | + return $this->getAsObject($key, Node::class, $default); |
| 569 | + } |
| 570 | + |
| 571 | + /** |
| 572 | + * @param mixed $default |
| 573 | + */ |
| 574 | + public function getAsRelationship(int $key, $default = null): Relationship |
| 575 | + { |
| 576 | + if (func_num_args() === 1) { |
| 577 | + return $this->getAsObject($key, Relationship::class); |
| 578 | + } |
| 579 | + return $this->getAsObject($key, Relationship::class, $default); |
| 580 | + } |
| 581 | + |
| 582 | + /** |
| 583 | + * @param mixed $default |
| 584 | + */ |
| 585 | + public function getAsPath(int $key, $default = null): Path |
| 586 | + { |
| 587 | + if (func_num_args() === 1) { |
| 588 | + return $this->getAsObject($key, Path::class); |
| 589 | + } |
| 590 | + return $this->getAsObject($key, Path::class, $default); |
| 591 | + } |
| 592 | + |
| 593 | + /** |
| 594 | + * @param mixed $default |
| 595 | + */ |
| 596 | + public function getAsCartesian3DPoint(int $key, $default = null): Cartesian3DPoint |
| 597 | + { |
| 598 | + if (func_num_args() === 1) { |
| 599 | + return $this->getAsObject($key, Cartesian3DPoint::class); |
| 600 | + } |
| 601 | + return $this->getAsObject($key, Cartesian3DPoint::class, $default); |
| 602 | + } |
| 603 | + |
| 604 | + /** |
| 605 | + * @param mixed $default |
| 606 | + */ |
| 607 | + public function getAsCartesianPoint(int $key, $default = null): CartesianPoint |
| 608 | + { |
| 609 | + if (func_num_args() === 1) { |
| 610 | + return $this->getAsObject($key, CartesianPoint::class); |
| 611 | + } |
| 612 | + return $this->getAsObject($key, CartesianPoint::class, $default); |
| 613 | + } |
| 614 | + |
| 615 | + /** |
| 616 | + * @param mixed $default |
| 617 | + */ |
| 618 | + public function getAsWGS84Point(int $key, $default = null): WGS84Point |
| 619 | + { |
| 620 | + if (func_num_args() === 1) { |
| 621 | + return $this->getAsObject($key, WGS84Point::class); |
| 622 | + } |
| 623 | + return $this->getAsObject($key, WGS84Point::class, $default); |
| 624 | + } |
| 625 | + |
| 626 | + /** |
| 627 | + * @param mixed $default |
| 628 | + */ |
| 629 | + public function getAsWGS843DPoint(int $key, $default = null): WGS843DPoint |
| 630 | + { |
| 631 | + if (func_num_args() === 1) { |
| 632 | + return $this->getAsObject($key, WGS843DPoint::class); |
| 633 | + } |
| 634 | + return $this->getAsObject($key, WGS843DPoint::class, $default); |
| 635 | + } |
342 | 636 | }
|
0 commit comments