FetchTranslationMemory.php
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace WPML\ST\Main\Ajax;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
class FetchTranslationMemory implements IHandler {
/** @var \WPML_ST_Translation_Memory_Records $records */
private $records;
public function __construct( \WPML_ST_Translation_Memory_Records $records ) {
$this->records = $records;
}
public function run( Collection $data ) {
$translations = Obj::prop( 'batch', $data )
? $this->fetchBatchStrings( Obj::prop( 'strings', $data ) )
: $this->fetchSingleString( $data );
if ( $translations !== false ) {
return Either::of( $translations );
} else {
return Either::left( 'invalid data' );
}
}
private function fetchBatchStrings( $strings ) {
$results = Fns::map( [ $this, 'fetchSingleString' ], $strings );
return Lst::includes( false, $results ) ? false : $results;
}
public function fetchSingleString( $data ) {
$string = Obj::prop( 'string', $data );
$source = Obj::prop( 'source', $data );
$target = Obj::prop( 'target', $data );
if ( $string && $source && $target ) {
return $this->records->get( [ $string ], $source, $target );
} else {
return false;
}
}
}