GetFromICLResponseMapper.php
1.87 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
// This is sample of data that mapper returns formatted data same as it.
// return Either::of( [
// 'translators' => [
// [
// 'user' => [
// 'id' => 1,
// 'first' => 'Translator',
// 'last' => 'First',
// 'email' => 'translator3@ytest.com',
// 'userName' => 'translator',
// 'wpRole' => 'author',
// ],
// 'languagePairs' => [
// 'en' => [ 'ar', 'bs'],
// ],
// ],
// ]
// ] );
namespace WPML\ICLToATEMigration\Endpoints\Translators;
use WPML\FP\Either;
use WPML\FP\Fns;
class GetFromICLResponseMapper {
/**
* Returns formatted data of translators.
*
* @param array $records
*
* @return callable|\WPML\FP\Right
*/
public static function map( $records ) {
return Either::of( [ 'translators' => Fns::map( [ self::class, 'constructUserData' ], $records ) ] );
}
/**
* Formats translator data
*
* @param object $record
*
* @return array
*/
public static function constructUserData( $record ) {
$user = get_user_by( 'email', $record->email );
return [
'user' => [
'id' => $record->icl_id,
'first' => $record->first_name,
'last' => $record->last_name,
'email' => $record->email,
'userName' => $user ? $user->data->user_login : strtolower( $record->first_name . '_' . $record->last_name ),
'wpRole' => $user ? current( $user->roles ) : 'subscriber'
],
'languagePairs' => self::constructUserLanguagePairs( $record->lang_pairs )
];
}
/**
* Formats translator language pairs data
*
* @param array $langPairs
*
* @return array
*/
public static function constructUserLanguagePairs( $langPairs ) {
$constructedLangPairs = [];
foreach ( $langPairs as $langPair ) {
$constructedLangPairs[ $langPair->from ][] = $langPair->to;
}
return $constructedLangPairs;
}
}