Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Api
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 mount
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Config;
6
7final class Api
8{
9    public function __construct(
10        private readonly \Cosray\Config $config,
11    ) {}
12
13    /** Where the API is mounted, like `/api`; null leaves it off. */
14    public ?string $path {
15        get => self::mount($this->config->get('api.path'));
16    }
17
18    /** @var list<string> */
19    public array $origins {
20        get => array_values(array_filter(
21            (array) $this->config->get('api.origins'),
22            static fn(mixed $origin): bool => is_string($origin) && $origin !== '',
23        ));
24    }
25
26    public int $maxAge {
27        get => max(0, (int) $this->config->get('api.max_age'));
28    }
29
30    /** A headless frontend's preview URL with `{uid}`, `{token}` and `{locale}` placeholders. */
31    public ?string $preview {
32        get => is_string($this->config->get('api.preview')) && $this->config->get('api.preview') !== ''
33            ? $this->config->get('api.preview')
34            : null;
35    }
36
37    private static function mount(mixed $path): ?string
38    {
39        if (!is_string($path) || trim($path, " \t/") === '') {
40            return null;
41        }
42
43        return '/' . trim($path, " \t/");
44    }
45}