<?php

declare(strict_types=1);

namespace Gls\GlsPoland\Carrier;

final class ParcelShopType implements \Stringable
{
    public const APM = 'apm';
    public const PUDO = 'pudo';

    private const TYPES = [
        self::APM,
        self::PUDO,
    ];

    private $value;

    private function __construct(string $type)
    {
        $this->value = $type;
    }

    public static function from(string $type): self
    {
        if (!in_array($type, self::TYPES, true)) {
            throw new \InvalidArgumentException(sprintf('Unrecognized ParcelShop type "%s".', $type));
        }

        return new self($type);
    }

    public static function tryFrom($value): ?self
    {
        if (null === $value || '' === $value || !in_array($value, self::TYPES, true)) {
            return null;
        }

        return new self($value);
    }

    public function getValue(): string
    {
        return $this->value;
    }

    public function __toString(): string
    {
        return $this->value;
    }
}
