Issues (15)

helpers/Str.php (2 issues)

1
<?php
2
3
class Str extends Prefab
4
{
5
    protected static $snakeCache = [];
6
    protected static $camelCache = [];
7
    protected static $studlyCache = [];
8
    
9
    public static function after($subject, $search)
10
    {
11
        return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
12
    }
13
14
    public static function before($subject, $search)
15
    {
16
        return $search === '' ? $subject : explode($search, $subject)[0];
17
    }
18
19
    public static function camel($value)
20
    {
21
        if (isset(static::$camelCache[$value])) {
22
            return static::$camelCache[$value];
23
        }
24
25
        return static::$camelCache[$value] = lcfirst(static::studly($value));
26
    }
27
28
    public static function contains($haystack, $needles)
29
    {
30
        foreach ((array) $needles as $needle) {
31
            if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
32
                return true;
33
            }
34
        }
35
36
        return false;
37
    }
38
39 View Code Duplication
    public static function endsWith($haystack, $needles)
40
    {
41
        foreach ((array) $needles as $needle) {
42
            if (substr($haystack, -strlen($needle)) === (string) $needle) {
43
                return true;
44
            }
45
        }
46
47
        return false;
48
    }
49
50
    public static function finish($value, $cap)
51
    {
52
        $quoted = preg_quote($cap, '/');
53
54
        return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap;
55
    }
56
57
    public static function is($pattern, $value)
58
    {
59
        $patterns = is_array($pattern) ? $pattern : (array) $pattern;
60
61
        if (empty($patterns)) {
62
            return false;
63
        }
64
65
        foreach ($patterns as $pattern) {
66
            if ($pattern == $value) {
67
                return true;
68
            }
69
70
            $pattern = preg_quote($pattern, '#');
71
            $pattern = str_replace('\*', '.*', $pattern);
72
73
            if (preg_match('#^'.$pattern.'\z#u', $value) === 1) {
74
                return true;
75
            }
76
        }
77
78
        return false;
79
    }
80
81
    public static function kebab($value)
82
    {
83
        return static::snake($value, '-');
84
    }
85
86
    public static function length($value, $encoding = null)
87
    {
88
        if ($encoding) {
89
            return mb_strlen($value, $encoding);
90
        }
91
92
        return mb_strlen($value);
93
    }
94
95
    public static function limit($value, $limit = 100, $end = '...')
96
    {
97
        if (mb_strwidth($value, 'UTF-8') <= $limit) {
98
            return $value;
99
        }
100
101
        return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
102
    }
103
104
    public static function lower($value)
105
    {
106
        return mb_strtolower($value, 'UTF-8');
107
    }
108
109
    public static function words($value, $words = 100, $end = '...')
110
    {
111
        preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
112
113
        if (!isset($matches[0]) || static::length($value) === static::length($matches[0])) {
114
            return $value;
115
        }
116
117
        return rtrim($matches[0]).$end;
118
    }
119
120
    public static function parseCallback($callback, $default = null)
121
    {
122
        return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default];
123
    }
124
125
    public static function plural($value, $count = 2)
126
    {
127
        return Pluralizer::plural($value, $count);
0 ignored issues
show
The type Pluralizer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
128
    }
129
130
    public static function random($length = 16)
131
    {
132
        $string = '';
133
134
        while (($len = strlen($string)) < $length) {
135
            $size = $length - $len;
136
            $bytes = random_bytes($size);
137
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
138
        }
139
140
        return $string;
141
    }
142
143
    public static function replaceArray($search, array $replace, $subject)
144
    {
145
        foreach ($replace as $value) {
146
            $subject = static::replaceFirst($search, $value, $subject);
147
        }
148
149
        return $subject;
150
    }
151
152 View Code Duplication
    public static function replaceFirst($search, $replace, $subject)
153
    {
154
        if ($search == '') {
155
            return $subject;
156
        }
157
158
        $position = strpos($subject, $search);
159
160
        if ($position !== false) {
161
            return substr_replace($subject, $replace, $position, strlen($search));
162
        }
163
164
        return $subject;
165
    }
166
167 View Code Duplication
    public static function replaceLast($search, $replace, $subject)
168
    {
169
        $position = strrpos($subject, $search);
170
171
        if ($position !== false) {
172
            return substr_replace($subject, $replace, $position, strlen($search));
173
        }
174
175
        return $subject;
176
    }
177
178
    public static function start($value, $prefix)
179
    {
180
        $quoted = preg_quote($prefix, '/');
181
        return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value);
182
    }
183
184
    public static function upper($value)
185
    {
186
        return mb_strtoupper($value, 'UTF-8');
187
    }
188
189
    public static function title($value)
190
    {
191
        return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
192
    }
193
194
    public static function singular($value)
195
    {
196
        return Pluralizer::singular($value);
197
    }
198
199
    public static function slug($title, $separator = '-', $language = 'en')
200
    {
201
        $title = static::ascii($title, $language);
0 ignored issues
show
The method ascii() does not exist on Str. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
        $title = static::/** @scrutinizer ignore-call */ ascii($title, $language);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
202
203
        $flip = $separator == '-' ? '_' : '-';
204
205
        $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
206
        $title = str_replace('@', $separator.'at'.$separator, $title);
207
        $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
208
        $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
209
210
        return trim($title, $separator);
211
    }
212
213
    public static function snake($value, $delimiter = '_')
214
    {
215
        $key = $value;
216
217
        if (isset(static::$snakeCache[$key][$delimiter])) {
218
            return static::$snakeCache[$key][$delimiter];
219
        }
220
221
        if (!ctype_lower($value)) {
222
            $value = preg_replace('/\s+/u', '', ucwords($value));
223
224
            $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
225
        }
226
227
        return static::$snakeCache[$key][$delimiter] = $value;
228
    }
229
230 View Code Duplication
    public static function startsWith($haystack, $needles)
231
    {
232
        foreach ((array) $needles as $needle) {
233
            if ($needle !== '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
234
                return true;
235
            }
236
        }
237
238
        return false;
239
    }
240
241
    public static function studly($value)
242
    {
243
        $key = $value;
244
245
        if (isset(static::$studlyCache[$key])) {
246
            return static::$studlyCache[$key];
247
        }
248
249
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
250
251
        return static::$studlyCache[$key] = str_replace(' ', '', $value);
252
    }
253
254
    public static function substr($string, $start, $length = null)
255
    {
256
        return mb_substr($string, $start, $length, 'UTF-8');
257
    }
258
259
    public static function ucfirst($string)
260
    {
261
        return static::upper(static::substr($string, 0, 1)).static::substr($string, 1);
262
    }
263
}
264