1
|
|
|
<?php declare (strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\l10n\Messages; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\l10n\Contracts\Messages\ResourceBundleInterface; |
22
|
|
|
use function array_keys; |
23
|
|
|
use function assert; |
24
|
|
|
use function is_scalar; |
25
|
|
|
use function is_string; |
26
|
|
|
use function locale_canonicalize; |
27
|
|
|
use function strlen; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @package Limoncello\l10n |
31
|
|
|
*/ |
32
|
|
|
class ResourceBundle implements ResourceBundleInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $locale; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $namespace; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $properties; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $locale |
51
|
|
|
* @param string $namespace |
52
|
|
|
* @param array $properties |
53
|
|
|
*/ |
54
|
9 |
|
public function __construct(string $locale, string $namespace, array $properties) |
55
|
|
|
{ |
56
|
9 |
|
$this->setLocale($locale)->setNamespace($namespace)->setProperties($properties); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
9 |
|
public function getLocale(): string |
63
|
|
|
{ |
64
|
9 |
|
return $this->locale; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
9 |
|
public function getNamespace(): string |
71
|
|
|
{ |
72
|
9 |
|
return $this->namespace; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
9 |
|
public function getKeys(): array |
79
|
|
|
{ |
80
|
9 |
|
return array_keys($this->getProperties()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
9 |
|
public function getValue(string $key): string |
87
|
|
|
{ |
88
|
9 |
|
$properties = $this->getProperties(); |
89
|
|
|
|
90
|
9 |
|
return $properties[$key]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $locale |
95
|
|
|
* |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
9 |
|
public function setLocale(string $locale): self |
99
|
|
|
{ |
100
|
9 |
|
assert(empty($locale) === false && locale_canonicalize($locale) === $locale); |
101
|
|
|
|
102
|
9 |
|
$this->locale = $locale; |
103
|
|
|
|
104
|
9 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $namespace |
109
|
|
|
* |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
9 |
|
public function setNamespace(string $namespace): self |
113
|
|
|
{ |
114
|
9 |
|
assert(empty($namespace) === false); |
115
|
|
|
|
116
|
9 |
|
$this->namespace = $namespace; |
117
|
|
|
|
118
|
9 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $properties |
123
|
|
|
* |
124
|
|
|
* @return self |
125
|
|
|
*/ |
126
|
9 |
|
public function setProperties(array $properties): self |
127
|
|
|
{ |
128
|
|
|
// check all keys and values are non-empty strings |
129
|
9 |
|
$this->properties = []; |
130
|
9 |
|
foreach ($properties as $key => $value) { |
131
|
9 |
|
assert(is_scalar($key) === true && strlen((string)$key) > 0); |
132
|
9 |
|
assert(is_string($value) === true && strlen($value) > 0); |
133
|
9 |
|
$this->properties[(string)$key] = (string)$value; |
134
|
|
|
} |
135
|
|
|
|
136
|
9 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
9 |
|
public function getProperties(): array |
143
|
|
|
{ |
144
|
9 |
|
return $this->properties; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|