Refactor text splitting logic into dedicated commands and introduce PartText DTO for improved modularity and maintainability.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace KorElf\TranslateLaravel\DTO;
|
||||
|
||||
final class PartText
|
||||
{
|
||||
private array $texts = [];
|
||||
private array $beforeTexts = [];
|
||||
private array $afterTexts = [];
|
||||
private int $part = 0;
|
||||
|
||||
public function add(string $text, ?string $beforeText = null, ?string $afterText = null): void
|
||||
{
|
||||
$this->texts[$this->part] = $text;
|
||||
$this->beforeTexts[$this->part] = $beforeText;
|
||||
$this->afterTexts[$this->part] = $afterText;
|
||||
|
||||
$this->part++;
|
||||
}
|
||||
|
||||
public function getTextsForTranslation(): array
|
||||
{
|
||||
return $this->texts;
|
||||
}
|
||||
|
||||
public function getTextsAfterTranslation(array $texts): string
|
||||
{
|
||||
$result = '';
|
||||
foreach ($texts as $key => $text) {
|
||||
if ($this->beforeTexts[$key] !== null) {
|
||||
$result .= $this->beforeTexts[$key];
|
||||
}
|
||||
$result .= $text;
|
||||
if ($this->afterTexts[$key] !== null) {
|
||||
$result .= $this->afterTexts[$key];
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user