| 1 |
<?php |
| 2 |
|
| 3 |
class DeepLApiGlossary extends DeepLApi { |
| 4 |
//protected $endPoint = ''; |
| 5 |
//public $allow_cache = false; |
| 6 |
//protected $http_mode = ''; |
| 7 |
|
| 8 |
public function getSupportedLanguages() { |
| 9 |
$this->endPoint = 'glossary-language-pairs'; |
| 10 |
$this->http_mode = 'GET'; |
| 11 |
$this->doRequest(); |
| 12 |
|
| 13 |
|
| 14 |
if ( !isset( $this->response ) || !isset( $this->response['body'] ) ) { |
| 15 |
return false; |
| 16 |
} |
| 17 |
$json = $this->response['body']; |
| 18 |
$array = json_decode( $json, true ); |
| 19 |
if( !isset( $array['supported_languages'] ) ) |
| 20 |
return false; |
| 21 |
|
| 22 |
return $array['supported_languages']; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
public function createGlossary( $name, $data, $source_lang, $target_lang, $format = 'tsv' ) { |
| 27 |
$this->endPoint = 'glossaries'; |
| 28 |
$this->http_mode = 'POST'; |
| 29 |
|
| 30 |
$body = array( |
| 31 |
'name' => $name, |
| 32 |
'source_lang' => $source_lang, |
| 33 |
'target_lang' => $target_lang, |
| 34 |
'entries' => $data, |
| 35 |
'entries_format' => $format |
| 36 |
); |
| 37 |
$this->final_request['body'] = json_encode( $body ); |
| 38 |
|
| 39 |
plouf( $this->final_request ); |
| 40 |
// die('okzemrpzejrpizr'); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
$this->doRequest( 'POST' ); |
| 45 |
if ( !isset( $this->response ) || !isset( $this->response['body'] ) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
$json = $this->response['body']; |
| 49 |
$array = json_decode( $json, true ); |
| 50 |
if( !isset( $array['glossary_id'] ) ) |
| 51 |
return false; |
| 52 |
|
| 53 |
return $array; |
| 54 |
} |
| 55 |
|
| 56 |
public function listGlossaries() { |
| 57 |
$this->endPoint = 'glossaries'; |
| 58 |
$this->http_mode = 'GET'; |
| 59 |
$this->doRequest(); |
| 60 |
|
| 61 |
|
| 62 |
if ( !isset( $this->response ) || !isset( $this->response['body'] ) ) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
$json = $this->response['body']; |
| 66 |
$array = json_decode( $json, true ); |
| 67 |
|
| 68 |
if( !isset( $array['glossaries'] ) ) |
| 69 |
return false; |
| 70 |
|
| 71 |
return $array['glossaries']; |
| 72 |
} |
| 73 |
|
| 74 |
public function deleteGlossary( $glossary_id ) { |
| 75 |
$this->endPoint = 'glossaries/' . htmlspecialchars_decode( $glossary_id ); |
| 76 |
$this->http_mode = 'DELETE'; |
| 77 |
$this->doRequest(); |
| 78 |
|
| 79 |
|
| 80 |
return $this->response; |
| 81 |
} |
| 82 |
|
| 83 |
public function listGlossaryEntries( $glossary_id, $format = 'tsv' ) { |
| 84 |
$this->endPoint = 'glossaries/' . htmlspecialchars_decode( $glossary_id ) . '/entries'; |
| 85 |
$this->http_mode = 'GET'; |
| 86 |
$this->format = $format; |
| 87 |
|
| 88 |
$this->doRequest(); |
| 89 |
|
| 90 |
if( !isset( $this->response['body'] ) ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
$tsv = $this->response['body']; |
| 94 |
return $tsv; |
| 95 |
|
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
function addHeaders( $headers ) { |
| 100 |
|
| 101 |
echo "end " . $this->endPoint; |
| 102 |
if( $this->endPoint == 'glossaries' && $this->http_mode == 'POST' ) { |
| 103 |
$headers['Content-Type'] = 'application/json'; |
| 104 |
} |
| 105 |
elseif( strpos( $this->endPoint, 'entries' ) !== false ) { |
| 106 |
if( $this->format == 'csv' ) |
| 107 |
$headers['Accept'] = 'text/csv'; |
| 108 |
else |
| 109 |
$headers['Accept'] = 'text/tab-separated-values'; |
| 110 |
} |
| 111 |
|
| 112 |
return $headers; |
| 113 |
} |
| 114 |
|
| 115 |
function buildBody( $mode = 'POST', $endPoint = 'glossaries' ) { |
| 116 |
if( $this->http_mode == 'POST' ) |
| 117 |
return $this->final_request['body']; |
| 118 |
else |
| 119 |
return; |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
} |