1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Helpers\Strings\Traits; |
4
|
|
|
|
5
|
|
|
trait StaticMethods |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Implode values that are not null. |
9
|
|
|
* |
10
|
|
|
* @param $glue |
11
|
|
|
* @param array $pieces |
12
|
|
|
* @return string|null |
13
|
|
|
*/ |
14
|
|
|
public static function implodeFiltered($glue, array $pieces): ?string |
15
|
|
|
{ |
16
|
|
|
if (! empty($pieces) && count($pieces) > 0) { |
17
|
|
|
return implode($glue, array_filter($pieces, function ($attr) { |
18
|
|
|
return isset($attr); |
19
|
|
|
})); |
20
|
|
|
} else { |
21
|
|
|
return null; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Concatenate directory and file path arrays. |
27
|
|
|
* |
28
|
|
|
* @param mixed ...$paths |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public static function joinPaths(...$paths): string |
32
|
|
|
{ |
33
|
|
|
if (count($paths) === count($paths, COUNT_RECURSIVE)) { |
34
|
|
|
$paths = array_values($paths); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return implode(DIRECTORY_SEPARATOR, $paths); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieve a website domain without prefixes. |
42
|
|
|
* |
43
|
|
|
* @param string $url |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public static function extractWebsiteDomain(string $url): string |
47
|
|
|
{ |
48
|
|
|
return str_replace('www.', '', parse_url($url)['host']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Retrieve a website domain from an email address. |
53
|
|
|
* |
54
|
|
|
* @param string $email |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public static function extractEmailDomain(string $email): string |
58
|
|
|
{ |
59
|
|
|
return array_reverse(explode('@', $email))[0]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Only retrieve an integer value if the $value is greater than zero. |
64
|
|
|
* |
65
|
|
|
* Uses $return as the $return value instead of $value if not null. |
66
|
|
|
* Return original $value or $return if it is greater than zero. |
67
|
|
|
* Return $substitute string if it is less. |
68
|
|
|
* |
69
|
|
|
* @param $value |
70
|
|
|
* @param string $substitute |
71
|
|
|
* @param mixed $return |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public static function zeroReplace($value, $substitute = '-', $return = null) |
75
|
|
|
{ |
76
|
|
|
// Return $substitute if the $value is not greater than the 0 |
77
|
|
|
return $value > 0 ? ($return ?? (int) $value) : $substitute; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Pretty implode an array by using a different glue for the last piece. |
82
|
|
|
* |
83
|
|
|
* Examples: |
84
|
|
|
* - implodePretty([1, 2, 3]) --> '1, 2 & 3' |
85
|
|
|
* - implodePretty([A, B, D]) --> 'A, B & D' |
86
|
|
|
* |
87
|
|
|
* @param array $pieces |
88
|
|
|
* @param string $glue |
89
|
|
|
* @param string $and |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public static function implodePretty(array $pieces, string $glue = ',', string $and = '&'): ?string |
93
|
|
|
{ |
94
|
|
|
// Pop off the last item so that it's |
95
|
|
|
// imploded with the $and char |
96
|
|
|
$last = array_pop($pieces); |
97
|
|
|
|
98
|
|
|
// Do nothing and return $last if there |
99
|
|
|
// are no remaining $pieces |
100
|
|
|
if (! count($pieces)) { |
101
|
|
|
return $last; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Implode all bust the last $piece |
105
|
|
|
// using the $glue char |
106
|
|
|
$start = implode(whitespacePadBack(trim($glue)), $pieces); |
107
|
|
|
|
108
|
|
|
// Implode comma separated $start with |
109
|
|
|
// & separated $last |
110
|
|
|
return implode(whitespacePad(trim($and)), [$start, $last]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|