Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
MsgFrmtr
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 getDomainCatalog
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDomainCatalog
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 format
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\frmtr\msg;
10
11use MessageFormatter;
12use pvc\frmtr\err\MsgContentNotSetException;
13use pvc\frmtr\err\NonExistentMessageException;
14use pvc\frmtr\err\UnsetLocaleException;
15use pvc\frmtr\Frmtr;
16use pvc\interfaces\msg\DomainCatalogInterface;
17use pvc\interfaces\msg\MsgInterface;
18
19/**
20 * Class MsgFrmtr
21 *
22 * This formatter behaves a bit differently than the others.  Other formatters depend on a locale, this object
23 * depends on a domain catalog (which is appropriate for a locale)
24 *
25 * @extends Frmtr<MsgInterface>
26 */
27class MsgFrmtr extends Frmtr
28{
29    protected DomainCatalogInterface $domainCatalog;
30
31
32    /**
33     * getDomainCatalog
34     * @return DomainCatalogInterface
35     */
36    public function getDomainCatalog(): DomainCatalogInterface
37    {
38        return $this->domainCatalog;
39    }
40
41    /**
42     * setDomainCatalog
43     * @param DomainCatalogInterface $domainCatalog
44     */
45    public function setDomainCatalog(DomainCatalogInterface $domainCatalog): void
46    {
47        $this->domainCatalog = $domainCatalog;
48    }
49
50    /**
51     * format
52     *
53     * @param MsgInterface $value
54     * @return string
55     * @throws MsgContentNotSetException
56     * @throws NonExistentMessageException
57     * @throws UnsetLocaleException
58     */
59    public function format($value): string
60    {
61        /**
62         * ensure the message content is set (e.g. msgId, domain and locale)
63         */
64        if (!$value->contentIsSet()) {
65            throw new MsgContentNotSetException();
66        }
67
68        /**
69         * Calling the load method will only load a new message set if the messages have not already been loaded. The
70         * load method also throws an error if it was unable to load the catalog for the requested domain and locale,
71         * so we know that the domain and the locale of the msg match the domain and locale of the catalog
72         */
73        $this->getDomainCatalog()->load($value->getDomain(), (string)$this->getLocale());
74
75
76        $msgId = $value->getMsgId();
77        if (!$pattern = $this->getDomainCatalog()->getMessage($msgId)) {
78            throw new NonExistentMessageException($msgId);
79        }
80        $frmtr = MessageFormatter::create((string)$this->getLocale(), $pattern);
81
82        /**
83         * if MessageFormatter fails for some reason, return an empty string
84         */
85        return ($frmtr->format($value->getParameters()) ?: '');
86    }
87}