|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chamilo\CoreBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Chamilo\CoreBundle\Entity\User; |
|
6
|
|
|
use Symfony\Polyfill\Intl\Normalizer\Normalizer; |
|
7
|
|
|
|
|
8
|
|
|
class StandardizationService |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Sorts an array of strings alphabetically after standardizing them. |
|
12
|
|
|
* |
|
13
|
|
|
* @param array $strings Array of strings to sort |
|
14
|
|
|
* @return array Sorted array of strings |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function sort(array $strings): array |
|
17
|
|
|
{ |
|
18
|
|
|
usort($strings, function ($a, $b) { |
|
19
|
|
|
return strcmp(self::standardizeString($a), self::standardizeString($b)); |
|
20
|
|
|
}); |
|
21
|
|
|
return $strings; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Sorts an array of User entities by name (first or last name based on country convention) |
|
26
|
|
|
* and returns an array of standardized names, optionally including email. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $users Array of User entities |
|
29
|
|
|
* @param bool $addEmail If true, includes the email field in the result |
|
30
|
|
|
* @param string|null $languageIsoCode ISO language code; if null, uses the current language code |
|
31
|
|
|
* @return array Array of associative arrays with 'identity' field (standardized name) |
|
32
|
|
|
* and optional 'email' field if $addEmail is true |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function sortByNameByCountryAndStandardizeName(array $users, bool $addEmail = false, string $languageIsoCode = null): array |
|
35
|
|
|
{ |
|
36
|
|
|
if ($languageIsoCode === null) { |
|
37
|
|
|
$languageIsoCode = api_get_language_isocode(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (_api_get_person_name_convention($languageIsoCode, 'sort_by')) { |
|
41
|
|
|
usort($users, function ($a, $b) { |
|
42
|
|
|
return strcmp(self::standardizeString($a->getLastName()), self::standardizeString($b->getLastName())); |
|
43
|
|
|
}); |
|
44
|
|
|
} else { |
|
45
|
|
|
usort($users, function ($a, $b) { |
|
46
|
|
|
return strcmp(self::standardizeString($a->getFirstName()), self::standardizeString($b->getFirstName())); |
|
47
|
|
|
}); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$standardizedNames = []; |
|
51
|
|
|
if ($addEmail) { |
|
52
|
|
|
foreach ($users as $user) { |
|
53
|
|
|
$standardizedNames[] = [ |
|
54
|
|
|
'identity' => self::standardizeName($user, $languageIsoCode), |
|
55
|
|
|
'email' => $user->getEmail() |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
} else { |
|
59
|
|
|
foreach ($users as $user) { |
|
60
|
|
|
$standardizedNames[] = ['identity' => self::standardizeName($user, $languageIsoCode)]; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
return $standardizedNames; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Standardizes a user's full name based on country-specific conventions. |
|
68
|
|
|
* |
|
69
|
|
|
* @param User $user The User entity containing first and last names |
|
70
|
|
|
* @param string|null $languageIsoCode ISO language code; if null, uses the current language code |
|
71
|
|
|
* @return string Standardized full name with capitalized words |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function standardizeName(User $user, string $languageIsoCode = null): string |
|
74
|
|
|
{ |
|
75
|
|
|
if ($languageIsoCode === null) { |
|
76
|
|
|
$languageIsoCode = api_get_language_isocode(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return ucwords(api_get_person_name($user->getFirstName(), $user->getLastName(), null, null, $languageIsoCode)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Standardizes a string by trimming whitespace, removing diacritics, and converting to lowercase to use sort functions. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $string The input string to standardize |
|
86
|
|
|
* @return string The standardized string in lowercase without diacritics |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function standardizeString(string $string): string |
|
89
|
|
|
{ |
|
90
|
|
|
$string = trim($string, " \t\n\r\0\x0B()"); |
|
91
|
|
|
$string = preg_replace('/[\x{0300}-\x{036f}]/u', '', normalizer_normalize($string, Normalizer::FORM_D)); |
|
92
|
|
|
return mb_strtolower($string, 'UTF-8'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|