Passed
Push — master ( a47c69...06a586 )
by Stephen
05:58
created

whitespaceReplace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Sfneal\Helpers\Strings\StringHelpers;
4
5
/**
6
 * Sanitize a file name to remove illegal chars.
7
 *
8
 * @param string $raw
9
 * @return string
10
 */
11
function sanitizeFileName(string $raw): string
12
{
13
    return (new StringHelpers($raw))->sanitizeFileName();
14
}
15
16
/**
17
 * Truncate a string to be no longer than $chars.
18
 *
19
 * @param string $string
20
 * @param int $chars
21
 * @param string $suffix
22
 * @return string
23
 */
24
function truncateString(string $string, int $chars = 100, string $suffix = '...'): string
25
{
26
    return (new StringHelpers($string))->truncate($chars, $suffix);
27
}
28
29
/**
30
 * Covert a camel cased string into a $char separated string.
31
 *
32
 * @param string $string
33
 * @param string $char
34
 * @return string
35
 */
36
function camelCaseConverter(string $string, $char = '-'): string
37
{
38
    return (new StringHelpers($string))->camelCaseConvert($char);
39
}
40
41
/**
42
 * Remove illegal characters from a string to create an ID.
43
 *
44
 * @param $string
45
 * @return string
46
 */
47
function str2id($string): string
48
{
49
    return (new StringHelpers($string))->id();
50
}
51
52
/**
53
 * Remove ' ', '-', '&' characters.
54
 *
55
 * @param $item
56
 * @return string
57
 */
58
function stripString($item): string
59
{
60
    return (new StringHelpers($item))->strip();
61
}
62
63
/**
64
 * Remove spaces and convert string to lowercase chars.
65
 *
66
 * @param $string
67
 * @return string|string[]
68
 */
69
function stringID($string): string
70
{
71
    return (new StringHelpers($string))->id();
72
}
73
74
/**
75
 * Check if a needle string is in a haystack string.
76
 *
77
 * @param string $haystack
78
 * @param string|array $needle
79
 * @param string $boolean
80
 * @return bool
81
 */
82
function inString(string $haystack, $needle, string $boolean = 'and'): bool
83
{
84
    return (new StringHelpers($haystack))->inString($needle, $boolean);
85
}
86
87
/**
88
 * Determine if the $string is a plain text list of values.
89
 *
90
 *  - if $string is a list, an array of values is returned
91
 *  - if $sting is a string, false is returned
92
 *
93
 * @param string $string
94
 * @param array|string[] $separators
95
 * @return bool|false|string[]
96
 */
97
function isListString(string $string, array $separators = [', ', ' '])
98
{
99
    return (new StringHelpers($string))->isListString($separators);
100
}
101
102
/**
103
 * Explode a string using an array of delimiters instead of a single string.
104
 *
105
 * @param string $string
106
 * @param array|string[] $delimiters
107
 * @param string $replacer
108
 * @return array
109
 */
110
function explodeMany(string $string,
111
                     array $delimiters = [',', '&', '/', '-'],
112
                     string $replacer = '***'): array
113
{
114
    return (new StringHelpers($string))->explodeMany($delimiters, $replacer);
115
}
116
117
/**
118
 * Add whitespace padding to a string.
119
 *
120
 * @param string $string
121
 * @param int $amount
122
 * @param string $pad
123
 * @param bool $front add whitespace to 'front' of the string
124
 * @param bool $back add whitespace to the 'back' of the string
125
 * @return string
126
 */
127
function whitespacePad(string $string,
128
                       int $amount = 1,
129
                       string $pad = ' ',
130
                       bool $front = true,
131
                       bool $back = true): string
132
{
133
    return (new StringHelpers($string))->whitespacePad($amount, $pad, $front, $back);
134
}
135
136
/**
137
 * Add whitespace padding to only the 'back' of the string.
138
 *
139
 * @param string $string
140
 * @param int $amount
141
 * @param string $pad
142
 * @return string
143
 */
144
function whitespacePadBack(string $string, int $amount = 1, string $pad = ' '): string
145
{
146
    return (new StringHelpers($string))->whitespacePadBack($amount, $pad);
147
}
148
149
/**
150
 * Add whitespace padding to only the 'back' of the string.
151
 *
152
 * @param string $string
153
 * @param int $amount
154
 * @param string $pad
155
 * @return string
156
 */
