|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TreeHouse\Feeder\Modifier\Item\Transformer; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
6
|
|
|
|
|
7
|
|
|
class ExpandNodeTransformer implements TransformerInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $field; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var bool |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $removeOriginal; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $overwriteExisting; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $field Expand nodes in this field |
|
26
|
|
|
* @param bool $removeOriginal Whether to remove the original node |
|
27
|
|
|
* @param array $overwriteExisting Keys that may be overwritten when they already exist |
|
28
|
|
|
*/ |
|
29
|
8 |
|
public function __construct($field, $removeOriginal = false, array $overwriteExisting = []) |
|
30
|
|
|
{ |
|
31
|
8 |
|
$this->field = $field; |
|
32
|
8 |
|
$this->removeOriginal = $removeOriginal; |
|
33
|
8 |
|
$this->overwriteExisting = $overwriteExisting; |
|
34
|
8 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
*/ |
|
39
|
8 |
|
public function transform(ParameterBag $item) |
|
40
|
|
|
{ |
|
41
|
8 |
|
if ($item->has($this->field)) { |
|
42
|
8 |
|
$value = $item->get($this->field); |
|
43
|
|
|
|
|
44
|
|
|
// check if the field is an array |
|
45
|
8 |
|
if (is_array($value)) { |
|
46
|
8 |
|
$this->expand($value, $item); |
|
47
|
8 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
// remove the compound field if requested |
|
50
|
8 |
|
if ($this->removeOriginal) { |
|
51
|
6 |
|
$item->remove($this->field); |
|
52
|
6 |
|
} |
|
53
|
8 |
|
} |
|
54
|
8 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array $value |
|
58
|
|
|
* @param ParameterBag $item |
|
59
|
|
|
*/ |
|
60
|
8 |
|
protected function expand(array $value, ParameterBag $item) |
|
61
|
|
|
{ |
|
62
|
8 |
|
foreach ($value as $key => $val) { |
|
63
|
|
|
// if key already exists, check if we may overwrite it |
|
64
|
8 |
|
if ($item->has($key)) { |
|
65
|
4 |
|
if (!in_array($key, $this->overwriteExisting)) { |
|
66
|
2 |
|
continue; |
|
67
|
|
|
} |
|
68
|
2 |
|
} |
|
69
|
|
|
|
|
70
|
8 |
|
$item->set($key, $val); |
|
71
|
8 |
|
} |
|
72
|
8 |
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|