Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
TemplateResolver
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 resolve
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 map
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2
3namespace Computator\FrameworkUtils\PHPTemplate;
4
5use TypeError;
6use ValueError;
7
8/**
9 * Resolves template names to template instances.
10 */
11class TemplateResolver {
12    /**
13     * @param string $new_template_class A subclass of `Templates\Base` which will
14     *                                   be used to instantiate new templates.
15     * @throws TypeError
16     */
17    public function __construct(
18        protected string $new_template_class = Templates\File::class,
19    ) {
20        if (!is_a($new_template_class, Templates\Base::class, true))
21            throw new TypeError("'\$new_template_class' must be an instance of Templates\Base");
22    }
23
24    /**
25     * Resolve a template name to a new template instance.
26     *
27     * @throws ValueError
28     */
29    final public function resolve(string $template): Templates\Base {
30        if ($template == '')
31            throw new ValueError("'\$template' can not be empty");
32        return $this->map($template);
33    }
34
35    /**
36     * Map a template name to a new class instance.
37     *
38     * This method is intended to be overridden by descendant classes to control the
39     * mapping of template names to class instances.
40     *
41     * @return Templates\Base The new template instance
42     */
43    protected function map(string $template): Templates\Base {
44        return new $this->new_template_class($template);
45    }
46}