Completed
Push — master ( 69097b...4e1c03 )
by Danilo
04:33
created

Text::camelCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Entities;
20
21
/**
22
 * \addtogroup Utility-classes Utility classes
23
 * \brief Static methods grouped by class and context.
24
 * @{
25
 */
26
27
/**
28
 * \class Text
29
 * \brief Contains text helper methods
30
 */
31
class Text
32
{
33
    /**
34
     * \brief Get hashtag contained in a string.
35
     * \details Check hashtags in a string using a regular expression.
36
     * All valid hashtags will be returned in an array.
37
<<<<<<< HEAD
38
     *
39
     * See the following [StackOverflow thread](http://stackoverflow.com/questions/3060601/retrieve-all-hashtags-from-a-tweet-in-a-php-function) to learn more.
40
     * @param $string The string to check for hashtags.
41
=======
42
     * [Credis to trante](http://stackoverflow.com/questions/3060601/retrieve-all-hashtags-from-a-tweet-in-a-php-function).
43
     * @param string $string The string to check for hashtags.
44
>>>>>>> master
45
     * @return array An array of valid hashtags, can be empty.
46
     */
47
    public static function getHashtags(string $string) : array
48
    {
49
        if (preg_match_all("/(#\w+)/u", $string, $matches) != 0) {
50
            $hashtagsArray = array_count_values($matches[0]);
51
            $hashtags = array_keys($hashtagsArray);
52
        }
53
54
        return $hashtags ?? [];
55
    }
56
57
    /**
58
     * \brief Convert a string into camelCase style.
59
     * \details Take a look [here](http://www.mendoweb.be/blog/php-convert-string-to-camelcase-string/)
60
     * to learn more.
61
     * @param string $str The string to convert.
62
     * @param array $noStrip
63
     * @return string $str The input string converted to camelCase.
64
     */
65 7
    public static function camelCase($str, array $noStrip = [])
66
    {
67
        // Non-alpha and non-numeric characters become spaces.
68 7
        $str = preg_replace('/[^a-z0-9' . implode("", $noStrip) . ']+/i', ' ', $str);
69 7
        $str = trim($str);
70
71
        // Make each word's letter uppercase.
72 7
        $str = ucwords($str);
73 7
        $str = str_replace(" ", "", $str);
74 7
        $str = lcfirst($str);
75
76 7
        return $str;
77
    }
78
79
    /**
80
<<<<<<< HEAD
81
     * \brief Remove HTML formattation from Telegram usernames.
82
     * \details Remove the $modificator html formattation from a message
83
     * containing Telegram username.
84
     * @param $string to parse.
85
     * @param $tag Formattation tag to remove.
86
=======
87
     * \brief Remove html formattation from telegram usernames in string.
88
     * \details Remove the $modificator html formattation from a message containing telegram username, to let the user click them.
89
     * @param string $string to parse.
90
     * @param string $tag Formattation tag to remove.
91
>>>>>>> master
92
     * @return string The string, modified if there are usernames. Otherwise $string.
93
     */
94
    public static function removeUsernameFormattation(string $string, string $tag) : string
95
    {
96
        // Check if there are usernames in string using regex
97
        if (preg_match_all('/(@\w+)/u', $string, $matches) != 0) {
98
            $usernamesArray = array_count_values($matches[0]);
99
100
            $usernames = array_keys($usernamesArray);
101
102
            // Count how many username we've got
103
            $count = count($usernames);
104
105
            // Delimitator to make the formattation start
106
            $delimitator_start = '<' . $tag . '>';
107
108
            // and to make it end
109
            $delimitator_end = '</' . $tag . '>';
110
111
            // For each username
112
            for ($i = 0; $i !== $count; $i++) {
113
                // Put the Delimitator_end before the username and the start one after it
114
                $string = str_replace($usernames[$i], $delimitator_end . $usernames[$i] . $delimitator_start, $string);
115
            }
116
        }
117
118
        return $string;
119
    }
120
121
    /** @} */
122
}
123