Add DTOs, exceptions, and jobs for translation service.

Introduced new Data Transfer Objects (DTOs), exceptions, and jobs to enhance the translation service functionality. Updated namespaces for consistency and added rate limiting to the translation provider. Expanded the README with detailed usage instructions.
This commit is contained in:
2024-10-11 00:22:46 +05:00
parent 0d13d602a7
commit 55b04f0eab
26 changed files with 751 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace korElf\TranslateLaravel\DTO;
namespace KorElf\TranslateLaravel\DTO;
final class Languages
{

41
src/DTO/Parameter.php Normal file
View File

@@ -0,0 +1,41 @@
<?php declare(strict_types=1);
namespace KorElf\TranslateLaravel\DTO;
use KorElf\TranslateLaravel\Enums\TextType;
final readonly class Parameter
{
public function __construct(
private string $text,
private TextType $textType,
private string $targetLanguageCode,
private ?string $sourceLanguageCode = null,
private ?string $driver = null
) { }
public function getText(): string
{
return $this->text;
}
public function getTextType(): TextType
{
return $this->textType;
}
public function getTargetLanguageCode(): string
{
return $this->targetLanguageCode;
}
public function getSourceLanguageCode(): ?string
{
return $this->sourceLanguageCode;
}
public function getDriver(): ?string
{
return $this->driver;
}
}

View File

@@ -0,0 +1,59 @@
<?php declare(strict_types=1);
namespace KorElf\TranslateLaravel\DTO;
use KorElf\TranslateLaravel\Enums\TextType;
final readonly class ProcessTranslateDto
{
public function __construct(
private string $groupName,
private string $key,
private int $part,
private string $text,
private TextType $textType,
private string $targetLanguageCode,
private ?string $sourceLanguageCode = null,
private ?string $driver = null
) { }
public function getGroupName(): string
{
return $this->groupName;
}
public function getKey(): string
{
return $this->key;
}
public function getPart(): int
{
return $this->part;
}
public function getText(): string
{
return $this->text;
}
public function getTextType(): TextType
{
return $this->textType;
}
public function getTargetLanguageCode(): string
{
return $this->targetLanguageCode;
}
public function getSourceLanguageCode(): ?string
{
return $this->sourceLanguageCode;
}
public function getDriver(): ?string
{
return $this->driver;
}
}

View File

@@ -0,0 +1,27 @@
<?php declare(strict_types=1);
namespace KorElf\TranslateLaravel\DTO;
final readonly class ProcessTranslateLimit
{
public function __construct(
private int $maxRequest,
private int $rateSeconds,
private string $driver,
) { }
public function getMaxRequest(): int
{
return $this->maxRequest;
}
public function getRateSeconds(): int
{
return $this->rateSeconds;
}
public function getDriver(): string
{
return $this->driver;
}
}

View File

@@ -0,0 +1,65 @@
<?php declare(strict_types=1);
namespace KorElf\TranslateLaravel\DTO;
use KorElf\TranslateLaravel\Enums\TextType;
use KorElf\TranslateLaravel\Exceptions\RunTranslateDtoException;
final class RunTranslateDto
{
/**
* @var array [key => \KorElf\TranslateLaravel\DTO\Parameter]
*/
private array $params = [];
public function addParam(
string $key,
string $text,
TextType $textType,
string $targetLanguageCode,
?string $sourceLanguageCode = null,
?string $driver = null
): self
{
if (isset($this->params[$key])) {
throw new RunTranslateDtoException('Duplicate key: ' . $key);
}
$this->params[$key] = new Parameter(
$text,
$textType,
$targetLanguageCode,
$sourceLanguageCode,
$driver,
);
return $this;
}
public function addParamHtml(
string $key,
string $text,
string $targetLanguageCode,
?string $sourceLanguageCode = null,
?string $driver = null
): self
{
return $this->addParam($key, $text, TextType::Html, $targetLanguageCode, $sourceLanguageCode, $driver);
}
public function addParamText(
string $key,
string $text,
string $targetLanguageCode,
?string $sourceLanguageCode = null,
?string $driver = null
): self
{
return $this->addParam($key, $text, TextType::Text, $targetLanguageCode, $sourceLanguageCode, $driver);
}
public function getParams(): array
{
return $this->params;
}
}

20
src/DTO/Translated.php Normal file
View File

@@ -0,0 +1,20 @@
<?php declare(strict_types=1);
namespace KorElf\TranslateLaravel\DTO;
final class Translated
{
private array $data = [];
public function add(string $key, int $parts): self
{
$this->data[$key] = $parts;
return $this;
}
public function toArray(): array
{
return $this->data;
}
}