Passed
Pull Request — master (#6880)
by
unknown
10:05
created

StandardizationService::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
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
    public static function sort(array $array): array
11
    {
12
        usort($array, function ($a, $b) {
13
            return strcmp(self::standardizeString($a), self::standardizeString($b));
14
        });
15
        return $array;
16
    }
17
18
    /**
19
     * @param array $users of User entity
20
     * @param bool $addEmail if true, add an email field
21
     * @param string|null $countryCode if null take the current countryCode
22
     * @return array with an identity field containing first and last name, if $addEmail = true : add an email field
23
     */
24
    public static function sortByNameByCountryAndStandardizeName(array $users, bool $addEmail = false, string $countryCode = null): array
25
    {
26
        if ($countryCode === null) {
27
            $countryCode = api_get_language_isocode();
28
        }
29
30
        if (_api_get_person_name_convention($countryCode, 'sort_by')) {
31
            usort($users, function ($a, $b) {
32
                return strcmp(self::standardizeString($a->getLastName()), self::standardizeString($b->getLastName()));
33
            });
34
        } else {
35
            usort($users, function ($a, $b) {
36
                return strcmp(self::standardizeString($a->getFirstName()), self::standardizeString($b->getFirstName()));
37
            });
38
        }
39
40
        $standardizedNames = [];
41
        if ($addEmail) {
42
            foreach ($users as $user) {
43
                $standardizedNames[] = [
44
                    'identity' => self::standardizeName($user, $countryCode),
45
                    'email' => $user->getEmail()
46
                ];
47
            }
48
        } else {
49
            foreach ($users as $user) {
50
                $standardizedNames[] = ['identity' => self::standardizeName($user, $countryCode)];
51
            }
52
        }
53
        return $standardizedNames;
54
    }
55
56
    public static function standardizeName(User $user, string $countryCode = null): string
57
    {
58
        if ($countryCode === null) {
59
            $countryCode = api_get_language_isocode();
60
        }
61
62
        return ucwords(api_get_person_name($user->getFirstName(), $user->getLastName(), null, null, $countryCode));
63
    }
64
65
    public static function standardizeString(string $string): string
66
    {
67
        $string = trim($string, " \t\n\r\0\x0B()");
68
        $string = preg_replace('/[\x{0300}-\x{036f}]/u', '', normalizer_normalize($string, Normalizer::FORM_D));
69
        return mb_strtolower($string, 'UTF-8');
70
    }
71
72
}
73