1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\Language; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Oc\Repository\Exception\RecordAlreadyExistsException; |
7
|
|
|
use Oc\Repository\Exception\RecordNotPersistedException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class LanguageRepository |
11
|
|
|
* |
12
|
|
|
* @package Oc\Language |
13
|
|
|
*/ |
14
|
|
|
class LanguageRepository |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Database table name that this repository maintains. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
const TABLE = 'languages'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Connection |
25
|
|
|
*/ |
26
|
|
|
private $connection; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* LanguageRepository constructor. |
30
|
|
|
* |
31
|
|
|
* @param Connection $connection |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Connection $connection) |
34
|
|
|
{ |
35
|
|
|
$this->connection = $connection; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Fetches all languages. |
40
|
|
|
* |
41
|
|
|
* @return LanguageEntity[] |
42
|
|
|
*/ |
43
|
|
|
public function fetchAll() |
44
|
|
|
{ |
45
|
|
|
$statement = $this->connection->createQueryBuilder() |
46
|
|
|
->select('*') |
47
|
|
|
->from(self::TABLE) |
48
|
|
|
->execute(); |
49
|
|
|
|
50
|
|
|
$result = $statement->fetchAll(); |
51
|
|
|
|
52
|
|
|
if ($result === false) { |
53
|
|
|
return []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->getEntityArrayFromDatabaseArray($result); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Fetches all translated languages. |
61
|
|
|
* |
62
|
|
|
* @return LanguageEntity[] |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function fetchAllTranslated() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$statement = $this->connection->createQueryBuilder() |
67
|
|
|
->select('*') |
68
|
|
|
->from(self::TABLE) |
69
|
|
|
->where('is_translated = 1') |
70
|
|
|
->execute(); |
71
|
|
|
|
72
|
|
|
$result = $statement->fetchAll(); |
73
|
|
|
|
74
|
|
|
if ($result === false) { |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->getEntityArrayFromDatabaseArray($result); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Creates a language in the database. |
83
|
|
|
* |
84
|
|
|
* @param LanguageEntity $entity |
85
|
|
|
* |
86
|
|
|
* @return LanguageEntity |
87
|
|
|
* |
88
|
|
|
* @throws RecordAlreadyExistsException |
89
|
|
|
*/ |
90
|
|
|
public function create(LanguageEntity $entity) |
91
|
|
|
{ |
92
|
|
|
if (!$entity->isNew()) { |
93
|
|
|
throw new RecordAlreadyExistsException('The entity does already exist.'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$databaseArray = $this->getDatabaseArrayFromEntity($entity); |
97
|
|
|
|
98
|
|
|
$this->connection->insert( |
99
|
|
|
self::TABLE, |
100
|
|
|
$databaseArray |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$entity->short = $this->connection->lastInsertId(); |
104
|
|
|
|
105
|
|
|
return $entity; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Update a language in the database. |
110
|
|
|
* |
111
|
|
|
* @param LanguageEntity $entity |
112
|
|
|
* |
113
|
|
|
* @return LanguageEntity |
114
|
|
|
* |
115
|
|
|
* @throws RecordNotPersistedException |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function update(LanguageEntity $entity) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
if ($entity->isNew()) { |
120
|
|
|
throw new RecordNotPersistedException('The entity does not exist.'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$databaseArray = $this->getDatabaseArrayFromEntity($entity); |
124
|
|
|
|
125
|
|
|
$this->connection->update( |
126
|
|
|
self::TABLE, |
127
|
|
|
$databaseArray, |
128
|
|
|
['short' => $entity->short] |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$entity->short = $this->connection->lastInsertId(); |
132
|
|
|
|
133
|
|
|
return $entity; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Removes a language from the database. |
138
|
|
|
* |
139
|
|
|
* @param LanguageEntity $entity |
140
|
|
|
* |
141
|
|
|
* @return LanguageEntity |
142
|
|
|
* |
143
|
|
|
* @throws RecordNotPersistedException |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function remove(LanguageEntity $entity) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
if ($entity->isNew()) { |
148
|
|
|
throw new RecordNotPersistedException('The entity does not exist.'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$databaseArray = $this->getDatabaseArrayFromEntity($entity); |
152
|
|
|
|
153
|
|
|
$this->connection->delete( |
154
|
|
|
self::TABLE, |
155
|
|
|
$databaseArray, |
156
|
|
|
['short' => $entity->short] |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$entity->short = null; |
160
|
|
|
|
161
|
|
|
return $entity; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Converts database array to entity array. |
166
|
|
|
* |
167
|
|
|
* @param array $result |
168
|
|
|
* |
169
|
|
|
* @return LanguageEntity[] |
170
|
|
|
*/ |
171
|
|
|
private function getEntityArrayFromDatabaseArray(array $result) |
172
|
|
|
{ |
173
|
|
|
$languages = []; |
174
|
|
|
|
175
|
|
|
foreach ($result as $item) { |
176
|
|
|
$languages[] = $this->getEntityFromDatabaseArray($item); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $languages; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Maps the given entity to the database array. |
184
|
|
|
* |
185
|
|
|
* @param LanguageEntity $entity |
186
|
|
|
* |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
View Code Duplication |
public function getDatabaseArrayFromEntity(LanguageEntity $entity) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
return [ |
192
|
|
|
'short' => $entity->short, |
193
|
|
|
'name' => $entity->name, |
194
|
|
|
'native_name' => $entity->nativeName, |
195
|
|
|
'de' => $entity->de, |
196
|
|
|
'en' => $entity->en, |
197
|
|
|
'trans_id' => $entity->translationId, |
198
|
|
|
'list_default_de' => $entity->listDefaultDe, |
199
|
|
|
'list_default_en' => $entity->listDefaultEn, |
200
|
|
|
'is_translated' => $entity->isTranslated |
201
|
|
|
]; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Prepares database array from properties. |
206
|
|
|
* |
207
|
|
|
* @param array $data |
208
|
|
|
* |
209
|
|
|
* @return LanguageEntity |
210
|
|
|
*/ |
211
|
|
View Code Duplication |
public function getEntityFromDatabaseArray(array $data) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$entity = new LanguageEntity(); |
214
|
|
|
$entity->short = (string) strtolower($data['short']); |
215
|
|
|
$entity->name = (string) $data['name']; |
216
|
|
|
$entity->nativeName = (string) $data['native_name']; |
217
|
|
|
$entity->de = (string) $data['de']; |
218
|
|
|
$entity->en = (string) $data['en']; |
219
|
|
|
$entity->translationId = (int) $data['trans_id']; |
220
|
|
|
$entity->listDefaultDe = (bool) $data['list_default_de']; |
221
|
|
|
$entity->listDefaultEn = (bool) $data['list_default_en']; |
222
|
|
|
$entity->isTranslated = (bool) $data['is_translated']; |
223
|
|
|
|
224
|
|
|
return $entity; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.