| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace App\Data\Pagarme\Request\PagarmeRecipientRequestData;
- use App\Data\Pagarme\PagarmeData;
- use App\Data\Pagarme\Request\PagarmeRecipientRequestData\PagarmeRecipientRegisterInformationData\PagarmeRecipientAddressData;
- use App\Data\Pagarme\Request\PagarmeRecipientRequestData\PagarmeRecipientRegisterInformationData\PagarmeRecipientPhoneNumbersData\PagarmeRecipientPhoneData;
- use App\Data\Pagarme\Request\PagarmeRecipientRequestData\PagarmeRecipientRegisterInformationData\PagarmeRecipientPhoneNumbersData\PagarmeRecipientPhoneNumbersData;
- use App\Data\Pagarme\Request\PagarmeRecipientRequestData\PagarmeRecipientRegisterInformationData\PagarmeRecipientRegisterInformationData;
- use Carbon\Carbon;
- final readonly class PagarmeRecipientRequestData extends PagarmeData
- {
- public function __construct(
- public string $code,
- public PagarmeRecipientRegisterInformationData $registerInformation,
- public PagarmeRecipientBankAccountData $defaultBankAccount,
- public PagarmeRecipientTransferSettingsData $transferSettings,
- public PagarmeRecipientAutomaticAnticipationSettingsData $automaticAnticipationSettings,
- ) {
- self::requireFilled($this->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',
- );
- }
- }
|