1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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 Sylius\Bundle\CoreBundle\Form\EventSubscriber; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
17
|
|
|
use Symfony\Component\Form\FormEvent; |
18
|
|
|
use Symfony\Component\Form\FormEvents; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Grzegorz Sadowski <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class ChannelFormSubscriber implements EventSubscriberInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public static function getSubscribedEvents(): array |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
FormEvents::PRE_SUBMIT => 'preSubmit', |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param FormEvent $event |
37
|
|
|
*/ |
38
|
|
|
public function preSubmit(FormEvent $event): void |
39
|
|
|
{ |
40
|
|
|
$data = $event->getData(); |
41
|
|
|
|
42
|
|
|
if (empty($data) || empty($data['defaultLocale']) || empty($data['baseCurrency'])) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$data['locales'] = $this->resolveLocales( |
47
|
|
|
$data['locales'] ?? [], |
48
|
|
|
$data['defaultLocale']) |
49
|
|
|
; |
50
|
|
|
|
51
|
|
|
$data['currencies'] = $this->resolveCurrencies( |
52
|
|
|
$data['currencies'] ?? [], |
53
|
|
|
$data['baseCurrency']) |
54
|
|
|
; |
55
|
|
|
|
56
|
|
|
$event->setData($data); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array|string[] $locales |
61
|
|
|
* @param string $defaultLocale |
62
|
|
|
* |
63
|
|
|
* @return array|string[] |
64
|
|
|
*/ |
65
|
|
|
private function resolveLocales(array $locales, string $defaultLocale): array |
66
|
|
|
{ |
67
|
|
|
if (empty($locales)) { |
68
|
|
|
return [$defaultLocale]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!in_array($defaultLocale, $locales)) { |
72
|
|
|
$locales[] = $defaultLocale; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $locales; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array|string[] $currencies |
80
|
|
|
* @param string $baseCurrency |
81
|
|
|
* |
82
|
|
|
* @return array|string[] |
83
|
|
|
*/ |
84
|
|
|
private function resolveCurrencies(array $currencies, string $baseCurrency): array |
85
|
|
|
{ |
86
|
|
|
if (empty($currencies)) { |
87
|
|
|
return [$baseCurrency]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!in_array($baseCurrency, $currencies)) { |
91
|
|
|
$currencies[] = $baseCurrency; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $currencies; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|