|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the SexyField package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dion Snoeijen <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare (strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tardigrades\SectionField\Service; |
|
15
|
|
|
|
|
16
|
|
|
use Tardigrades\Entity\Language; |
|
17
|
|
|
use Tardigrades\Entity\LanguageInterface; |
|
18
|
|
|
use Tardigrades\SectionField\ValueObject\I18n; |
|
19
|
|
|
use Tardigrades\SectionField\ValueObject\Id; |
|
20
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
21
|
|
|
use Tardigrades\SectionField\ValueObject\LanguageConfig; |
|
22
|
|
|
|
|
23
|
|
|
class DoctrineLanguageManager implements LanguageManagerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var EntityManagerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $entityManager; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
EntityManagerInterface $entityManager |
|
32
|
|
|
) { |
|
33
|
|
|
$this->entityManager = $entityManager; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function create(LanguageInterface $entity): LanguageInterface |
|
37
|
|
|
{ |
|
38
|
|
|
$this->entityManager->persist($entity); |
|
39
|
|
|
$this->entityManager->flush(); |
|
40
|
|
|
|
|
41
|
|
|
return $entity; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function read(Id $id): LanguageInterface |
|
45
|
|
|
{ |
|
46
|
|
|
$languageRepository = $this->entityManager->getRepository(Language::class); |
|
47
|
|
|
|
|
48
|
|
|
/** @var $language Language */ |
|
49
|
|
|
$language = $languageRepository->find($id->toInt()); |
|
50
|
|
|
|
|
51
|
|
|
if (empty($language)) { |
|
52
|
|
|
throw new LanguageNotFoundException(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $language; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function readAll(): array |
|
59
|
|
|
{ |
|
60
|
|
|
$languageRepository = $this->entityManager->getRepository(Language::class); |
|
61
|
|
|
$languages = $languageRepository->findAll(); |
|
62
|
|
|
|
|
63
|
|
|
if (empty($languages)) { |
|
64
|
|
|
throw new LanguageNotFoundException(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $languages; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function update(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->entityManager->flush(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function delete(LanguageInterface $entity): void |
|
76
|
|
|
{ |
|
77
|
|
|
$this->entityManager->remove($entity); |
|
78
|
|
|
$this->entityManager->flush(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function readByI18n(I18n $i18n): LanguageInterface |
|
82
|
|
|
{ |
|
83
|
|
|
$languageRepository = $this->entityManager->getRepository(Language::class); |
|
84
|
|
|
|
|
85
|
|
|
/** @var Language $language */ |
|
86
|
|
|
$language = $languageRepository->findOneBy([ |
|
87
|
|
|
'i18n' => (string) $i18n |
|
88
|
|
|
]); |
|
89
|
|
|
|
|
90
|
|
|
if (empty($language)) { |
|
91
|
|
|
throw new LanguageNotFoundException(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $language; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function readByI18ns(array $i18ns): array |
|
98
|
|
|
{ |
|
99
|
|
|
$in = []; |
|
100
|
|
|
foreach ($i18ns as $i18n) { |
|
101
|
|
|
$in[] = '\'' . $i18n . '\''; |
|
102
|
|
|
} |
|
103
|
|
|
$whereIn = implode(',', $in); |
|
104
|
|
|
$query = $this->entityManager->createQuery( |
|
105
|
|
|
"SELECT language FROM Tardigrades\Entity\Language language WHERE language.i18n IN ({$whereIn})" |
|
106
|
|
|
); |
|
107
|
|
|
$results = $query->getResult(); |
|
108
|
|
|
|
|
109
|
|
|
if (empty($results)) { |
|
110
|
|
|
throw new LanguageNotFoundException(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$finalResults = []; |
|
114
|
|
|
/** @var Language $result */ |
|
115
|
|
|
foreach ($results as $result) { |
|
116
|
|
|
$finalResults[(string) $result->getI18n()] = $result; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $finalResults; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function createByConfig(LanguageConfig $languageConfig): LanguageManagerInterface |
|
123
|
|
|
{ |
|
124
|
|
|
$this->setUpByConfig($languageConfig); |
|
125
|
|
|
$this->entityManager->flush(); |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function updateByConfig(LanguageConfig $languageConfig): LanguageManagerInterface |
|
131
|
|
|
{ |
|
132
|
|
|
$this->setUpByConfig($languageConfig); |
|
133
|
|
|
$this->entityManager->flush(); |
|
134
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function setUpByConfig(LanguageConfig $languageConfig): void |
|
139
|
|
|
{ |
|
140
|
|
|
$languageConfig = $languageConfig->toArray(); |
|
141
|
|
|
|
|
142
|
|
|
try { |
|
143
|
|
|
$existing = $this->readByI18ns($languageConfig['language']); |
|
144
|
|
|
} catch (LanguageNotFoundException $exception) { |
|
145
|
|
|
$existing = []; |
|
146
|
|
|
} |
|
147
|
|
|
$existingCheck = []; |
|
148
|
|
|
/** @var Language $language */ |
|
149
|
|
|
foreach ($existing as $language) { |
|
150
|
|
|
$existingCheck[] = (string) $language->getI18n(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
foreach ($languageConfig['language'] as $configLanguage) { |
|
154
|
|
|
if (!in_array($configLanguage, $existingCheck)) { |
|
155
|
|
|
$this->entityManager->persist((new Language())->setI18n($configLanguage)); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|