This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace WebservicesNl\Utils; |
||
4 | |||
5 | use Ddeboer\Transcoder\Transcoder; |
||
6 | |||
7 | /** |
||
8 | * Class StringUtils. |
||
9 | */ |
||
10 | class StringUtils |
||
11 | { |
||
12 | /** |
||
13 | * @param string $filename |
||
14 | * @param bool $lowercase |
||
15 | * |
||
16 | * @throws \InvalidArgumentException |
||
17 | * |
||
18 | * @return string |
||
19 | */ |
||
20 | 9 | public static function getFileExtension($filename, $lowercase = true) |
|
21 | { |
||
22 | 9 | if (false === is_string($filename)) { |
|
23 | 5 | throw new \InvalidArgumentException(sprintf('Filename must be a string, %s given', gettype($filename))); |
|
24 | } |
||
25 | 4 | $extension = pathinfo($filename, PATHINFO_EXTENSION); |
|
26 | |||
27 | 4 | return $lowercase ? strtolower($extension) : $extension; |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * Determine if a string ends with a particular sequence. |
||
32 | * |
||
33 | * @param string $haystack |
||
34 | * @param string $needle |
||
35 | * |
||
36 | * @throws \InvalidArgumentException |
||
37 | * |
||
38 | * @return bool |
||
39 | */ |
||
40 | 12 | public static function stringEndsWith($haystack, $needle) |
|
41 | { |
||
42 | 12 | if (!is_string($haystack) || !is_string($needle)) { |
|
43 | 4 | throw new \InvalidArgumentException('Not a string'); |
|
44 | } |
||
45 | |||
46 | 8 | return (strrpos($haystack, $needle) + strlen($needle)) === strlen($haystack); |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Determine if a string starts with a particular sequence. |
||
51 | * |
||
52 | * @param string $haystack |
||
53 | * @param string $needle |
||
54 | * |
||
55 | * @throws \InvalidArgumentException |
||
56 | * |
||
57 | * @return bool |
||
58 | */ |
||
59 | 12 | public static function stringStartsWith($haystack, $needle) |
|
60 | { |
||
61 | 12 | if (!is_string($haystack) || !is_string($needle)) { |
|
62 | 5 | throw new \InvalidArgumentException('Not a string'); |
|
63 | } |
||
64 | |||
65 | 8 | return strpos($haystack, $needle) === 0; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Remove the prefix from the provided string. |
||
70 | * |
||
71 | * @param string $haystack |
||
72 | * @param string $prefix |
||
73 | * |
||
74 | * @throws \InvalidArgumentException |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 12 | public static function removePrefix($haystack, $prefix) |
|
79 | { |
||
80 | 12 | if (!is_string($haystack) || !is_string($prefix)) { |
|
81 | 4 | throw new \InvalidArgumentException('Not a string'); |
|
82 | } |
||
83 | |||
84 | 8 | if (strpos($haystack, $prefix) === 0) { |
|
85 | 8 | $haystack = substr($haystack, strlen($prefix)); |
|
86 | 8 | } |
|
87 | |||
88 | 8 | return $haystack; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Convert strings with underscores into CamelCase (for getters and setters). |
||
93 | * |
||
94 | * @param string $string The string to convert |
||
95 | * @param bool $firstCharCaps camelCase or CamelCase |
||
96 | * |
||
97 | * @throws \Ddeboer\Transcoder\Exception\UnsupportedEncodingException |
||
98 | * @throws \Ddeboer\Transcoder\Exception\ExtensionMissingException |
||
99 | * @throws \InvalidArgumentException |
||
100 | * |
||
101 | * @return string The converted string |
||
102 | */ |
||
103 | 12 | public static function toCamelCase($string, $firstCharCaps = true) |
|
104 | { |
||
105 | 12 | if (!is_string($string)) { |
|
106 | 4 | throw new \InvalidArgumentException('Not a string'); |
|
107 | } |
||
108 | |||
109 | 8 | $transcoder = Transcoder::create(); |
|
110 | 8 | $string = $transcoder->transcode($string); |
|
111 | |||
112 | 8 | if ($firstCharCaps === true) { |
|
113 | 8 | $string = ucfirst($string); |
|
114 | 8 | } |
|
115 | |||
116 | 8 | return $string; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Internal function for toUnderscore. |
||
121 | * |
||
122 | * @param string $word |
||
123 | * |
||
124 | * @throws \InvalidArgumentException |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | 17 | private static function wordToUnderscored($word) |
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
129 | { |
||
130 | 17 | return strtolower( |
|
131 | 17 | trim( |
|
132 | 17 | preg_replace( |
|
133 | 17 | '~[^a-z^\d]+~i', |
|
134 | 17 | '_', |
|
135 | 17 | preg_replace( |
|
136 | 17 | '~([a-z\d+])([A-Z])~', |
|
137 | 17 | '\1_\2', |
|
138 | 17 | preg_replace('~([A-Z]+)([A-Z][a-z])~', '\1_\2', $word) |
|
139 | 17 | ) |
|
140 | 17 | ), |
|
141 | '_' |
||
142 | 17 | ) |
|
143 | 17 | ); |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Converts a string to underscore version. |
||
148 | * Also tries to filter out higher UTF-8 chars. |
||
149 | * |
||
150 | * @param string $string |
||
151 | * |
||
152 | * @throws \Ddeboer\Transcoder\Exception\UnsupportedEncodingException |
||
153 | * @throws \Ddeboer\Transcoder\Exception\ExtensionMissingException |
||
154 | * @throws \InvalidArgumentException |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | 22 | public static function toUnderscore($string) |
|
159 | { |
||
160 | 22 | if (!is_string($string)) { |
|
161 | 5 | throw new \InvalidArgumentException('Not a string'); |
|
162 | } |
||
163 | |||
164 | 17 | $transCoder = Transcoder::create('ASCII'); |
|
165 | 17 | $string = $transCoder->transcode(trim($string)); |
|
166 | 17 | $words = explode(' ', $string); |
|
167 | |||
168 | 17 | return implode('_', array_filter(array_map([static::class, 'wordToUnderscored'], $words))); |
|
169 | } |
||
170 | } |
||
171 |