gearsdigital/reporter-for-kirby

View on GitHub
lib/Traits/Expander.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace KirbyReporter\Traits;

use QL\UriTemplate\Exception;
use QL\UriTemplate\UriTemplate;

/**
 * Expander is a simple helper to expand templated Urls.
 *
 * Trailing slashes are removed.
 *
 * @package KirbyReporter\Traits
 * @author Steffen Giers <steffen.giers@gmail.com>
 */
trait Expander
{
    /**
     * Utilizes RFC 6570 (Level 4) Url expansion.
     *
     * @param  string  $template
     * @param  array  $data
     *
     * @return string
     * @throws Exception
     */
    public final function expandUrl(string $template, array $data): string
    {
        $tpl = new UriTemplate($template);
        return rtrim($tpl->expand($data), '/');
    }
}