Skip to content

Commit fee2498

Browse files
committed
Fixed mergeWith flags #36
1 parent ca67814 commit fee2498

2 files changed

Lines changed: 42 additions & 7 deletions

File tree

src/Translations.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ public function mergeWith(Translations $translations, $method = null)
211211
}
212212
}
213213

214-
$add = (boolean) $method & self::MERGE_ADD;
215-
$references = (boolean) $method & self::MERGE_REFERENCES;
216-
$comments = (boolean) $method & self::MERGE_COMMENTS;
214+
$add = (boolean) ($method & self::MERGE_ADD);
215+
$references = (boolean) $method & (self::MERGE_REFERENCES);
216+
$comments = (boolean) ($method & self::MERGE_COMMENTS);
217217

218218
foreach ($translations as $entry) {
219219
if (($existing = $this->find($entry))) {
@@ -224,13 +224,15 @@ public function mergeWith(Translations $translations, $method = null)
224224
}
225225

226226
if ($method & self::MERGE_REMOVE) {
227-
$iterator = $this->getIterator();
227+
$filtered = array();
228228

229-
foreach ($iterator as $k => $entry) {
230-
if (!($existing = $translations->find($entry))) {
231-
$iterator->offsetUnset($k);
229+
foreach ($this as $entry) {
230+
if ($translations->find($entry)) {
231+
$filtered[] = $entry;
232232
}
233233
}
234+
235+
$this->exchangeArray($filtered);
234236
}
235237
}
236238
}

tests/TranslationsTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,37 @@ public function testGettersSetters()
5656
$translations->setHeader('POT-Creation-Date', '2012-08-07 13:03+0100');
5757
$this->assertEquals('2012-08-07 13:03+0100', $translations->getHeader('POT-Creation-Date'));
5858
}
59+
60+
public function testMergeDefault()
61+
{
62+
$translations1 = Gettext\Extractors\Po::fromFile(__DIR__.'/files/po.po');
63+
$translations2 = Gettext\Extractors\Po::fromFile(__DIR__.'/files/plurals.po');
64+
65+
$this->assertCount(9, $translations1);
66+
$this->assertCount(3, $translations2);
67+
68+
$translations1->mergeWith($translations2);
69+
70+
$this->assertCount(12, $translations1);
71+
}
72+
73+
public function testMergeAddRemove()
74+
{
75+
$translations1 = Gettext\Extractors\Po::fromFile(__DIR__.'/files/po.po');
76+
$translations2 = Gettext\Extractors\Po::fromFile(__DIR__.'/files/plurals.po');
77+
78+
$translations1->mergeWith($translations2, Gettext\Translations::MERGE_REMOVE | Gettext\Translations::MERGE_ADD);
79+
80+
$this->assertCount(3, $translations1);
81+
}
82+
83+
public function testMergeRemove()
84+
{
85+
$translations1 = Gettext\Extractors\Po::fromFile(__DIR__.'/files/po.po');
86+
$translations2 = Gettext\Extractors\Po::fromFile(__DIR__.'/files/plurals.po');
87+
88+
$translations1->mergeWith($translations2, Gettext\Translations::MERGE_REMOVE);
89+
90+
$this->assertCount(0, $translations1);
91+
}
5992
}

0 commit comments

Comments
 (0)