1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Printful\GettextCms; |
4
|
|
|
|
5
|
|
|
use Printful\GettextCms\Interfaces\MessageRepositoryInterface; |
6
|
|
|
use Printful\GettextCms\Structures\MessageItem; |
7
|
|
|
|
8
|
|
|
class MessageArrayRepository implements MessageRepositoryInterface |
9
|
|
|
{ |
10
|
|
|
/** @var MessageItem[] */ |
11
|
|
|
private $store = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Retrieve all items in repository |
15
|
|
|
* |
16
|
|
|
* @return MessageItem[] |
17
|
|
|
*/ |
18
|
|
|
public function getItems(): array |
19
|
|
|
{ |
20
|
|
|
return $this->store; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
public function save(MessageItem $item): bool |
27
|
|
|
{ |
28
|
|
|
$this->store[$item->key] = $item; |
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
|
|
public function getAll(string $locale, string $domain): array |
36
|
|
|
{ |
37
|
|
|
return array_filter($this->store, function (MessageItem $item) use ($locale, $domain) { |
38
|
|
|
return $item->locale === $locale && $item->domain === $domain; |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function getEnabled(string $locale, string $domain): array |
46
|
|
|
{ |
47
|
|
|
$allItems = $this->getAll($locale, $domain); |
48
|
|
|
|
49
|
|
|
return array_filter($allItems, function (MessageItem $item) { |
50
|
|
|
return !$item->isDisabled; |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
|
public function getEnabledTranslated(string $locale, string $domain): array |
59
|
|
|
{ |
60
|
|
|
$enabledItems = $this->getEnabled($locale, $domain); |
61
|
|
|
|
62
|
|
|
return array_filter($enabledItems, function (MessageItem $item) { |
63
|
|
|
return $item->hasOriginalTranslation; |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function getEnabledTranslatedJs(string $locale, string $domain): array |
71
|
|
|
{ |
72
|
|
|
$enabledTranslated = $this->getEnabledTranslated($locale, $domain); |
73
|
|
|
|
74
|
|
|
return array_filter($enabledTranslated, function (MessageItem $item) { |
75
|
|
|
return $item->isInJs; |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
|
|
public function getSingle($key): MessageItem |
83
|
|
|
{ |
84
|
|
|
foreach ($this->store as $v) { |
85
|
|
|
if ($v->key === $key) { |
86
|
|
|
return $v; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return new MessageItem(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
|
|
public function setAllAsNotInFilesAndInJs(string $locale, string $domain) |
97
|
|
|
{ |
98
|
|
|
$this->store = array_map(function (MessageItem $item) use ($locale, $domain) { |
99
|
|
|
if ($item->domain === $domain && $item->locale === $locale) { |
100
|
|
|
$item->isInFile = false; |
101
|
|
|
$item->isInJs = false; |
102
|
|
|
} |
103
|
|
|
return $item; |
104
|
|
|
}, $this->store); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritdoc |
109
|
|
|
*/ |
110
|
|
|
public function setAllAsNotDynamic(string $locale, string $domain) |
111
|
|
|
{ |
112
|
|
|
$this->store = array_map(function (MessageItem $item) use ($locale, $domain) { |
113
|
|
|
if ($item->domain === $domain && $item->locale === $locale) { |
114
|
|
|
$item->isDynamic = false; |
115
|
|
|
} |
116
|
|
|
return $item; |
117
|
|
|
}, $this->store); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
|
|
public function getRequiresTranslating(string $locale, string $domain): array |
124
|
|
|
{ |
125
|
|
|
return array_filter($this->getEnabled($locale, $domain), function (MessageItem $item) { |
126
|
|
|
return $item->requiresTranslating; |
127
|
|
|
}); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritdoc |
132
|
|
|
*/ |
133
|
|
|
public function disableUnused() |
134
|
|
|
{ |
135
|
|
|
$this->store = array_map(function (MessageItem $item) { |
136
|
|
|
$item->isDisabled = !$item->isDynamic && !$item->isInJs && !$item->isInFile; |
137
|
|
|
return $item; |
138
|
|
|
}, $this->store); |
139
|
|
|
} |
140
|
|
|
} |