Skip to content

Commit 35d2db4

Browse files
committed
made original and context inmutable
1 parent e3e4c1c commit 35d2db4

6 files changed

Lines changed: 77 additions & 52 deletions

File tree

src/Extractors/Blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class Blade extends Extractor implements ExtractorInterface
1515
*/
1616
public static function fromString($string, Translations $translations = null, $file = '')
1717
{
18-
$string = (new BladeCompiler(new Filesystem(), null))->compileString($string);
18+
$bladeCompiler = new BladeCompiler(new Filesystem(), null);
19+
$string = $bladeCompiler->compileString($string);
1920

2021
return PhpCode::fromString($string, $translations, $file);
2122
}

src/Extractors/Po.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function fromString($string, Translations $translations = null, $f
3737
}
3838
}
3939

40-
$translation = new Translation();
40+
$translation = new Translation('', '');
4141

4242
for ($n = count($lines); $i < $n; $i++) {
4343
$line = trim($lines[$i]);
@@ -47,15 +47,14 @@ public static function fromString($string, Translations $translations = null, $f
4747
if ($line === '') {
4848
if ($translation->hasOriginal()) {
4949
$translations[] = $translation;
50-
$translation = new Translation();
50+
$translation = new Translation('', '');
5151
}
5252
continue;
5353
}
5454

5555
$splitLine = preg_split('/\s+/', $line, 2);
5656
$key = $splitLine[0];
5757
$data = isset($splitLine[1]) ? $splitLine[1] : '';
58-
$append = null;
5958

6059
switch ($key) {
6160
case '#':
@@ -85,12 +84,12 @@ public static function fromString($string, Translations $translations = null, $f
8584
break;
8685

8786
case 'msgctxt':
88-
$translation->setContext(self::clean($data));
87+
$translation = $translation->getClone(self::clean($data));
8988
$append = 'Context';
9089
break;
9190

9291
case 'msgid':
93-
$translation->setOriginal(self::clean($data));
92+
$translation = $translation->getClone(null, self::clean($data));
9493
$append = 'Original';
9594
break;
9695

@@ -118,6 +117,16 @@ public static function fromString($string, Translations $translations = null, $f
118117
}
119118

120119
if (isset($append)) {
120+
if ($append === 'Context') {
121+
$translation = $translation->getClone($translation->getContext()."\n".self::clean($data));
122+
break;
123+
}
124+
125+
if ($append === 'Original') {
126+
$translation = $translation->getClone(null, $translation->getOriginal()."\n".self::clean($data));
127+
break;
128+
}
129+
121130
if ($append === 'PluralTranslation') {
122131
$key = count($translation->getPluralTranslation()) - 1;
123132
$translation->setPluralTranslation($translation->getPluralTranslation($key)."\n".self::clean($data), $key);

src/Translation.php

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,76 @@ class Translation
1717
protected $flags = array();
1818
protected $translationCount;
1919

20+
/**
21+
* Generates the id of a translation (context + glue + original)
22+
*
23+
* @param string $context
24+
* @param string $original
25+
*
26+
* @return string
27+
*/
28+
public static function generateId($context, $original)
29+
{
30+
return "{$context}\004{$original}";
31+
}
32+
2033
/**
2134
* Construct
2235
*
2336
* @param string $context The context of the translation
2437
* @param string $original The original string
2538
* @param string $plural The original plural string
2639
*/
27-
public function __construct($context = '', $original = '', $plural = '')
40+
public function __construct($context, $original, $plural = '')
2841
{
29-
$this->setContext($context);
30-
$this->setOriginal($original);
42+
$this->context = (string) $context;
43+
$this->original = (string) $original;
44+
3145
$this->setPlural($plural);
3246
}
3347

3448
/**
35-
* Checks whether the translation matches with the arguments
49+
* Clones this translation
3650
*
37-
* @param string $context
38-
* @param string $original
51+
* @param null|string $context Optional new context
52+
* @param null|string $original Optional new original
53+
*/
54+
public function getClone($context = null, $original = null)
55+
{
56+
$new = clone $this;
57+
58+
if ($context !== null) {
59+
$new->context = (string) $context;
60+
}
61+
62+
if ($original !== null) {
63+
$new->original = (string) $original;
64+
}
65+
66+
return $new;
67+
}
68+
69+
/**
70+
* Returns the id of this translation
3971
*
40-
* @return boolean
72+
* @return string
4173
*/
42-
public function is($context, $original = '')
74+
public function getId()
4375
{
44-
return (($this->context === $context) && ($this->original === $original)) ? true : false;
76+
return static::generateId($this->context, $this->original);
4577
}
4678

4779
/**
48-
* Sets the original string
80+
* Checks whether the translation matches with the arguments
4981
*
82+
* @param string $context
5083
* @param string $original
84+
*
85+
* @return boolean
5186
*/
52-
public function setOriginal($original)
87+
public function is($context, $original = '')
5388
{
54-
$this->original = (string) $original;
89+
return (($this->context === $context) && ($this->original === $original)) ? true : false;
5590
}
5691

5792
/**
@@ -230,16 +265,6 @@ protected function normalizeTranslationCount()
230265
}
231266
}
232267

233-
/**
234-
* Sets the context of this translation
235-
*
236-
* @param string $context
237-
*/
238-
public function setContext($context)
239-
{
240-
$this->context = (string) $context;
241-
}
242-
243268
/**
244269
* Gets the context of this translation
245270
*

src/Translations.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ public function __call($name, $arguments)
8181
public function __clone()
8282
{
8383
$array = array();
84+
8485
foreach ($this as $key => $translation) {
8586
$array[$key] = clone $translation;
8687
}
88+
8789
$this->exchangeArray($array);
8890
}
8991

@@ -103,16 +105,18 @@ public function offsetSet($index, $value)
103105
throw new \InvalidArgumentException('Only instances of Gettext\\Translation must be added to a Gettext\\Translations');
104106
}
105107

106-
if (($exists = $this->find($value))) {
107-
$exists->mergeWith($value);
108-
$exists->setTranslationCount($this->translationCount);
108+
$id = $value->getId();
109109

110-
return $exists;
110+
if ($this->offsetExists($id)) {
111+
$this[$id]->mergeWith($value);
112+
$this[$id]->setTranslationCount($this->translationCount);
113+
114+
return $this[$id];
111115
}
112116

113117
$value->setTranslationCount($this->translationCount);
114118

115-
parent::offsetSet($index, $value);
119+
parent::offsetSet($id, $value);
116120

117121
return $value;
118122
}
@@ -293,20 +297,12 @@ public function hasDomain()
293297
public function find($context, $original = '')
294298
{
295299
if ($context instanceof Translation) {
296-
$original = $context->getOriginal();
297-
$context = $context->getContext();
300+
$id = $context->getId();
298301
} else {
299-
$original = (string) $original;
300-
$context = (string) $context;
302+
$id = Translation::generateId($context, $original);
301303
}
302304

303-
foreach ($this as $t) {
304-
if ($t->is($context, $original)) {
305-
return $t;
306-
}
307-
}
308-
309-
return false;
305+
return $this->offsetExists($id) ? $this[$id] : false;
310306
}
311307

312308
/**

src/Utils/Locales.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,8 +1108,8 @@ public static function getLocaleInfo($code)
11081108
$variants[] = $language;
11091109

11101110
foreach ($variants as $variant) {
1111-
if (isset(static::$localeInfo[$variant])) {
1112-
$result = static::$localeInfo[$variant];
1111+
if (isset(static::$pluralRules[$variant])) {
1112+
$result = static::$pluralRules[$variant];
11131113
$result['languageName'] = $language;
11141114
foreach ($variants as $variant) {
11151115
if (isset(static::$languageNames[$variant])) {

tests/CloningTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,5 @@ public function testClone()
1919
$item2 = $list2->find($item1);
2020
$this->assertInstanceOf('Gettext\\Translation', $item2);
2121
$this->assertNotSame($item, $item2);
22-
23-
$item1->setOriginal('Test 1');
24-
$item2->setOriginal('Test 2');
25-
26-
$this->assertSame('Test 1', $item1->getOriginal());
27-
$this->assertSame('Test 2', $item2->getOriginal());
2822
}
2923
}

0 commit comments

Comments
 (0)