Skip to content

Commit

Permalink
Implement all classes
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy ESCRICH <[email protected]>
  • Loading branch information
Ciloe committed Mar 6, 2024
1 parent f02710a commit bdd0a7f
Show file tree
Hide file tree
Showing 38 changed files with 858 additions and 33 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ try {

See `example/retrieve_supported_languages.php`

#### Working with glossaries

See [use_glossaries.php](example/use_glossaries.php)

## Testing

``` bash
Expand Down
98 changes: 98 additions & 0 deletions example/use_glossaries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use Scn\DeeplApiConnector\DeeplClientFactory;
use Scn\DeeplApiConnector\Enum\GlossarySubmissionEntryFormatEnum;
use Scn\DeeplApiConnector\Model\GlossariesListInterface;
use Scn\DeeplApiConnector\Model\GlossaryEntries;
use Scn\DeeplApiConnector\Model\GlossaryIdSubmission;
use Scn\DeeplApiConnector\Model\GlossaryInterface;
use Scn\DeeplApiConnector\Model\GlossarySubmission;
use Scn\DeeplApiConnector\Model\GlossariesSupportedLanguagesPairsInterface;
use Scn\DeeplApiConnector\Model\TranslationConfig;

$apiKey = 'your-api-key';

$deepl = DeeplClientFactory::create($apiKey);

/** @var GlossariesSupportedLanguagesPairsInterface $result */
$result = $deepl->getGlossariesSupportedLanguagesPairs();
/**
* List all available pairs
*/
var_dump($result->getList());

/** @var GlossariesListInterface $result */
$result = $deepl->getGlossariesList();
/**
* List all glossaries
*/
var_dump($result->getList());

$input = (new GlossarySubmission())
->setName('en => nl')
->setSourceLang('en')
->setTargetLang('nl')
->setEntries("Hello\tDag\nDog\tHond");

// NB.: Note that entries can be a tsv or csv. Related to the documentation : https://www.deepl.com/fr/docs-api/glossaries/formats
// To use CSV, this is the example :

$input->setEntriesFormat(GlossarySubmissionEntryFormatEnum::FORMAT_CSV)
->setEntries("Hello,Dag\nDog,Hond");
/** @var GlossaryInterface $glossary */
$glossary = $deepl->createGlossary($input);
/**
* Get created glossary
*/
var_dump($glossary->getDetails());

$input = (new GlossaryIdSubmission())
->setId($glossary->getDetails()['glossary_id']);
/** @var GlossaryInterface $result */
$result = $deepl->retrieveGlossary($input);
/**
* Get glossary details
*/
var_dump($result->getDetails());

/** @var GlossaryEntries $result */
$result = $deepl->retrieveGlossaryEntries($input);
/**
* Get glossary entries array
*/
var_dump($result->getList());
/**
* Get glossary entries real result
*/
var_dump($result->getResult());

$result = $deepl->deleteGlossary($input);
/**
* True if glossary successfully deleted
*/
var_dump($result);

/**
* Now, whe can get the glossary from the source and target lang and use it in the TranslationConfig
*/
$source = 'en';
$target = 'nl';
/** @var GlossariesListInterface $glossaries */
$glossaries = $deepl->getGlossariesList();
$glossary = current(array_filter(
$glossaries->getList(),
fn (array $e) => $e['source_lang'] === $source && $e['target_lang'] === $target
));

$translationConfig = new TranslationConfig(
'Hello World',
$target,
$source,
glossaryId: $glossary !== false ? $glossary['glossary_id'] : ''
);
$translationObj = $deepl->getTranslation($translationConfig);
var_dump($translationObj);
42 changes: 42 additions & 0 deletions src/DeeplClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
use Scn\DeeplApiConnector\Handler\DeeplRequestHandlerInterface;
use Scn\DeeplApiConnector\Model\BatchTranslation;
use Scn\DeeplApiConnector\Model\BatchTranslationConfig;
use Scn\DeeplApiConnector\Model\GlossaryEntries;
use Scn\DeeplApiConnector\Model\GlossaryIdSubmission;
use Scn\DeeplApiConnector\Model\GlossarySubmissionInterface;
use Scn\DeeplApiConnector\Model\FileSubmission;
use Scn\DeeplApiConnector\Model\FileSubmissionInterface;
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\Glossary;
use Scn\DeeplApiConnector\Model\ResponseModelInterface;
use Scn\DeeplApiConnector\Model\SupportedLanguages;
use Scn\DeeplApiConnector\Model\Translation;
Expand Down Expand Up @@ -148,6 +152,36 @@ public function getGlossariesList(): ResponseModelInterface
);
}

public function createGlossary(GlossarySubmissionInterface $submission): ResponseModelInterface
{
return (new Glossary())->hydrate($this->executeRequest(
$this->deeplRequestFactory->createDeeplGlossaryCreateRequestHandler($submission)
));
}

public function retrieveGlossary(GlossaryIdSubmission $submission): ResponseModelInterface
{
return (new Glossary())->hydrate($this->executeRequest(
$this->deeplRequestFactory->createDeeplGlossaryRetrieveRequestHandler($submission)
));
}

public function deleteGlossary(GlossaryIdSubmission $submission): bool
{
$this->executeRequest(
$this->deeplRequestFactory->createDeeplGlossaryDeleteRequestHandler($submission)
);

return true;
}

public function retrieveGlossaryEntries(GlossaryIdSubmission $submission): ResponseModelInterface
{
return (new GlossaryEntries())->hydrate($this->executeRequest(
$this->deeplRequestFactory->createDeeplGlossaryEntriesRetrieveRequestHandler($submission)
));
}

/**
* Execute given RequestHandler Request and returns decoded Json Object or throws Exception with Error Code
* and maybe given Error Message.
Expand All @@ -168,6 +202,14 @@ private function executeRequest(DeeplRequestHandlerInterface $requestHandler): s
)
->withBody($requestHandler->getBody());

if (!is_null($requestHandler->getAuthHeader())) {
$request = $request->withHeader('Authorization', $requestHandler->getAuthHeader());
}

if (!is_null($requestHandler->getAcceptHeader())) {
$request = $request->withHeader('Accept', $requestHandler->getAcceptHeader());
}

try {
$response = $this->httpClient->sendRequest($request);
} catch (ClientExceptionInterface $exception) {
Expand Down
32 changes: 32 additions & 0 deletions src/DeeplClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Scn\DeeplApiConnector\Enum\LanguageEnum;
use Scn\DeeplApiConnector\Model\FileSubmissionInterface;
use Scn\DeeplApiConnector\Model\FileTranslationConfigInterface;
use Scn\DeeplApiConnector\Model\GlossaryIdSubmission;
use Scn\DeeplApiConnector\Model\GlossarySubmissionInterface;
use Scn\DeeplApiConnector\Model\ResponseModelInterface;
use Scn\DeeplApiConnector\Model\TranslationConfigInterface;

Expand Down Expand Up @@ -59,4 +61,34 @@ public function getFileTranslation(FileSubmissionInterface $fileSubmission): Res
* Returns list of supported languages
*/
public function getSupportedLanguages(): ResponseModelInterface;

/**
* Returns list of supported languages pairs for glossaries
*/
public function getGlossariesSupportedLanguagesPairs(): ResponseModelInterface;

/**
* Returns list of glossaries
*/
public function getGlossariesList(): ResponseModelInterface;

/**
* Create a glossary and return glossary object
*/
public function createGlossary(GlossarySubmissionInterface $submission): ResponseModelInterface;

/**
* Returns a glossary using id
*/
public function retrieveGlossary(GlossaryIdSubmission $submission): ResponseModelInterface;

/**
* Delete a glossary using id
*/
public function deleteGlossary(GlossaryIdSubmission $submission): bool;

/**
* Returns list of entries
*/
public function retrieveGlossaryEntries(GlossaryIdSubmission $submission): ResponseModelInterface;
}
11 changes: 11 additions & 0 deletions src/Enum/GlossarySubmissionEntryFormatEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Scn\DeeplApiConnector\Enum;

final class GlossarySubmissionEntryFormatEnum
{
public const FORMAT_TSV = 'tsv';
public const FORMAT_CSV = 'csv';
}
16 changes: 16 additions & 0 deletions src/Handler/AbstractDeeplHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Scn\DeeplApiConnector\Handler;

abstract class AbstractDeeplHandler implements DeeplRequestHandlerInterface
{
public function getAuthHeader(): ?string
{
return null;
}

public function getAcceptHeader(): ?string
{
return null;
}
}
3 changes: 2 additions & 1 deletion src/Handler/DeeplBatchTranslationRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Builds the request body for batch translations
*/
final class DeeplBatchTranslationRequestHandler implements DeeplRequestHandlerInterface
final class DeeplBatchTranslationRequestHandler extends AbstractDeeplHandler
{
private const SEPARATOR = ',';
public const API_ENDPOINT = '/v2/translate';
Expand Down Expand Up @@ -59,6 +59,7 @@ public function getBody(): StreamInterface
'ignore_tags' => implode(static::SEPARATOR, $this->translation->getIgnoreTags()),
'split_sentences' => $this->translation->getSplitSentences(),
'preserve_formatting' => $this->translation->getPreserveFormatting(),
'glossary_id' => $this->translation->getGlossaryId(),
'auth_key' => $this->authKey,
]
)
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/DeeplFileRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Psr\Http\Message\StreamInterface;
use Scn\DeeplApiConnector\Model\FileSubmissionInterface;

final class DeeplFileRequestHandler implements DeeplRequestHandlerInterface
final class DeeplFileRequestHandler extends AbstractDeeplHandler
{
public const API_ENDPOINT = '/v2/document/%s/result';

Expand Down
2 changes: 1 addition & 1 deletion src/Handler/DeeplFileSubmissionRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Psr\Http\Message\StreamInterface;
use Scn\DeeplApiConnector\Model\FileTranslationConfigInterface;

final class DeeplFileSubmissionRequestHandler implements DeeplRequestHandlerInterface
final class DeeplFileSubmissionRequestHandler extends AbstractDeeplHandler
{
public const API_ENDPOINT = '/v2/document';

Expand Down
2 changes: 1 addition & 1 deletion src/Handler/DeeplFileTranslationStatusRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Psr\Http\Message\StreamInterface;
use Scn\DeeplApiConnector\Model\FileSubmissionInterface;

final class DeeplFileTranslationStatusRequestHandler implements DeeplRequestHandlerInterface
final class DeeplFileTranslationStatusRequestHandler extends AbstractDeeplHandler
{
public const API_ENDPOINT = '/v2/document/%s';

Expand Down
7 changes: 6 additions & 1 deletion src/Handler/DeeplGlossariesListRetrievalRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

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

Expand Down Expand Up @@ -46,6 +46,11 @@ public function getBody(): StreamInterface
);
}

public function getAuthHeader(): ?string
{
return sprintf('DeepL-Auth-Key %s', $this->authKey);
}

public function getContentType(): string
{
return 'application/x-www-form-urlencoded';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

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

Expand Down Expand Up @@ -35,15 +35,12 @@ public function getPath(): string

public function getBody(): StreamInterface
{
return $this->streamFactory->createStream(
http_build_query(
array_filter(
[
'auth_key' => $this->authKey,
]
)
)
);
return $this->streamFactory->createStream();
}

public function getAuthHeader(): ?string
{
return sprintf('DeepL-Auth-Key %s', $this->authKey);
}

public function getContentType(): string
Expand Down
Loading

0 comments on commit bdd0a7f

Please sign in to comment.