1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of byrokrat\autogiro. |
4
|
|
|
* |
5
|
|
|
* byrokrat\autogiro is free software: you can redistribute it and/or |
6
|
|
|
* modify it under the terms of the GNU General Public License as published |
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* byrokrat\autogiro is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
* |
18
|
|
|
* Copyright 2016-18 Hannes Forsgård |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
22
|
|
|
|
23
|
|
|
namespace byrokrat\autogiro; |
24
|
|
|
|
25
|
|
|
class MessageRetriever |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Match all wildcard key |
29
|
|
|
*/ |
30
|
|
|
const WILDCARD = '*'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Default location of messages file |
34
|
|
|
*/ |
35
|
|
|
const DEFAULT_MESSAGE_STORE = __DIR__ . '/messages.json'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $messages; |
41
|
|
|
|
42
|
|
|
public function __construct(array $messages = []) |
43
|
|
|
{ |
44
|
|
|
$this->messages = $messages ?: json_decode(file_get_contents(self::DEFAULT_MESSAGE_STORE), true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function readMessage(string ...$keys): string |
48
|
|
|
{ |
49
|
|
|
return $this->pickMessage($this->messages, ...$keys); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function pickMessage(array $messages, string $key, string ...$additionalKeys): string |
53
|
|
|
{ |
54
|
|
|
$value = $messages[$key] ?? []; |
55
|
|
|
|
56
|
|
|
if ($additionalKeys) { |
|
|
|
|
57
|
|
|
$value = $this->pickMessage((array)$value, ...$additionalKeys); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!$value && $key != self::WILDCARD) { |
61
|
|
|
$value = $this->pickMessage($messages, self::WILDCARD, ...$additionalKeys); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!is_scalar($value)) { |
65
|
|
|
return ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $value; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.