Completed
Pull Request — newinternal (#285)
by Simon
07:17 queued 04:17
created

StringFunctions::formatAsUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 16
ccs 0
cts 8
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca;
10
11
class StringFunctions
12
{
13
    /**
14
     * Formats a string to be used as a username.
15
     *
16
     * @param $username
17
     *
18
     * @return string
19
     */
20
    public function formatAsUsername($username)
21
    {
22
        // trim whitespace from the ends
23
        $uname = mb_ereg_replace("^[ \t]+|[ \t]+$", "", $username);
24
25
        // convert first char to uppercase
26
        $uname = $this->ucfirst($uname);
27
28
        // replace spaces with underscores
29
        $uname = mb_ereg_replace("[ ]+", "_", $uname);
30
31
        // trim underscores from the end
32
        $uname = mb_ereg_replace("[_]+$", "", $uname);
33
34
        return $uname;
35
    }
36
37
    /**
38
     * Formats a string to be used as an email (specifically strips whitespace
39
     * from the beginning/end of the Email, as well as immediately before/after
40
     * the @ in the Email).
41
     *
42
     * @param $email
43
     *
44
     * @return string
45
     */
46
    public static function formatAsEmail($email)
47
    {
48
        // trim whitespace from the ends
49
        $newemail = mb_ereg_replace("^[ \t]+|[ \t]+$", "", $email);
50
51
        // trim whitespace from around the email address
52
        $newemail = mb_ereg_replace("[ \t]+@", "@", $newemail);
53
        $newemail = mb_ereg_replace("@[ \t]+", "@", $newemail);
54
55
        return $newemail;
56
    }
57
58
    /**
59
     * Returns true if a string is a multibyte string
60
     *
61
     * @param string $string
62
     *
63
     * @return bool
64
     */
65
    public function isMultibyte($string)
66
    {
67
        return strlen($string) !== mb_strlen($string);
68
    }
69
70
    /**
71
     * Make a string's first character uppercase
72
     *
73
     * @param string $string
74
     *
75
     * @return string
76
     */
77
    public function ucfirst($string)
78
    {
79
        if (ord($string) < 128) {
80
            return ucfirst($string);
81
        }
82
        else {
83
            return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
84
        }
85
    }
86
}
87