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
|
|
|
namespace Sylius\Bundle\CoreBundle\Form\DataTransformer; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
15
|
|
|
use Webmozart\Assert\Assert; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class ChannelAndCurrencyPricingConfigurationTransformer implements DataTransformerInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $delimiter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $delimiter |
29
|
|
|
*/ |
30
|
|
|
public function __construct($delimiter) |
31
|
|
|
{ |
32
|
|
|
$this->delimiter = $delimiter; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function transform($configuration) |
39
|
|
|
{ |
40
|
|
|
if (empty($configuration)) { |
41
|
|
|
return []; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
Assert::isArray($configuration); |
45
|
|
|
|
46
|
|
|
$flatConfiguration = []; |
47
|
|
|
array_walk($configuration, function ($value, $channelCode) use (&$flatConfiguration) { |
48
|
|
|
foreach ($value as $currencyCode => $price) { |
49
|
|
|
$flatConfiguration[sprintf('%s%s%s', $channelCode, $this->delimiter, $currencyCode)] = $price; |
50
|
|
|
} |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
return $flatConfiguration; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
|
public function reverseTransform($flatConfiguration) |
60
|
|
|
{ |
61
|
|
|
Assert::isArray($flatConfiguration); |
62
|
|
|
|
63
|
|
|
$configuration = []; |
64
|
|
|
foreach ($flatConfiguration as $key => $value) { |
65
|
|
|
list($channelCode, $currencyCode) = explode($this->delimiter, $key); |
66
|
|
|
$configuration[$channelCode][$currencyCode] = $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $configuration; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|