<?php

namespace App\Data\Pagarme\Customer\Parts\Response;

use App\Data\Pagarme\PagarmeResponseData;

final readonly class PhonesData extends PagarmeResponseData
{
    public function __construct(
        public ?PhoneData $homePhone,
        public ?PhoneData $mobilePhone,
    ) {}

    public static function fromArray(array $payload): static
    {
        $home   = static::arrArray($payload, 'home_phone');
        $mobile = static::arrArray($payload, 'mobile_phone');

        return new self(
            homePhone:   ! empty($home)   ? PhoneData::fromArray($home)   : null,
            mobilePhone: ! empty($mobile) ? PhoneData::fromArray($mobile) : null,
        );
    }

    public function toArray(): array
    {
        return array_filter([
            'home_phone'   => $this->homePhone?->toArray(),
            'mobile_phone' => $this->mobilePhone?->toArray(),
        ]);
    }
}
