forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongo.php
218 lines (185 loc) · 5.73 KB
/
Mongo.php
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Gorka Guridi <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Translate\Adapter;
use Phalcon\Translate\Exception;
use Phalcon\Mvc\CollectionInterface;
use Phalcon\Translate\AdapterInterface;
/**
* Phalcon\Translate\Adapter\Mongo
*
* Implements a mongo adapter for translations.
*
* A generic collection with a source to store the translations must be created
* and passed as a parameter.
*
* @package Phalcon\Translate\Adapter
*/
class Mongo extends Base implements AdapterInterface, \ArrayAccess
{
protected $language;
protected $collection;
protected $formatter;
/**
* Mongo constructor.
*
* @param array $options
*
* @throws Exception
*/
public function __construct($options)
{
if (!isset($options['collection'])) {
throw new Exception("Parameter 'collection' is required");
}
$this->setCollection($options['collection']);
if (!isset($options['language'])) {
throw new Exception("Parameter 'language' is required");
}
$this->setLanguage($options['language']);
if (isset($options['formatter'])) {
$this->setFormatter($options['formatter']);
}
}
/**
* Sets the collection object.
*
* @param CollectionInterface|string $collection Translations collection class to use.
* Can be an instance of CollectionInterface or a string.
*/
protected function setCollection($collection)
{
$this->collection = $collection;
}
/**
* Sets the language to use.
*
* @param string $language
*/
protected function setLanguage($language)
{
$this->language = $language;
}
/**
* Sets the formatter to use if necessary.
*
* @param \MessageFormatter $formatter Message formatter.
*/
protected function setFormatter(\MessageFormatter $formatter)
{
$this->formatter = $formatter;
}
/**
* Gets the translations set.
*
* @param string $translateKey
* Key of the collection set.
*
* @return mixed
*/
protected function getTranslations($translateKey)
{
/** @var CollectionInterface $collection */
$collection = $this->collection;
return $collection::findFirst([['key' => $translateKey]]);
}
/**
* {@inheritDoc}
*/
public function _($translateKey, $placeholders = null)
{
return $this->query($translateKey, $placeholders);
}
/**
* {@inheritDoc}
*/
public function query($translateKey, $placeholders = null)
{
$translations = $this->getTranslations($translateKey);
$translation = $translateKey;
if (isset($translations->{$this->language})) {
$translation = $translations->{$this->language};
}
if (!empty($placeholders)) {
return $this->format($translation, $placeholders);
}
return $translation;
}
/**
* Formats a translation.
*
* @param string $translation Translated text.
* @param array $placeholders Placeholders to apply.
*
* @return string
*/
protected function format($translation, $placeholders = [])
{
if ($this->formatter) {
$formatter = $this->formatter;
return $formatter::formatMessage($this->language, $translation, $placeholders);
}
foreach ($placeholders as $key => $value) {
$translation = str_replace("%$key%", $value, $translation);
}
return $translation;
}
/**
* {@inheritDoc}
*/
public function exists($translateKey)
{
$translations = $this->getTranslations($translateKey);
return isset($translations->{$this->language});
}
/**
* {@inheritDoc}
*/
public function offsetExists($translateKey)
{
return $this->exists($translateKey);
}
/**
* {@inheritDoc}
*/
public function offsetSet($translateKey, $message)
{
$translations = $this->getTranslations($translateKey);
$translations->{$this->language} = $message;
return $translations->save();
}
/**
* {@inheritDoc}
*/
public function offsetGet($translateKey)
{
$translations = $this->getTranslations($translateKey);
if (isset($translations->{$this->language})) {
return $translations->{$this->language};
}
return '';
}
/**
* {@inheritDoc}
*/
public function offsetUnset($translateKey)
{
$translations = $this->getTranslations($translateKey);
unset($translations->{$this->language});
return $translations->save();
}
}