1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
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 Translation\Bundle\Catalogue\Operation; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\Catalogue\AbstractOperation; |
15
|
|
|
use Symfony\Component\Translation\MetadataAwareInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This will merge and replace all values in $target with values from $source. |
19
|
|
|
* It is the equivalent of running array_merge($target, $source). When in conflict, |
20
|
|
|
* always take values from $source. |
21
|
|
|
* |
22
|
|
|
* This operation is metadata aware. It will do the same recursive merge on metadata. |
23
|
|
|
* |
24
|
|
|
* all = source ∪ target = {x: x ∈ source ∨ x ∈ target} |
25
|
|
|
* new = all ∖ target = {x: x ∈ source ∧ x ∉ target} |
26
|
|
|
* obsolete = target ∖ all = {x: x ∈ target ∧ x ∉ source} |
27
|
|
|
* |
28
|
|
|
* @author Tobias Nyholm <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class ReplaceOperation extends AbstractOperation |
31
|
|
|
{ |
32
|
5 |
|
protected function processDomain($domain) |
33
|
|
|
{ |
34
|
5 |
|
$this->messages[$domain] = [ |
35
|
|
|
'all' => [], |
36
|
|
|
'new' => [], |
37
|
|
|
'obsolete' => [], |
38
|
|
|
]; |
39
|
5 |
|
$sourceMessages = $this->source->all($domain); |
40
|
|
|
|
41
|
5 |
|
foreach ($this->target->all($domain) as $id => $message) { |
42
|
4 |
|
$this->messages[$domain]['all'][$id] = $message; |
43
|
|
|
|
44
|
|
|
// If $id is NOT defined in source. |
45
|
4 |
|
if (!array_key_exists($id, $sourceMessages)) { |
46
|
4 |
|
$this->messages[$domain]['obsolete'][$id] = $message; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
foreach ($sourceMessages as $id => $message) { |
51
|
4 |
|
if (!empty($message)) { |
52
|
4 |
|
$this->messages[$domain]['all'][$id] = $message; |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
if (!$this->target->has($id, $domain)) { |
56
|
4 |
|
$this->messages[$domain]['new'][$id] = $message; |
57
|
|
|
|
58
|
|
|
// Make sure to add it to the source if even if empty($message) |
59
|
4 |
|
$this->messages[$domain]['all'][$id] = $message; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
5 |
|
$this->result->add($this->messages[$domain]['all'], $domain); |
64
|
|
|
|
65
|
5 |
|
$targetMetadata = $this->target instanceof MetadataAwareInterface ? $this->target->getMetadata('', $domain) : []; |
66
|
5 |
|
$sourceMetadata = $this->source instanceof MetadataAwareInterface ? $this->source->getMetadata('', $domain) : []; |
67
|
5 |
|
$resultMetadata = $this->mergeMetaData($sourceMetadata, $targetMetadata); |
68
|
|
|
|
69
|
|
|
// Write back metadata |
70
|
5 |
|
foreach ($resultMetadata as $id => $data) { |
|
|
|
|
71
|
2 |
|
$this->result->setMetadata($id, $data, $domain); |
72
|
|
|
} |
73
|
5 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array|null $source |
77
|
|
|
* @param array|null $target |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
5 |
|
private function mergeMetadata($source, $target) |
82
|
|
|
{ |
83
|
5 |
|
if (empty($source) && empty($target)) { |
84
|
3 |
|
return []; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
if (empty($source)) { |
88
|
|
|
return $target; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
if (empty($target)) { |
92
|
|
|
return $source; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
if (!is_array($source) || !is_array($target)) { |
96
|
|
|
return $source; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
$result = $this->doMergeMetadata($source, $target); |
100
|
|
|
|
101
|
2 |
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
private function doMergeMetadata(array $source, array $target) |
105
|
|
|
{ |
106
|
2 |
|
$isTargetArrayAssociative = $this->isArrayAssociative($target); |
107
|
2 |
|
foreach ($target as $key => $value) { |
108
|
2 |
|
if ($isTargetArrayAssociative) { |
109
|
2 |
|
if (isset($source[$key]) && $source[$key] !== $value) { |
110
|
2 |
|
if (is_array($source[$key]) && is_array($value)) { |
111
|
|
|
// If both arrays, do recursive call |
112
|
2 |
|
$source[$key] = $this->doMergeMetadata($source[$key], $value); |
113
|
|
|
} |
114
|
|
|
// Else, use value form $source |
115
|
|
|
} else { |
116
|
|
|
// Add new value |
117
|
2 |
|
$source[$key] = $value; |
118
|
|
|
} |
119
|
|
|
// if sequential |
120
|
1 |
|
} elseif (!in_array($value, $source)) { |
121
|
2 |
|
$source[] = $value; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
return $source; |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
public function isArrayAssociative(array $arr) |
129
|
|
|
{ |
130
|
2 |
|
if ([] === $arr) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
return array_keys($arr) !== range(0, count($arr) - 1); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.