From f02710a3a06e94652323bbc058db712cabab32c5 Mon Sep 17 00:00:00 2001 From: Jimmy E Date: Tue, 5 Mar 2024 08:51:09 +0000 Subject: [PATCH] Issue #47 : Add glossary support --- src/DeeplClient.php | 20 +++++++ ...lGlossariesListRetrievalRequestHandler.php | 53 +++++++++++++++++++ ...dLanguagesPairsRetrievalRequestHandler.php | 53 +++++++++++++++++++ src/Handler/DeeplRequestFactory.php | 16 ++++++ src/Handler/DeeplRequestFactoryInterface.php | 4 ++ src/Model/GlossariesList.php | 37 +++++++++++++ src/Model/GlossariesListInterfaces.php | 18 +++++++ .../GlossariesSupportedLanguagesPairs.php | 31 +++++++++++ ...ariesSupportedLanguagesPairsInterfaces.php | 13 +++++ 9 files changed, 245 insertions(+) create mode 100644 src/Handler/DeeplGlossariesListRetrievalRequestHandler.php create mode 100644 src/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler.php create mode 100644 src/Model/GlossariesList.php create mode 100644 src/Model/GlossariesListInterfaces.php create mode 100644 src/Model/GlossariesSupportedLanguagesPairs.php create mode 100644 src/Model/GlossariesSupportedLanguagesPairsInterfaces.php diff --git a/src/DeeplClient.php b/src/DeeplClient.php index 7e39705..eecadef 100644 --- a/src/DeeplClient.php +++ b/src/DeeplClient.php @@ -17,6 +17,8 @@ use Scn\DeeplApiConnector\Model\FileTranslation; use Scn\DeeplApiConnector\Model\FileTranslationConfigInterface; use Scn\DeeplApiConnector\Model\FileTranslationStatus; +use Scn\DeeplApiConnector\Model\GlossariesList; +use Scn\DeeplApiConnector\Model\GlossariesSupportedLanguagesPairs; use Scn\DeeplApiConnector\Model\ResponseModelInterface; use Scn\DeeplApiConnector\Model\SupportedLanguages; use Scn\DeeplApiConnector\Model\Translation; @@ -128,6 +130,24 @@ public function getSupportedLanguages(): ResponseModelInterface ); } + public function getGlossariesSupportedLanguagesPairs(): ResponseModelInterface + { + return (new GlossariesSupportedLanguagesPairs())->hydrate( + $this->executeRequest( + $this->deeplRequestFactory->createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler() + ) + ); + } + + public function getGlossariesList(): ResponseModelInterface + { + return (new GlossariesList())->hydrate( + $this->executeRequest( + $this->deeplRequestFactory->createDeeplGlossariesListRetrievalRequestHandler() + ) + ); + } + /** * Execute given RequestHandler Request and returns decoded Json Object or throws Exception with Error Code * and maybe given Error Message. diff --git a/src/Handler/DeeplGlossariesListRetrievalRequestHandler.php b/src/Handler/DeeplGlossariesListRetrievalRequestHandler.php new file mode 100644 index 0000000..840bb10 --- /dev/null +++ b/src/Handler/DeeplGlossariesListRetrievalRequestHandler.php @@ -0,0 +1,53 @@ +authKey = $authKey; + $this->streamFactory = $streamFactory; + } + + public function getMethod(): string + { + return DeeplRequestHandlerInterface::METHOD_GET; + } + + public function getPath(): string + { + return static::API_ENDPOINT; + } + + public function getBody(): StreamInterface + { + return $this->streamFactory->createStream( + http_build_query( + array_filter( + [ + 'auth_key' => $this->authKey, + ] + ) + ) + ); + } + + public function getContentType(): string + { + return 'application/x-www-form-urlencoded'; + } +} diff --git a/src/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler.php b/src/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler.php new file mode 100644 index 0000000..4c69b80 --- /dev/null +++ b/src/Handler/DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler.php @@ -0,0 +1,53 @@ +authKey = $authKey; + $this->streamFactory = $streamFactory; + } + + public function getMethod(): string + { + return DeeplRequestHandlerInterface::METHOD_GET; + } + + public function getPath(): string + { + return static::API_ENDPOINT; + } + + public function getBody(): StreamInterface + { + return $this->streamFactory->createStream( + http_build_query( + array_filter( + [ + 'auth_key' => $this->authKey, + ] + ) + ) + ); + } + + public function getContentType(): string + { + return 'application/x-www-form-urlencoded'; + } +} diff --git a/src/Handler/DeeplRequestFactory.php b/src/Handler/DeeplRequestFactory.php index a1ceb4f..5df098b 100644 --- a/src/Handler/DeeplRequestFactory.php +++ b/src/Handler/DeeplRequestFactory.php @@ -96,6 +96,22 @@ public function createDeeplSupportedLanguageRetrievalRequestHandler(): DeeplRequ ); } + public function createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(): DeeplRequestHandlerInterface + { + return new DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler( + $this->authKey, + $this->streamFactory + ); + } + + public function createDeeplGlossariesListRetrievalRequestHandler(): DeeplRequestHandlerInterface + { + return new DeeplGlossariesListRetrievalRequestHandler( + $this->authKey, + $this->streamFactory + ); + } + public function getDeeplBaseUri(): string { if (strpos($this->authKey, ':fx') !== false) { diff --git a/src/Handler/DeeplRequestFactoryInterface.php b/src/Handler/DeeplRequestFactoryInterface.php index 2abf52d..cbca447 100644 --- a/src/Handler/DeeplRequestFactoryInterface.php +++ b/src/Handler/DeeplRequestFactoryInterface.php @@ -33,5 +33,9 @@ public function createDeeplFileTranslationRequestHandler( public function createDeeplSupportedLanguageRetrievalRequestHandler(): DeeplRequestHandlerInterface; + public function createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(): DeeplRequestHandlerInterface; + + public function createDeeplGlossariesListRetrievalRequestHandler(): DeeplRequestHandlerInterface; + public function getDeeplBaseUri(): string; } diff --git a/src/Model/GlossariesList.php b/src/Model/GlossariesList.php new file mode 100644 index 0000000..901c781 --- /dev/null +++ b/src/Model/GlossariesList.php @@ -0,0 +1,37 @@ + */ + private array $list; + + public function hydrate(stdClass $responseModel): ResponseModelInterface + { + $this->list = $responseModel->content->supported_languages; + + return $this; + } + + public function getList(): array + { + return array_map( + fn (stdClass $item): array => [ + 'glossary_id' => $item->glossary_id, + 'name' => $item->name, + 'ready' => $item->ready, + 'source_lang' => $item->source_lang, + 'target_lang' => $item->target_lang, + 'creation_time' => new DateTimeImmutable($item->creation_time), + 'entry_count' => $item->entry_count, + ], + $this->list + ); + } +} diff --git a/src/Model/GlossariesListInterfaces.php b/src/Model/GlossariesListInterfaces.php new file mode 100644 index 0000000..2f349b6 --- /dev/null +++ b/src/Model/GlossariesListInterfaces.php @@ -0,0 +1,18 @@ + + */ + public function getList(): array; +} diff --git a/src/Model/GlossariesSupportedLanguagesPairs.php b/src/Model/GlossariesSupportedLanguagesPairs.php new file mode 100644 index 0000000..61dbe31 --- /dev/null +++ b/src/Model/GlossariesSupportedLanguagesPairs.php @@ -0,0 +1,31 @@ + */ + private array $list; + + public function hydrate(stdClass $responseModel): ResponseModelInterface + { + $this->list = $responseModel->content->supported_languages; + + return $this; + } + + public function getList(): array + { + return array_map( + fn (stdClass $item): array => [ + 'source_lang' => $item->source_lang, + 'target_lang' => $item->target_lang, + ], + $this->list + ); + } +} diff --git a/src/Model/GlossariesSupportedLanguagesPairsInterfaces.php b/src/Model/GlossariesSupportedLanguagesPairsInterfaces.php new file mode 100644 index 0000000..5041f50 --- /dev/null +++ b/src/Model/GlossariesSupportedLanguagesPairsInterfaces.php @@ -0,0 +1,13 @@ + + */ + public function getList(): array; +}