ExpectedDetailsData.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Data\Didit\Session\Parts\Request;
  3. use App\Data\Didit\DiditData;
  4. final readonly class ExpectedDetailsData extends DiditData
  5. {
  6. public function __construct(
  7. public ?string $firstName = null,
  8. public ?string $lastName = null,
  9. public ?string $dateOfBirth = null,
  10. public ?string $identificationNumber = null,
  11. public string $idCountry = 'BRA',
  12. ) {}
  13. public static function fromName(
  14. ?string $fullName,
  15. ?string $dateOfBirth = null,
  16. ?string $document = null,
  17. ): self {
  18. $parts = preg_split('/\s+/', trim((string) $fullName), 2) ?: [];
  19. return new self(
  20. firstName: $parts[0] ?? null,
  21. lastName: $parts[1] ?? null,
  22. dateOfBirth: $dateOfBirth,
  23. identificationNumber: $document ? self::digits($document) : null,
  24. );
  25. }
  26. public function toArray(): array
  27. {
  28. return $this->filterFilledRecursive([
  29. 'first_name' => $this->firstName,
  30. 'last_name' => $this->lastName,
  31. 'date_of_birth' => $this->dateOfBirth,
  32. 'identification_number' => $this->identificationNumber,
  33. 'id_country' => $this->idCountry,
  34. ]);
  35. }
  36. }