1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\HelperTraits; |
4
|
|
|
|
5
|
|
|
trait Str |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @param string $string |
9
|
|
|
* @return string |
10
|
|
|
*/ |
11
|
4 |
|
public function camelCase($string) |
12
|
|
|
{ |
13
|
4 |
|
$string = ucwords(str_replace(['-', '_'], ' ', trim($string))); |
14
|
4 |
|
return str_replace(' ', '', $string); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $name |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
1 |
|
public function convertPathToId($path, $prefix = '') |
22
|
|
|
{ |
23
|
1 |
|
return str_replace(['[', ']'], ['-', ''], $this->convertPathToName($path, $prefix)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $path |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
2 |
|
public function convertPathToName($path, $prefix = '') |
31
|
|
|
{ |
32
|
2 |
|
$levels = explode('.', $path); |
33
|
2 |
|
return array_reduce($levels, function ($result, $value) { |
34
|
2 |
|
return $result .= '['.$value.']'; |
35
|
2 |
|
}, $prefix); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $string |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
8 |
|
public function dashCase($string) |
43
|
|
|
{ |
44
|
8 |
|
return str_replace('_', '-', $this->snakeCase($string)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $needle |
49
|
|
|
* @param string $haystack |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
1 |
|
public function endsWith($needle, $haystack) |
53
|
|
|
{ |
54
|
1 |
|
$length = strlen($needle); |
55
|
1 |
|
return 0 != $length |
56
|
1 |
|
? substr($haystack, -$length) === $needle |
57
|
1 |
|
: true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $prefix |
62
|
|
|
* @param string $string |
63
|
|
|
* @param string|null $trim |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
2 |
|
public function prefix($prefix, $string, $trim = null) |
67
|
|
|
{ |
68
|
2 |
|
if (null === $trim) { |
69
|
2 |
|
$trim = $prefix; |
70
|
|
|
} |
71
|
2 |
|
return $prefix.trim($this->removePrefix($trim, $string)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $prefix |
76
|
|
|
* @param string $string |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
8 |
|
public function removePrefix($prefix, $string) |
80
|
|
|
{ |
81
|
8 |
|
return $this->startsWith($prefix, $string) |
82
|
7 |
|
? substr($string, strlen($prefix)) |
83
|
8 |
|
: $string; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $string |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
9 |
|
public function snakeCase($string) |
91
|
|
|
{ |
92
|
9 |
|
if (!ctype_lower($string)) { |
93
|
9 |
|
$string = preg_replace('/\s+/u', '', $string); |
94
|
9 |
|
$string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
95
|
9 |
|
$string = function_exists('mb_strtolower') |
96
|
9 |
|
? mb_strtolower($string, 'UTF-8') |
97
|
9 |
|
: strtolower($string); |
98
|
|
|
} |
99
|
9 |
|
return str_replace('-', '_', $string); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $needle |
104
|
|
|
* @param string $haystack |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
9 |
|
public function startsWith($needle, $haystack) |
108
|
|
|
{ |
109
|
9 |
|
return substr($haystack, 0, strlen($needle)) === $needle; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|