StringHelper   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 142
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A isEmpty() 0 4 1
A has() 0 4 2
A hasBeginning() 0 4 2
A hasEnding() 0 4 2
A toCamel() 0 4 1
A toSnake() 0 8 2
A toStudly() 0 4 1
A isJson() 0 6 1
A remove() 0 4 1
A removeFromBeginning() 0 4 2
A removeFromEnd() 0 4 1
A replace() 0 4 1
A replaceFromBeginning() 0 10 3
A replaceFromEnd() 0 4 1
A toSlug() 0 22 3
1
<?php
2
3
namespace Hgraca\Helper;
4
5
use Hgraca\Helper\Concept\HelperAbstract;
6
7
final class StringHelper extends HelperAbstract
8
{
9 2
    public static function isEmpty(string $value): bool
10
    {
11 2
        return $value === '';
12
    }
13
14
    /**
15
     * Returns true if this string starts with the given needle.
16
     *
17
     * @param string $needle
18
     * @param string $haystack
19
     *
20
     * @return bool
21
     */
22 5
    public static function has(string $needle, string $haystack): bool
23
    {
24 5
        return $needle === '' || strpos($haystack, $needle) !== false;
25
    }
26
27
    /**
28
     * Returns true if this string starts with the given needle.
29
     *
30
     * @param string $needle
31
     * @param string $haystack
32
     *
33
     * @return bool
34
     */
35 26
    public static function hasBeginning(string $needle, string $haystack): bool
36
    {
37 26
        return $needle === '' || strpos($haystack, $needle) === 0;
38
    }
39
40
    /**
41
     * Returns true if this string ends with the given needle.
42
     *
43
     * @param string $needle
44
     * @param string $haystack
45
     *
46
     * @return bool
47
     */
48 5
    public static function hasEnding(string $needle, string $haystack): bool
49
    {
50 5
        return $needle === '' || substr($haystack, -strlen($needle)) === $needle;
51
    }
52
53 1
    public static function toCamel(string $string, array $wordSeparators = ['-', '_', ' ']): string
54
    {
55 1
        return lcfirst(static::toStudly($string, $wordSeparators));
56
    }
57
58 3
    public static function toSnake(string $string, string $delimiter = '_', array $wordSeparators = ['-', '_', ' ']): string
59
    {
60 3
        $string = static::toStudly($string, $wordSeparators);
61
62 3
        $replace = '$1' . $delimiter . '$2';
63
64 3
        return ctype_lower($string) ? $string : strtolower(preg_replace('/(.)([A-Z])/', $replace, $string));
65
    }
66
67 5
    public static function toStudly(string $string, array $wordSeparators = ['-', '_', ' ']): string
68
    {
69 5
        return str_replace(' ', '', ucwords(str_replace($wordSeparators, ' ', $string)));
70
    }
71
72 2
    public static function isJson(string $string): bool
73
    {
74 2
        json_decode($string);
75
76 2
        return json_last_error() == JSON_ERROR_NONE;
77
    }
78
79 6
    public static function remove(string $search, string $haystack): string
80
    {
81 6
        return static::replace($search, '', $haystack);
82
    }
83
84 6
    public static function removeFromBeginning(string $search, string $haystack): string
85
    {
86 6
        return $search === '' ? $haystack : static::replaceFromBeginning($search, '', $haystack);
87
    }
88
89 6
    public static function removeFromEnd(string $search, string $haystack): string
90
    {
91 6
        return static::replaceFromEnd($search, '', $haystack);
92
    }
93
94 12
    public static function replace(string $search, string $replacement, string $haystack): string
95
    {
96 12
        return str_replace($search, $replacement, $haystack);
97
    }
98
99 24
    public static function replaceFromBeginning(string $search, string $replacement, string $haystack): string
100
    {
101 24
        if ($search === '' || !self::hasBeginning($search, $haystack)) {
102 16
            return $haystack;
103
        }
104
105 8
        $search = '/' . preg_quote($search, '/') . '/';
106
107 8
        return preg_replace($search, $replacement, $haystack, 1);
108
    }
109
110 12
    public static function replaceFromEnd(string $search, string $replacement, string $haystack): string
111
    {
112 12
        return strrev(self::replaceFromBeginning(strrev($search), strrev($replacement), strrev($haystack)));
113
    }
114
115
    /**
116
     * Given a string, it returns an SEO friendly slug.
117
     * IE:
118
     * echo slug('`~#this_is_%%a!{(title)}[]_of<>_p@o&$st*;,.\/-~!'); // this-is-a-title-of-post
119
     *
120
     * @param string $string
121
     * @param bool   $resultWithDashes   set this to false if you want output with spaces as a separator
122
     * @param bool   $inputIsEnglishOnly set this to false if your input contains non english words
123
     *
124
     * @return  string
125
     */
126 4
    public static function toSlug(string $string, bool $resultWithDashes = true, bool $inputIsEnglishOnly = true): string
127
    {
128 4
        $string = str_replace(['"', '+', "'"], ['', ' ', ''], urldecode($string));
129
130 4
        $string = preg_replace('/[\@\$\&]/', '', $string);
131 4
        if ($inputIsEnglishOnly === true) {
132 2
            $string = preg_replace('/[~`\!\@\#\$\%\^\&\*\(\)\_\=\+\/\?\>\<\,\[\]\:\;\|\\\]/', ' ', $string);
133
        } else {
134 2
            $string = preg_replace('/[^A-Za-z0-9\s\s+\.\)\(\{\}\-]/', ' ', $string);
135
        }
136
137 4
        $bad_brackets = ['(', ')', '{', '}'];
138 4
        $string = str_replace($bad_brackets, ' ', $string);
139 4
        $string = preg_replace('/\s+/', ' ', $string);
140 4
        $string = trim($string, ' .-');
141
142 4
        if ($resultWithDashes === true) {
143 2
            $string = str_replace(' ', '-', $string);
144
        }
145
146 4
        return strtolower(preg_replace('/-+/', '-', $string));
147
    }
148
}
149