-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatalog.hh
More file actions
134 lines (115 loc) · 3.2 KB
/
Copy pathCatalog.hh
File metadata and controls
134 lines (115 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Intl;
use Titon\Intl\Exception\InvalidMessageKeyException;
use Titon\Intl\Exception\MissingMessageException;
/**
* The `Catalog` represents an accumulation of message strings for a specific domain and catalog
* from a list of defined resource bundles.
*
* @package Titon\Intl
*/
class Catalog {
/**
* Domain the catalog belongs to.
*
* @var string
*/
protected string $domain;
/**
* List of messages within the catalog.
*
* @var \Titon\Intl\MessageMap
*/
protected MessageMap $messages = Map {};
/**
* Name of the catalog.
*
* @var string
*/
protected string $name;
/**
* Store the domain and catalog name and message strings.
*
* @param string $name
* @param string $domain
* @param \Titon\Intl\MessageMap $messages
*/
public function __construct(string $name, string $domain, MessageMap $messages) {
$this->name = $name;
$this->domain = $domain;
$this->messages = $messages;
}
/**
* Return the domain name.
*
* @return string
*/
public function getDomain(): string {
return $this->domain;
}
/**
* Return a message from the catalog defined by key.
*
* @param string $key
* @return string
* @throws \Titon\Intl\Exception\MissingMessageException
*/
public function getMessage(string $key): string {
if ($this->messages->contains($key)) {
return $this->messages[$key];
}
throw new MissingMessageException(sprintf('Message key %s.%s.%s does not exist', $this->getDomain(), $this->getName(), $key));
}
/**
* Return the list of messages.
*
* @return \Titon\Intl\MessageMap
*/
public function getMessages(): MessageMap {
return $this->messages;
}
/**
* Return the catalog name.
*
* @return string
*/
public function getName(): string {
return $this->name;
}
/**
* Parse out the domain, catalog, and key for a message lookup.
*
* @param string $key
* @return \Titon\Intl\MessageKey
* @throws \Titon\Intl\Exception\InvalidMessageKeyException
*/
<<__Memoize>>
public static function parseKey(string $key): MessageKey {
$parts = explode('.', preg_replace('/[^-_a-z0-9\.]+/i', '', $key), 3);
$count = count($parts);
$domain = MessageLoader::DEFAULT_DOMAIN;
$catalog = MessageLoader::DEFAULT_CATALOG;
if (!$key || $count === 0) {
throw new InvalidMessageKeyException(sprintf('No domain or catalog present for %s key', $key));
} else if ($count === 1) {
$key = $parts[0];
} else if ($count === 2) {
$catalog = $parts[0];
$key = $parts[1];
} else {
$domain = $parts[0];
$catalog = $parts[1];
$key = $parts[2];
}
return shape(
'domain' => $domain,
'catalog' => $catalog,
'id' => $key
);
}
}