Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
DomainCatalog
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
7 / 7
10
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
 load
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getDomain
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLocale
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMessages
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMessage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isLoaded
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\msg;
10
11use pvc\interfaces\msg\DomainCatalogInterface;
12use pvc\interfaces\msg\DomainCatalogLoaderInterface;
13
14/**
15 * Class DomainCatalog
16 */
17class DomainCatalog implements DomainCatalogInterface
18{
19    /**
20     * @var string
21     */
22    protected string $domain;
23
24    /**
25     * @var string
26     */
27    protected string $locale;
28
29    /**
30     * @var array<string>
31     */
32    protected array $messages;
33
34    /**
35     * @param DomainCatalogLoaderInterface $loader
36     */
37    public function __construct(protected DomainCatalogLoaderInterface $loader)
38    {
39    }
40
41    /**
42     * @param non-empty-string $domain
43     * @param non-empty-string $locale
44     * @return void
45     */
46    public function load(string $domain, string $locale): void
47    {
48        /**
49         * no need to repeat loading a catalog....
50         */
51        if ($this->isLoaded($domain, $locale)) {
52            return;
53        }
54
55        $this->messages = $this->loader->loadCatalog($domain, $locale);
56        $this->domain = $domain;
57        $this->locale = $locale;
58    }
59
60    /**
61     * @return string
62     */
63    public function getDomain(): string
64    {
65        return $this->domain ?? '';
66    }
67
68    /**
69     * @return string
70     */
71    public function getLocale(): string
72    {
73        return $this->locale ?? '';
74    }
75
76    /**
77     * @return array<string>
78     */
79    public function getMessages(): array
80    {
81        return $this->messages ?? [];
82    }
83
84    /**
85     * getMessage
86     * @param string $messageId
87     * @return string|null
88     */
89    public function getMessage(string $messageId): ?string
90    {
91        /**
92         * if the messageId is not in the catalog, just return it.
93         */
94        return $this->messages[$messageId] ?? null;
95    }
96
97    /**
98     * @param non-empty-string $domain
99     * @param non-empty-string $locale
100     * @return bool
101     */
102    protected function isLoaded(string $domain, string $locale): bool
103    {
104        /**
105         * recall that domain and locale are set simultaneously via the load method, so either both properties are
106         * empty or they are both set.
107         */
108        if (empty($this->getDomain())) return false;
109
110        /**
111         * if domain and locale are set, then check to see if the catalog is loaded with the arguments specified.
112         */
113        return ($domain == $this->getDomain() && $locale == $this->getLocale());
114    }
115}