1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zemit\Translate\Adapter; |
4
|
|
|
|
5
|
|
|
use Phalcon\Translate\Adapter\AbstractAdapter; |
6
|
|
|
use Phalcon\Translate\Exception; |
7
|
|
|
use Phalcon\Translate\InterpolatorFactory; |
8
|
|
|
|
9
|
|
|
class NestedNativeArray extends AbstractAdapter implements \ArrayAccess |
10
|
|
|
{ |
11
|
|
|
private array $translate = []; |
12
|
|
|
|
13
|
|
|
private bool $triggerError = false; |
14
|
|
|
|
15
|
|
|
protected string $delimiter = '.'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Zemit\Translate\Adapter\NestedNativeArray constructor |
19
|
|
|
* |
20
|
|
|
* @param InterpolatorFactory $interpolator |
21
|
|
|
* @param array $options [ |
22
|
|
|
* 'content' => '', |
23
|
|
|
* 'triggerError' => false, |
24
|
|
|
* 'delimiter' => '.', |
25
|
|
|
* ] |
26
|
|
|
*/ |
27
|
|
|
public function __construct(InterpolatorFactory $interpolator, array $options) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($interpolator, $options); |
30
|
|
|
$this->delimiter = $options['delimiter'] ?? $this->delimiter; |
31
|
|
|
$this->triggerError = $options['triggerError'] ?? $this->triggerError; |
32
|
|
|
$this->translate = $options['content'] ?? $this->translate; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Check whether is defined a translation key in the internal array |
37
|
|
|
* @deprecated |
38
|
|
|
*/ |
39
|
|
|
public function exists(string $index): bool |
40
|
|
|
{ |
41
|
|
|
return $this->has($index); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Check whether is defined a translation key in the internal array |
46
|
|
|
*/ |
47
|
|
|
public function has(string $index): bool |
48
|
|
|
{ |
49
|
|
|
$translate = $this->translate; |
50
|
|
|
|
51
|
|
|
if (isset($translate[$index])) { |
52
|
|
|
return $translate[$index]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
foreach (explode($this->delimiter, $index) as $nestedIndex) { |
56
|
|
|
|
57
|
|
|
if (is_array($translate) && array_key_exists($nestedIndex, $translate)) { |
58
|
|
|
$translate = $translate[$nestedIndex]; |
59
|
|
|
} |
60
|
|
|
else { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function notFound(string $index): string |
69
|
|
|
{ |
70
|
|
|
if ($this->triggerError) { |
71
|
|
|
throw new Exception('Cannot find translation key: ' . $index); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $index; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the translation related to the given key |
79
|
|
|
*/ |
80
|
|
|
public function query(string $translateKey, array $placeholders = []): string |
81
|
|
|
{ |
82
|
|
|
$translate = $this->translate; |
83
|
|
|
|
84
|
|
|
if (isset($translate[$translateKey])) { |
85
|
|
|
return $translate[$translateKey]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach (explode($this->delimiter, $translateKey) as $nestedIndex) { |
89
|
|
|
|
90
|
|
|
if (is_array($translate) && array_key_exists($nestedIndex, $translate)) { |
91
|
|
|
$translate = $translate[$nestedIndex]; |
92
|
|
|
} |
93
|
|
|
else { |
94
|
|
|
return $this->notFound($translateKey); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->replacePlaceholders($translate, $placeholders); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|