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