157
function whitespacePadFront(string $string, int $amount = 1, string $pad = ' '): string
158
{
159
    return (new StringHelpers($string))->whitespacePadFront($amount, $pad);
160
}
161
162
/**
163
 * Remove all whitespace (spaces) from a string
164
 *
165
 * @param string $string
166
 * @return string
167
 */
168
function whitespaceRemove(string $string): string
169
{
170
    return (new StringHelpers($string))->whitespaceRemove();
171
}
172
173
/**
174
 * Remove all whitespace (spaces) from a string & replace with another string
175
 *
176
 * @param string $string
177
 * @param string $replacement defaults to ''
178
 * @return string
179
 */
180
function whitespaceReplace(string $string, string $replacement = ''): string
181
{
182
    return (new StringHelpers($string))->whitespaceReplace($replacement);
183
}
184
185
/**
186
 * Implode values that are not null.
187
 *
188
 * @param $glue
189
 * @param array $pieces
190
 * @return string|null
191
 */
192
function implodeFiltered($glue, array $pieces)
193
{
194
    if (! empty($pieces) && count($pieces) > 0) {
195
        return implode($glue, array_filter($pieces, function ($attr) {
196
            return isset($attr);
197
        }));
198
    } else {
199
        return null;
200
    }
201
}
202
203
/**
204
 * Concatenate directory and file path arrays.
205
 *
206
 * @param mixed ...$paths
207
 * @return string
208
 */
209
function joinPaths(...$paths)
210
{
211
    if (count($paths) === count($paths, COUNT_RECURSIVE)) {
212
        $paths = array_values($paths);
213
    }
214
215
    return implode(DIRECTORY_SEPARATOR, $paths);
216
}
217
218
/**
219
 * Retrieve a website domain without prefixes.
220
 *
221
 * @param string $url
222
 * @return string
223
 */
224
function extractWebsiteDomain(string $url)
225
{
226
    return isset($url) ? str_replace('www.', '', parse_url($url)['host']) : '';
227
}
228
229
/**
230
 * Only retrieve an integer value if the $value is greater than zero.
231
 *
232
 * Uses $return as the $return value instead of $value if not null.
233
 * Return original $value or $return if it is greater than zero.
234
 * Return $substitute string if it is less.
235
 *
236
 * @param $value
237
 * @param string $substitute
238
 * @param mixed $return
239
 * @return mixed|string
240
 */
241
function zero_replace($value, $substitute = '-', $return = null)
242
{
243
    // Return $substitute if the $value is not greater than the 0
244
    return $value > 0 ? ($return ?? (int) $value) : $substitute;
245
}
246
247
/**
248
 * Pretty implode an array by using a different glue for the last piece.
249
 *
250
 * Examples:
251
 *  - implodePretty([1, 2, 3]) --> '1, 2 & 3'
252
 *  - implodePretty([A, B, D]) --> 'A, B & D'
253
 *
254
 * @param array $pieces
255
 * @param string $glue
256
 * @param string $and
257
 * @return string
258
 */
259
function implodePretty(array $pieces, string $glue = ',', string $and = '&')
260
{
261
    // Pop off the last item so that it's
262
    // imploded with the $and char
263
    $last = array_pop($pieces);
264
265
    // Do nothing and return $last if there
266
    // are no remaining $pieces
267
    if (! count($pieces)) {
268
        return $last;
269
    }
270
271
    // Implode all bust the last $piece
272
    // using the $glue char
273
    $start = implode(whitespacePadBack(trim($glue)), $pieces);
274
275
    // Implode comma separated $start with
276
    // & separated $last
277
    return implode(whitespacePad(trim($and)), [$start, $last]);
278
}
279
280
/**
281
 * Generate a random password using uniqueid & md5 functions.
282
 *
283
 * @param int $length
284
 * @param string $prefix
285
 * @param string $hashFunction
286
 * @return string
287
 */
288
function randomPassword(int $length = 8, string $prefix = '', string $hashFunction = 'md5'): string
289
{
290
    return $prefix.truncateString($hashFunction(uniqid()), $length, '');
291
}
292