Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Locale
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 fallback
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 fallbacks
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 domain
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray;
6
7class Locale
8{
9    public readonly string $urlPrefix;
10    /** @var array<string> */
11    public readonly array $domains;
12
13    /** @param array<string>|null $domains */
14    public function __construct(
15        protected readonly Locales $locales,
16        public readonly string $id,
17        public readonly string $title,
18        public readonly ?string $fallback = null,
19        public readonly ?string $pgDict = null,
20        ?array $domains = null,
21        ?string $urlPrefix = null,
22    ) {
23        if ($domains) {
24            $this->domains = array_map(static fn($d) => strtolower($d), $domains);
25        } else {
26            $this->domains = [];
27        }
28
29        $this->urlPrefix = $urlPrefix ?: $id;
30    }
31
32    /**
33     * The next locale in the fallback chain, used for content translations
34     * stored in the database and for UI strings translated through verba.
35     */
36    public function fallback(): ?Locale
37    {
38        return $this->fallback ? $this->locales->get($this->fallback) : null;
39    }
40
41    /**
42     * The ids of the whole fallback chain in resolution order, stopping
43     * before a locale repeats.
44     *
45     * @return list<string>
46     */
47    public function fallbacks(): array
48    {
49        $chain = [];
50        $locale = $this->fallback();
51
52        while ($locale !== null && $locale->id !== $this->id && !in_array($locale->id, $chain, true)) {
53            $chain[] = $locale->id;
54            $locale = $locale->fallback();
55        }
56
57        return $chain;
58    }
59
60    public function domain(int $index = 0): string
61    {
62        return $this->domains[$index];
63    }
64}