<?php

namespace AuthStack\Driver\AUDGeneric\App\REST;

use Webmozart\Assert\Assert;

final class Route
{
    protected $url;
    protected $method;
    protected $path;
    protected $verify;

    public function __construct(string $url ,string $method, string $path, bool $verify)
    {
        $this->url = $url;
        $this->method = $method;
        $this->path = $path;
        $this->verify = $verify;
    }

    public static function fromArray(array $data): self
    {
        Assert::keyExists($data, 'url', "Required key 'url' not present");
        Assert::keyExists($data, 'method', "Required key 'method' not present");
        Assert::keyExists($data, 'path', "Required key 'path' not present");
        Assert::keyExists($data, 'verify', "Required key 'verify' not present");

        return new static($data['url'], $data['method'], $data['path'], $data['verify']);
    }

    /**
     * @return string
     */
    public function getUrl(): string
    {
        return $this->url;
    }

    /**
     * @return string
     */
    public function getMethod(): string
    {
        return $this->method;
    }

    /**
     * Returns the route path with placeholders filled out.
     * Example: /api/v1/delete/%s/%s
     * Params: ['1', 'test']
     * Result: /api/v1/delete/1/test
     *
     * @param array|null $params
     * @return string
     */
    public function getPath(?array $params = []): string
    {
        return vsprintf($this->path, $params);
    }

    /**
     * @return bool
     */
    public function verifySSL(): bool
    {
        return $this->verify;
    }
}
