<?php

namespace App\Data\Pagarme\Response\CustomerResponseData\CustomerPhonesResponseData;

use App\Data\Pagarme\PagarmeResponseData;

final readonly class CustomerPhonesResponseData extends PagarmeResponseData
{
    public function __construct(
        public ?PhoneResponseData $homePhone,
        public ?PhoneResponseData $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)   ? PhoneResponseData::fromArray($home)   : null,
            mobilePhone: ! empty($mobile) ? PhoneResponseData::fromArray($mobile) : null,
        );
    }

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