Passed
Push — master ( 9b1eb8...9fc60f )
by Stephen
07:19
created

joinPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
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
 * Implode values that are not null.
164
 *
165
 * @param $glue
166
 * @param array $pieces
167
 * @return string|null
168
 */
169
function implodeFiltered($glue, array $pieces)
170
{
171
    if (! empty($pieces) && count($pieces) > 0) {
172
        return implode($glue, array_filter($pieces, function ($attr) {
173
            return isset($attr);
174
        }));
175
    } else {
176
        return null;
177
    }
178
}
179
180
/**
181
 * Concatenate directory and file path arrays.
182
 *
183
 * @param mixed ...$paths
184
 * @return string
185
 */
186
function joinPaths(...$paths)
187
{
188
    if (count($paths) === count($paths, COUNT_RECURSIVE)) {
189
        $paths = array_values($paths);
190
    }
191
192
    return implode(DIRECTORY_SEPARATOR, $paths);
193
}
194
195
/**
196
 * Retrieve a website domain without prefixes.
197
 *
198
 * @param string $url
199
 * @return string
200
 */
201
function extractWebsiteDomain(string $url)
202
{
203
    return isset($url) ? str_replace('www.', '', parse_url($url)['host']) : '';
204
}
205
206
/**
207
 * Only retrieve an integer value if the $value is greater than zero.
208
 *
209
 * Uses $return as the $return value instead of $value if not null.
210
 * Return original $value or $return if it is greater than zero.
211
 * Return $substitute string if it is less.
212
 *
213
 * @param $value
214
 * @param string $substitute
215
 * @param mixed $return
216
 * @return mixed|string
217
 */
218
function zero_replace($value, $substitute = '-', $return = null)
219
{
220
    // Return $substitute if the $value is not greater than the 0
221
    return $value > 0 ? ($return ?? (int) $value) : $substitute;
222
}
223
224
/**
225
 * Pretty implode an array by using a different glue for the last piece.
226
 *
227
 * Examples:
228
 *  - implodePretty([1, 2, 3]) --> '1, 2 & 3'
229
 *  - implodePretty([A, B, D]) --> 'A, B & D'
230
 *
231
 * @param array $pieces
232
 * @param string $glue
233
 * @param string $and
234
 * @return string
235
 */
236
function implodePretty(array $pieces, string $glue = ',', string $and = '&')
237
{
238
    // Pop off the last item so that it's
239
    // imploded with the $and char
240
    $last = array_pop($pieces);
241
242
    // Do nothing and return $last if there
243
    // are no remaining $pieces
244
    if (! count($pieces)) {
245
        return $last;
246
    }
247
248
    // Implode all bust the last $piece
249
    // using the $glue char
250
    $start = implode(whitespacePadBack(trim($glue)), $pieces);
251
252
    // Implode comma separated $start with
253
    // & separated $last
254
    return implode(whitespacePad(trim($and)), [$start, $last]);
255
}
256
257
/**
258
 * Generate a random password using uniqueid & md5 functions.
259
 *
260
 * @param int $length
261
 * @param string $prefix
262
 * @param string $hashFunction
263
 * @return string
264
 */
265
function randomPassword(int $length = 8, string $prefix = '', string $hashFunction = 'md5'): string
266
{
267
    return $prefix.truncateString($hashFunction(uniqid()), $length, '');
268
}
269