Skip to content

Commit e93a686

Browse files
authored
Merge pull request #10 from saifulferoz/main
feat: add save json type method
2 parents 7b5b390 + ee66d7b commit e93a686

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

Services/Manager/ConfigManager.php

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ class ConfigManager
1717
private array $groups = [];
1818

1919
public function __construct(
20-
private ConfigRepositoryInterface $repository,
21-
private FormFactoryInterface $formFactory,
22-
private TokenStorageInterface $tokenStorage,
20+
private ConfigRepositoryInterface $repository,
21+
private FormFactoryInterface $formFactory,
22+
private TokenStorageInterface $tokenStorage,
2323
private AuthorizationCheckerInterface $checker,
24-
$configurationGroups = [])
25-
{
24+
$configurationGroups = []
25+
) {
2626
foreach ($configurationGroups as $group) {
2727
$this->groups[$group->getNameSpace()] = $group;
2828
}
2929
}
3030

31-
public function addConfigGroup(ConfigGroupInterface $group)
31+
public function addConfigGroup(ConfigGroupInterface $group): void
3232
{
3333
$this->groups[$group->getNameSpace()] = $group;
3434
}
@@ -42,7 +42,7 @@ public function getConfigurationGroups(): array
4242
$groups = [];
4343

4444
foreach ($this->groups as $key => $group) {
45-
$groups[str_replace($username . '.', '', $key)] = $group;
45+
$groups[str_replace($username.'.', '', $key)] = $group;
4646
}
4747

4848
return $groups;
@@ -77,8 +77,8 @@ public function getConfigurationsByGroup(string $groupKey): array
7777
* @var BaseConfig $configuration
7878
*/
7979
foreach ($configurations as $configuration) {
80-
$key = str_replace($username . '.', '', $configuration->getId());
81-
$key = str_replace($groupKey . '.', '', $key);
80+
$key = str_replace($username.'.', '', $configuration->getId());
81+
$key = str_replace($groupKey.'.', '', $key);
8282

8383
if (str_contains($configuration->getId(), $username)) {
8484
$results[$key] = $configuration->getValue();
@@ -102,7 +102,7 @@ public function getConfigurationValueByKey(string $key): string
102102
foreach ($configurations as $configuration) {
103103
$value = $configuration->getValue();
104104

105-
if (str_contains($configuration->getId(), $username . $key)) {
105+
if (str_contains($configuration->getId(), $username.$key)) {
106106
break;
107107
}
108108
}
@@ -176,12 +176,12 @@ public function saveUserGroupData($key, FormInterface $form)
176176
$formData = $form->getData();
177177

178178
foreach ($formData as $k => $val) {
179-
$checkBoxKey = $k . 'Preference';
179+
$checkBoxKey = $k.'Preference';
180180

181181
if (array_key_exists($checkBoxKey, $formData)) {
182182
if ($formData[$checkBoxKey]) {
183183
unset($formData[$k]);
184-
$this->repository->removeByKey($key . '.' . $k);
184+
$this->repository->removeByKey($key.'.'.$k);
185185
}
186186

187187
unset($types[$checkBoxKey]);
@@ -221,10 +221,10 @@ public function getUserConfigurationValuesByGroupKey($groupKey): array
221221

222222
if (str_contains($configuration->getId(), $username)) {
223223
$values[$key] = $configuration->getValue();
224-
$values[$key . 'Preference'] = false;
224+
$values[$key.'Preference'] = false;
225225
} elseif (!array_key_exists($key, $values)) {
226226
$values[$key] = $configuration->getValue();
227-
$values[$key . 'Preference'] = true;
227+
$values[$key.'Preference'] = true;
228228
}
229229
}
230230

@@ -248,7 +248,7 @@ public function getValueByKey(int $isGlobal, string $key): string
248248
$key = str_replace("{$username}.", '', $key);
249249

250250
if (!$isGlobal) {
251-
$key = $username . '.' . $key;
251+
$key = $username.'.'.$key;
252252
}
253253

254254
$result = $this->repository->getConfigurationValue($key);
@@ -275,19 +275,13 @@ protected function typeCast($value, $type)
275275
return null;
276276
}
277277

278-
switch ($type) {
279-
case Types::DATE_MUTABLE:
280-
case Types::DATETIME_MUTABLE:
281-
return new \DateTime($value);
282-
case Types::BOOLEAN:
283-
return (bool)$value;
284-
case Types::INTEGER:
285-
return (int)$value;
286-
case Types::JSON:
287-
return json_decode($value);
288-
default:
289-
return $value;
290-
}
278+
return match ($type) {
279+
Types::DATE_MUTABLE, Types::DATETIME_MUTABLE => new \DateTime($value),
280+
Types::BOOLEAN => (bool)$value,
281+
Types::INTEGER => (int)$value,
282+
Types::JSON => json_decode($value),
283+
default => $value,
284+
};
291285
}
292286

293287
public function getConfigurationValue($id, $type = null)
@@ -327,4 +321,37 @@ public function getGlobalAndUserConfigurationByKey(string $key): ?array
327321
{
328322
return $this->repository->getGlobalAndUserConfigurationByKey($this->getUsername(), $key);
329323
}
324+
325+
public function getRepository(): ConfigRepositoryInterface
326+
{
327+
return $this->repository;
328+
}
329+
330+
public function getConfigurationJson($id, $default = [])
331+
{
332+
$data = $this->repository->find($id);
333+
334+
$data = json_decode($data->getValue(), true);
335+
336+
if (json_last_error()) {
337+
return $default;
338+
}
339+
340+
return array_merge_recursive($default, $data);
341+
}
342+
343+
public function saveConfigurationJson($id, array $data)
344+
{
345+
if (empty($data)) {
346+
return null;
347+
}
348+
349+
$data = json_encode($data);
350+
351+
if (json_last_error()) {
352+
return null;
353+
}
354+
355+
return $this->repository->save($id, $data, 'json');
356+
}
330357
}

Services/Repository/ConfigRepository.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\ORM\EntityManagerInterface;
66
use Xiidea\EasyConfigBundle\Exception\UnrecognizedEntityException;
77
use Xiidea\EasyConfigBundle\Model\BaseConfig;
8+
use Xiidea\EasyConfigBundle\Model\BaseConfigInterface;
89

910
class ConfigRepository implements ConfigRepositoryInterface
1011
{

0 commit comments

Comments
 (0)