-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Static Code Analysis, Refactor, Optimization
- Loading branch information
1 parent
45a6855
commit df4f7bd
Showing
33 changed files
with
169 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Macopedia\OpenAiTranslator\Client\OpenAiClient; | ||
|
||
use Macopedia\OpenAiTranslator\Client\OpenAiClient\Response\ChoicesResponse; | ||
use Macopedia\OpenAiTranslator\Client\OpenAiClient\Response\ErrorResponse; | ||
use Webmozart\Assert\Assert; | ||
use DateTime; | ||
use RuntimeException; | ||
|
||
class Response | ||
{ | ||
public static function fromArray(array $data): self | ||
{ | ||
if (array_key_exists('id', $data)) { | ||
Assert::keyExists($data, 'object'); | ||
Assert::keyExists($data, 'created'); | ||
Assert::keyExists($data, 'model'); | ||
Assert::keyExists($data, 'choices'); | ||
Assert::keyExists($data, 'usage'); | ||
|
||
return new ChoicesResponse( | ||
$data['id'], | ||
$data['object'], | ||
(new DateTime())->setTimestamp($data['created']), | ||
$data['model'], | ||
new Choices($data['choices']), | ||
$data['usage'] | ||
); | ||
} | ||
|
||
if (array_key_exists('error', $data)) { | ||
return new ErrorResponse($data['error']); | ||
} | ||
|
||
throw new RuntimeException('Can not create Response'); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ChoicesResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Macopedia\OpenAiTranslator\Client\OpenAiClient\Response; | ||
|
||
use Macopedia\OpenAiTranslator\Client\OpenAiClient\Choices; | ||
use Macopedia\OpenAiTranslator\Client\OpenAiClient\Response; | ||
use DateTime; | ||
|
||
class ChoicesResponse extends Response | ||
{ | ||
public function __construct( | ||
private string $id, | ||
private string $object, | ||
private DateTime $created, | ||
private string $model, | ||
private Choices $choices, | ||
array $usage | ||
) { | ||
} | ||
|
||
/** | ||
* @return Choices | ||
*/ | ||
public function getChoices(): Choices | ||
{ | ||
return $this->choices; | ||
} | ||
|
||
public function getFirstChoiceMessage(): ?string | ||
{ | ||
$firstMessage = $this->choices->getAnswers()[0] ?? null; | ||
|
||
if ($firstMessage === null) { | ||
return null; | ||
} | ||
return $firstMessage->getMessage()->getContent(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ErrorResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Macopedia\OpenAiTranslator\Client\OpenAiClient\Response; | ||
|
||
use Macopedia\OpenAiTranslator\Client\OpenAiClient\Response; | ||
|
||
class ErrorResponse extends Response | ||
{ | ||
public function __construct( | ||
private string $error | ||
) { | ||
} | ||
|
||
public function getError(): string | ||
{ | ||
return $this->error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
.../Translator/Resources/config/services.yml → ...iTranslator/Resources/config/services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Macopedia/OpenAiTranslator/Translator/OpenAiTranslator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Macopedia\OpenAiTranslator\Translator; | ||
|
||
use Macopedia\OpenAiTranslator\Client\OpenAiClient; | ||
|
||
class OpenAiTranslator implements TranslatorInterface | ||
{ | ||
private const MESSAGE = 'Translate the text after a semicolon to %s;'; | ||
|
||
public function __construct( | ||
private OpenAiClient $openAiClient | ||
) { | ||
} | ||
|
||
public function translate(string $text, Language $targetLanguageCode): ?string | ||
{ | ||
return $this | ||
->openAiClient | ||
->ask('user', sprintf(self::MESSAGE, $targetLanguageCode->asText()) . $text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.