code, 'code'); } public static function fromPayload(string $code, array $data): self { $addressParts = self::extractAddressParts($data); return new self( code: $code, registerInformation: new PagarmeRecipientRegisterInformationData( name: $data['recipient_name'], email: $data['recipient_email'], document: self::digits($data['recipient_document'] ?? null), type: $data['recipient_type'] ?? 'individual', birthdate: self::formatBirthdate($data['birth_date'] ?? null), monthlyIncome: isset($data['monthly_income']) ? (int) $data['monthly_income'] : 1000, professionalOccupation: $data['professional_occupation'] ?? 'autonomo', phoneNumbers: new PagarmeRecipientPhoneNumbersData( self::phoneFromPayload($data['phone'] ?? null), ), address: new PagarmeRecipientAddressData( street: $data['address'], complementary: $addressParts['complementary'], streetNumber: $addressParts['street_number'], neighborhood: $addressParts['neighborhood'], city: $data['city'] ?? null, state: $data['state'] ?? null, zipCode: self::digits($data['zip_code'] ?? null), referencePoint: $addressParts['reference_point'], ), ), defaultBankAccount: PagarmeRecipientBankAccountData::fromArray( $data['recipient_default_bank_account'], ), transferSettings: new PagarmeRecipientTransferSettingsData( transferEnabled: false, transferInterval: 'Daily', transferDay: 0, ), automaticAnticipationSettings: new PagarmeRecipientAutomaticAnticipationSettingsData( enabled: false, ), ); } public function toArray(): array { return $this->filterFilledRecursive([ 'code' => $this->code, 'register_information' => $this->registerInformation, 'default_bank_account' => $this->defaultBankAccount, 'transfer_settings' => $this->transferSettings, 'automatic_anticipation_settings' => $this->automaticAnticipationSettings, ]); } // private static function extractAddressParts(array $data): array { $addressLine = trim((string) ($data['address'] ?? '')); $segments = array_map('trim', explode(',', $addressLine)); $streetSegment = $segments[0] ?? ''; if (($data['number'] ?? null) === null) { preg_match('/^(\d+)/', $streetSegment, $matches); } return [ 'street_number' => (string) ($data['number'] ?? $matches[1] ?? 'S/N'), 'neighborhood' => (string) ($data['district'] ?? $segments[1] ?? 'N/A'), 'reference_point' => (string) ($data['reference_point'] ?? 'N/A'), 'complementary' => (string) ($data['complement'] ?? 'N/A'), ]; } private static function formatBirthdate(mixed $birthdate): ?string { if ($birthdate === null || $birthdate === '') { return null; } if ($birthdate instanceof \DateTimeInterface) { return Carbon::instance($birthdate)->format('d/m/Y'); } $birthdate = trim((string) $birthdate); if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $birthdate) === 1) { return $birthdate; } if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthdate) === 1) { return Carbon::createFromFormat('Y-m-d', $birthdate)->format('d/m/Y'); } return Carbon::parse($birthdate)->format('d/m/Y'); } private static function phoneFromPayload(?string $phone): PagarmeRecipientPhoneData { $digits = self::digits($phone); if (strlen($digits) < 10) { return new PagarmeRecipientPhoneData( ddd: '11', number: '999999999', type: 'mobile', ); } if (str_starts_with($digits, '55')) { $digits = substr($digits, 2); } return new PagarmeRecipientPhoneData( ddd: substr($digits, 0, 2), number: substr($digits, 2), type: 'mobile', ); } }