Skip to content

Commit

Permalink
Issue #47 : Add glossary support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciloe committed Mar 5, 2024
1 parent 1bd86b7 commit f02710a
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/DeeplClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
53 changes: 53 additions & 0 deletions src/Handler/DeeplGlossariesListRetrievalRequestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Scn\DeeplApiConnector\Handler;

use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

final class DeeplGlossariesListRetrievalRequestHandler implements DeeplRequestHandlerInterface
{
public const API_ENDPOINT = '/v2/glossaries';

private string $authKey;

private StreamFactoryInterface $streamFactory;

public function __construct(
string $authKey,
StreamFactoryInterface $streamFactory
) {
$this->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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Scn\DeeplApiConnector\Handler;

use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

final class DeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler implements DeeplRequestHandlerInterface
{
public const API_ENDPOINT = '/v2/glossary-language-pairs';

private string $authKey;

private StreamFactoryInterface $streamFactory;

public function __construct(
string $authKey,
StreamFactoryInterface $streamFactory
) {
$this->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';
}
}
16 changes: 16 additions & 0 deletions src/Handler/DeeplRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/Handler/DeeplRequestFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ public function createDeeplFileTranslationRequestHandler(

public function createDeeplSupportedLanguageRetrievalRequestHandler(): DeeplRequestHandlerInterface;

public function createDeeplGlossariesSupportedLanguagesPairsRetrievalRequestHandler(): DeeplRequestHandlerInterface;

public function createDeeplGlossariesListRetrievalRequestHandler(): DeeplRequestHandlerInterface;

public function getDeeplBaseUri(): string;
}
37 changes: 37 additions & 0 deletions src/Model/GlossariesList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Scn\DeeplApiConnector\Model;

use DateTimeImmutable;
use stdClass;

final class GlossariesList extends AbstractResponseModel implements GlossariesListInterface
{
/** @var array<stdClass> */
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
);
}
}
18 changes: 18 additions & 0 deletions src/Model/GlossariesListInterfaces.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Scn\DeeplApiConnector\Model;

interface GlossariesListInterfaces
{
/**
* @return array<array{
* glossary_id: string,
* name: string,
* ready: boolean,
* source_lang: string,
* target_lang: string,
* creation_time: DateTimeImmutable,
* entry_count: int
* }>
*/
public function getList(): array;
}
31 changes: 31 additions & 0 deletions src/Model/GlossariesSupportedLanguagesPairs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Scn\DeeplApiConnector\Model;

use stdClass;

final class GlossariesSupportedLanguagesPairs extends AbstractResponseModel implements GlossariesSupportedLanguagesPairsInterface
{
/** @var array<stdClass> */
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
);
}
}
13 changes: 13 additions & 0 deletions src/Model/GlossariesSupportedLanguagesPairsInterfaces.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Scn\DeeplApiConnector\Model;

interface GlossariesSupportedLanguagesPairsInterfaces
{
/**
* @return array<array{
* source_lang: string,
* target_lang: string
* }>
*/
public function getList(): array;
}

0 comments on commit f02710a

Please sign in to comment.