Refactor text splitting logic into dedicated commands and introduce PartText DTO for improved modularity and maintainability.

This commit is contained in:
2026-06-20 16:05:31 +05:00
parent 92fd2cab1b
commit fe90500e44
6 changed files with 441 additions and 105 deletions
+40
View File
@@ -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;
}
}