Baby-Markt /
deepl-php-lib
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BabyMarkt\DeepL; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class Glossary capsules functionality provided by DeepL where one can configure a glossary/dictionary which |
||
| 7 | * is used in translation. |
||
| 8 | * |
||
| 9 | * @see https://support.deepl.com/hc/en-us/articles/4405021321746-Managing-glossaries-with-the-DeepL-API |
||
| 10 | */ |
||
| 11 | class Glossary |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * API URL: glossaries |
||
| 15 | */ |
||
| 16 | const API_URL_RESOURCE_GLOSSARIES = 'glossaries'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var ClientInterface |
||
| 20 | */ |
||
| 21 | private $client; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param $authKey |
||
| 25 | * @param int $apiVersion |
||
| 26 | * @param string $host |
||
| 27 | * @param ClientInterface|null $client |
||
| 28 | */ |
||
| 29 | public function __construct($authKey, $apiVersion = 2, $host = 'api-free.deepl.com', ClientInterface $client = null) |
||
| 30 | { |
||
| 31 | $this->client = $client ?? new Client($authKey, $apiVersion, $host); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Calls the glossary-Endpoint and return Json-response as an array |
||
| 36 | * |
||
| 37 | * @return array |
||
| 38 | * |
||
| 39 | * @throws DeepLException |
||
| 40 | */ |
||
| 41 | public function listGlossaries() |
||
| 42 | { |
||
| 43 | return $this->client->request($this->client->buildBaseUrl(self::API_URL_RESOURCE_GLOSSARIES), '', 'GET'); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Creates a glossary, entries must be formatted as [sourceText => entryText] e.g: ['Hallo' => 'Hello'] |
||
| 48 | * |
||
| 49 | * @param string $name |
||
| 50 | * @param array $entries |
||
| 51 | * @param string $sourceLang |
||
| 52 | * @param string $targetLang |
||
| 53 | * @param string $entriesFormat |
||
| 54 | * |
||
| 55 | * @return array|null |
||
| 56 | * |
||
| 57 | * @throws DeepLException |
||
| 58 | */ |
||
| 59 | public function createGlossary( |
||
| 60 | string $name, |
||
| 61 | array $entries, |
||
| 62 | string $sourceLang = 'de', |
||
| 63 | string $targetLang = 'en', |
||
| 64 | string $entriesFormat = 'tsv' |
||
| 65 | ) { |
||
| 66 | $formattedEntries = ""; |
||
| 67 | foreach ($entries as $source => $target) { |
||
| 68 | $formattedEntries .= sprintf("%s\t%s\n", $source, $target); |
||
| 69 | } |
||
| 70 | |||
| 71 | $paramsArray = [ |
||
| 72 | 'name' => $name, |
||
| 73 | 'source_lang' => $sourceLang, |
||
| 74 | 'target_lang' => $targetLang, |
||
| 75 | 'entries' => $formattedEntries, |
||
| 76 | 'entries_format' => $entriesFormat |
||
| 77 | ]; |
||
| 78 | |||
| 79 | $url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_GLOSSARIES); |
||
| 80 | $body = $this->client->buildQuery($paramsArray); |
||
| 81 | |||
| 82 | return $this->client->request($url, $body); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Deletes a glossary |
||
| 87 | * |
||
| 88 | * @param string $glossaryId |
||
| 89 | * |
||
| 90 | * @return array|null |
||
| 91 | * |
||
| 92 | * @throws DeepLException |
||
| 93 | */ |
||
| 94 | public function deleteGlossary(string $glossaryId) |
||
| 95 | { |
||
| 96 | $url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_GLOSSARIES); |
||
| 97 | $url .= "/$glossaryId"; |
||
| 98 | |||
| 99 | return $this->client->request($url, '', 'DELETE'); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Gets information about a glossary |
||
| 104 | * |
||
| 105 | * @param string $glossaryId |
||
| 106 | * |
||
| 107 | * @return array|null |
||
| 108 | * |
||
| 109 | * @throws DeepLException |
||
| 110 | */ |
||
| 111 | public function glossaryInformation(string $glossaryId) |
||
| 112 | { |
||
| 113 | $url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_GLOSSARIES); |
||
| 114 | $url .= "/$glossaryId"; |
||
| 115 | |||
| 116 | return $this->client->request($url, '', 'GET'); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Fetch glossary entries and format them as associative array [source => target] |
||
| 121 | * |
||
| 122 | * @param string $glossaryId |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | * |
||
| 126 | * @throws DeepLException |
||
| 127 | */ |
||
| 128 | public function glossaryEntries(string $glossaryId) |
||
| 129 | { |
||
| 130 | $url = $this->client->buildBaseUrl(self::API_URL_RESOURCE_GLOSSARIES); |
||
| 131 | $url .= "/$glossaryId/entries"; |
||
| 132 | |||
| 133 | $response = $this->client->request($url, '', 'GET'); |
||
| 134 | |||
| 135 | $entries = []; |
||
| 136 | if (!empty($response)) { |
||
| 137 | $allEntries = preg_split('/\n/', $response); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 138 | foreach ($allEntries as $entry) { |
||
| 139 | $sourceAndTarget = preg_split('/\s+/', rtrim($entry)); |
||
| 140 | if (isset($sourceAndTarget[0], $sourceAndTarget[1])) { |
||
| 141 | $entries[$sourceAndTarget[0]] = $sourceAndTarget[1]; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | return $entries; |
||
| 147 | } |
||
| 148 | } |
||
| 149 |