Text   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 31.82%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 79
ccs 7
cts 22
cp 0.3182
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getHashtags() 0 9 2
A camelCase() 0 13 1
B removeUsernameFormattation() 0 26 3
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
     * See the following [StackOverflow thread](http://stackoverflow.com/questions/3060601/retrieve-all-hashtags-from-a-tweet-in-a-php-function) to learn more.
38
     * @param $string The string to check for hashtags.
39
     * @return array An array of valid hashtags, can be empty.
40
     */
41
    public static function getHashtags(string $string) : array
42
    {
43
        if (preg_match_all("/(#\w+)/u", $string, $matches) != 0) {
44
            $hashtagsArray = array_count_values($matches[0]);
45
            $hashtags = array_keys($hashtagsArray);
46
        }
47
48
        return $hashtags ?? [];
49
    }
50
51
    /**
52
     * \brief Convert a string into camelCase style.
53
     * \details Take a look [here](http://www.mendoweb.be/blog/php-convert-string-to-camelcase-string/)
54
     * to learn more.
55
     * @param string $str The string to convert.
56
     * @param array $noStrip
57
     * @return string $str The input string converted to camelCase.
58
     */
59 13
    public static function camelCase($str, array $noStrip = [])
60
    {
61
        // Non-alpha and non-numeric characters become spaces.
62 13
        $str = preg_replace('/[^a-z0-9' . implode("", $noStrip) . ']+/i', ' ', $str);
63 13
        $str = trim($str);
64
65
        // Make each word's letter uppercase.
66 13
        $str = ucwords($str);
67 13
        $str = str_replace(" ", "", $str);
68 13
        $str = lcfirst($str);
69
70 13
        return $str;
71
    }
72
73
    /**
74
     * \brief Remove HTML formattation from Telegram usernames.
75
     * \details Remove the $modificator html formattation from a message
76
     * containing Telegram usernames.
77
     * @param string $string to parse.
78
     * @param string $tag Formattation tag to remove.
79
     * @return string The string, modified if there are usernames. Otherwise $string.
80
     */
81
    public static function removeUsernameFormattation(string $string, string $tag) : string
82
    {
83
        // Check if there are usernames in string using regex
84
        if (preg_match_all('/(@\w+)/u', $string, $matches) != 0) {
85
            $usernamesArray = array_count_values($matches[0]);
86
87
            $usernames = array_keys($usernamesArray);
88
89
            // Count how many username we've got
90
            $count = count($usernames);
91
92
            // Delimitator to make the formattation start
93
            $delimitator_start = '<' . $tag . '>';
94
95
            // and to make it end
96
            $delimitator_end = '</' . $tag . '>';
97
98
            // For each username
99
            for ($i = 0; $i !== $count; $i++) {
100
                // Put the Delimitator_end before the username and the start one after it
101
                $string = str_replace($usernames[$i], $delimitator_end . $usernames[$i] . $delimitator_start, $string);
102
            }
103
        }
104
105
        return $string;
106
    }
107
108
    /** @} */
109
}
110