<?php

namespace Containers\Dto;

readonly class ContainerPortDto
{
    public function __construct(
        public int     $container_port,
        public string  $host_ip,
        public ?int    $host_port,
        public string  $protocol,
    ) {
    }

    public static function fromArray(array $data): self
    {
        return new self(
            container_port: (int) ($data['container_port'] ?? 0),
            host_ip: (string) ($data['host_ip'] ?? ''),
            host_port: isset($data['host_port']) ? (int) $data['host_port'] : null,
            protocol: (string) ($data['protocol'] ?? 'tcp'),
        );
    }

    public function toArray(): array
    {
        return [
            'container_port' => $this->container_port,
            'host_ip' => $this->host_ip,
            'host_port' => $this->host_port,
            'protocol' => $this->protocol,
        ];
    }
}
