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
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Tobias Nyholm <[email protected]> |
18
|
|
|
* @author Jean-François Simon <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class MetadataAwareMerge extends AbstractOperation |
21
|
|
|
{ |
22
|
|
|
protected function processDomain($domain) |
23
|
|
|
{ |
24
|
|
|
$this->messages[$domain] = [ |
25
|
|
|
'all' => [], |
26
|
|
|
'new' => [], |
27
|
|
|
'obsolete' => [], |
28
|
|
|
]; |
29
|
|
|
$targetMessages = $this->target->all($domain); |
30
|
|
|
|
31
|
|
|
foreach ($this->source->all($domain) as $id => $message) { |
32
|
|
|
$this->messages[$domain]['all'][$id] = $message; |
33
|
|
|
$this->result->add([$id => $message], $domain); |
34
|
|
|
if (empty($message)) { |
35
|
|
|
$this->messages[$domain]['new'][$id] = $message; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// If $id is NOT defined in target. |
39
|
|
|
if (!array_key_exists($id, $targetMessages)) { |
40
|
|
|
$this->messages[$domain]['obsolete'][$id] = $message; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (null !== $keySourceMetadata = $this->source->getMetadata($id, $domain)) { |
44
|
|
|
$this->result->setMetadata($id, $keySourceMetadata, $domain); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Get metadata from target |
48
|
|
|
if (null !== $keyTargetMetadata = $this->target->getMetadata($id, $domain)) { |
49
|
|
|
if (null === $keySourceMetadata) { |
50
|
|
|
// If there were no metadata in source. Just use target's metadata |
51
|
|
|
$this->result->setMetadata($id, $keyTargetMetadata, $domain); |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Merge metadata |
56
|
|
|
$resultMetadata = $this->mergeMetaData($keySourceMetadata, $keyTargetMetadata); |
57
|
|
|
$this->result->setMetadata($id, $resultMetadata, $domain); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($targetMessages as $id => $message) { |
62
|
|
|
if (!$this->source->has($id, $domain)) { |
63
|
|
|
$this->messages[$domain]['all'][$id] = $message; |
64
|
|
|
$this->messages[$domain]['new'][$id] = $message; |
65
|
|
|
|
66
|
|
|
$this->result->add([$id => $message], $domain); |
67
|
|
|
if (null !== $keyMetadata = $this->target->getMetadata($id, $domain)) { |
68
|
|
|
$this->result->setMetadata($id, $keyMetadata, $domain); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $source |
76
|
|
|
* @param array $target |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
private function mergeMetadata(array $source, array $target) |
81
|
|
|
{ |
82
|
|
|
$toRemove = ['status:new', 'status:obsolete', 'file-source']; |
83
|
|
|
$resultNotes = []; |
84
|
|
|
|
85
|
|
|
// Remove some old notes |
86
|
|
|
if (isset($source['notes'])) { |
87
|
|
|
foreach ($source['notes'] as $note) { |
88
|
|
|
if (isset($note['content']) && in_array($note['content'], $toRemove)) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
$resultNotes[] = $note; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (isset($target['notes'])) { |
96
|
|
|
foreach ($target['notes'] as $note) { |
97
|
|
|
$resultNotes[] = $note; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$result = $source; |
102
|
|
|
$result['notes'] = $resultNotes; |
103
|
|
|
|
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|