|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TreeHouse\Feeder\Modifier\Item\Transformer; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
6
|
|
|
use TreeHouse\Feeder\Exception\UnexpectedTypeException; |
|
7
|
|
|
|
|
8
|
|
|
class ExpandAttributesTransformer implements TransformerInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $field; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var bool |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $removeOriginal; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $overwriteExisting; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $field Expand attributes in this field, if omitted, all root-level attributes are |
|
27
|
|
|
* expanded |
|
28
|
|
|
* @param bool $removeOriginal Whether to remove the original attribute |
|
29
|
|
|
* @param array $overwriteExisting Keys that may be overwritten when they already exist |
|
30
|
|
|
* |
|
31
|
|
|
* @throws UnexpectedTypeException |
|
32
|
|
|
*/ |
|
33
|
20 |
|
public function __construct($field = null, $removeOriginal = false, array $overwriteExisting = []) |
|
34
|
|
|
{ |
|
35
|
20 |
|
if (!is_string($field) && !is_null($field)) { |
|
36
|
2 |
|
throw new UnexpectedTypeException($field, 'string or null'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
18 |
|
$this->field = $field; |
|
40
|
18 |
|
$this->removeOriginal = $removeOriginal; |
|
41
|
18 |
|
$this->overwriteExisting = $overwriteExisting; |
|
42
|
18 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritdoc |
|
46
|
|
|
*/ |
|
47
|
18 |
|
public function transform(ParameterBag $item) |
|
48
|
|
|
{ |
|
49
|
18 |
|
if (!$this->field) { |
|
50
|
|
|
// expand root-level attributes |
|
51
|
8 |
|
$item->replace($this->expand($item->all())); |
|
52
|
|
|
|
|
53
|
8 |
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// proceed only when the field exists and is an array |
|
57
|
10 |
|
$value = $item->get($this->field); |
|
58
|
10 |
|
if (!is_array($value)) { |
|
59
|
2 |
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
8 |
|
$item->set($this->field, $this->expand($value)); |
|
63
|
8 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param array $value |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
16 |
|
protected function expand(array $value) |
|
71
|
|
|
{ |
|
72
|
16 |
|
foreach ($value as $name => $val) { |
|
73
|
|
|
// attributes are converted to @attribute |
|
74
|
16 |
|
if (substr($name, 0, 1) === '@') { |
|
75
|
16 |
|
if ($this->removeOriginal) { |
|
76
|
12 |
|
unset($value[$name]); |
|
77
|
12 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
// the new name |
|
80
|
16 |
|
$name = ltrim($name, '@'); |
|
81
|
|
|
|
|
82
|
|
|
// if key already exists, check if we may overwrite it |
|
83
|
16 |
|
if (array_key_exists($name, $value) && !in_array($name, $this->overwriteExisting)) { |
|
84
|
4 |
|
continue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
12 |
|
$value[$name] = $val; |
|
88
|
12 |
|
} |
|
89
|
16 |
|
} |
|
90
|
|
|
|
|
91
|
16 |
|
return $value; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|