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

StringFunctions   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A formatAsUsername() 0 16 1
A formatAsEmail() 0 11 1
A isMultibyte() 0 4 1
A ucfirst() 0 9 2
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