Skip to content

Commit 81e6e03

Browse files
authored
Merge pull request #234 from schlessera/use-factory-methods-for-extensible-classes
Remove direct references to class `Translation`
2 parents c51ac33 + b66e289 commit 81e6e03

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

src/Extractors/Po.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function fromString($string, Translations $translations, array $op
2323
$lines = explode("\n", $string);
2424
$i = 0;
2525

26-
$translation = new Translation('', '');
26+
$translation = $translations->createNewTranslation('', '');
2727

2828
for ($n = count($lines); $i < $n; ++$i) {
2929
$line = trim($lines[$i]);
@@ -36,7 +36,7 @@ public static function fromString($string, Translations $translations, array $op
3636
$translations[] = $translation;
3737
}
3838

39-
$translation = new Translation('', '');
39+
$translation = $translations->createNewTranslation('', '');
4040
continue;
4141
}
4242

src/Extractors/Xliff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function fromString($string, Translations $translations, array $op
4040
$targets[] = (string) $target;
4141
}
4242

43-
$translation = new Translation(null, (string) $segment->source);
43+
$translation = $translations->createNewTranslation(null, (string) $segment->source);
4444
if (isset($unit['id'])) {
4545
$unitId = (string) $unit['id'];
4646
$translation->addComment("XLIFF_UNIT_ID: $unitId");

src/Translation.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ public static function generateId($context, $original)
3232
return "{$context}\004{$original}";
3333
}
3434

35+
/**
36+
* Create a new instance of a Translation object.
37+
*
38+
* This is a factory method that will work even when Translation is extended.
39+
*
40+
* @param string $context The context of the translation
41+
* @param string $original The original string
42+
* @param string $plural The original plural string
43+
* @return static New Translation instance
44+
*/
45+
public static function create($context, $original, $plural = '')
46+
{
47+
return new static($context, $original, $plural);
48+
}
49+
3550
/**
3651
* Construct.
3752
*

src/Translations.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,19 @@ class Translations extends ArrayObject
120120
],
121121
];
122122

123-
private $headers;
123+
protected $headers;
124+
125+
protected $translationClass;
124126

125127
/**
126128
* @see ArrayObject::__construct()
127129
*/
128-
public function __construct($input = [], $flags = 0, $iterator_class = 'ArrayIterator')
129-
{
130+
public function __construct(
131+
$input = [],
132+
$flags = 0,
133+
$iterator_class = 'ArrayIterator',
134+
$translationClass = 'Gettext\Translation'
135+
) {
130136
$this->headers = static::$options['defaultHeaders'];
131137

132138
foreach (static::$options['defaultDateHeaders'] as $header) {
@@ -135,6 +141,8 @@ public function __construct($input = [], $flags = 0, $iterator_class = 'ArrayIte
135141

136142
$this->headers[self::HEADER_LANGUAGE] = '';
137143

144+
$this->translationClass = $translationClass;
145+
138146
parent::__construct($input, $flags, $iterator_class);
139147
}
140148

@@ -455,7 +463,7 @@ public function countTranslated()
455463
*/
456464
public function insert($context, $original, $plural = '')
457465
{
458-
return $this->offsetSet(null, new Translation($context, $original, $plural));
466+
return $this->offsetSet(null, $this->createNewTranslation($context, $original, $plural));
459467
}
460468

461469
/**
@@ -473,4 +481,18 @@ public function mergeWith(Translations $translations, $options = Merge::DEFAULTS
473481

474482
return $this;
475483
}
484+
485+
/**
486+
* Create a new instance of a Translation object.
487+
*
488+
* @param string $context The context of the translation
489+
* @param string $original The original string
490+
* @param string $plural The original plural string
491+
* @return Translation New Translation instance
492+
*/
493+
public function createNewTranslation($context, $original, $plural = '')
494+
{
495+
$class = $this->translationClass;
496+
return $class::create($context, $original, $plural);
497+
}
476498
}

0 commit comments

Comments
 (0)