15
15
use PhpStringHelpers \exceptions \UrlIsNotValidException ;
16
16
use PhpStringHelpers \exceptions \FileDoesNotExistsException ;
17
17
use PhpStringHelpers \exceptions \LanguageFileIsNotArrayException ;
18
+ use PHPUnit \Util \Json ;
18
19
19
20
class StrUtility
20
21
{
@@ -126,7 +127,7 @@ public static function toAdaCase(string $words): string
126
127
*/
127
128
public static function dotNotation (string $ words ): string
128
129
{
129
- return preg_replace ('/\s+/ ' , '. ' , strtolower ( self ::regularWords ($ words) ));
130
+ return preg_replace ('/\s+/ ' , '. ' , self ::regularWords ($ words ));
130
131
}
131
132
132
133
/**
@@ -179,28 +180,23 @@ public static function alternate(?string $string, string $alternate = null): str
179
180
180
181
/**
181
182
* translation methods
182
- * must create lang folder in root of project
183
- * lang/en
183
+ * create lang folder in root of your project
184
+ * then create wrapper function or method based on documentation
184
185
*
185
- * @param string $key fileName.keyName |
186
- * translate ('app.title') reference to lang/en/app.php and
186
+ * @param string $key |
187
+ * <your custom wrapper> ('app.title') reference to ./ lang/en/app.php and
187
188
* title array key in app.php file
188
189
* @param string $replace
189
190
* [optional]
190
- * @param string $dirName
191
- * [optional] default directory name is en
192
191
* @return string
193
192
* @throws FileDoesNotExistsException
194
193
* @throws LanguageFileIsNotArrayException
195
194
*/
196
- public static function translate (string $ key , string $ replace = '' , string $ dirName = ' en ' ): string
195
+ public static function translate (string $ key , string $ replace = '' ): string
197
196
{
198
197
$ fileName = explode ('. ' , $ key );
199
198
$ key = $ fileName [1 ];
200
- $ filePath = self ::filePath ('lang. ' . $ dirName . '. ' . $ fileName [0 ]);
201
-
202
- if (!is_file ($ filePath ) || !file_exists ($ filePath ))
203
- throw new FileDoesNotExistsException ("File Does Not Exist " );
199
+ $ filePath = self ::filePath ($ fileName [0 ]);
204
200
205
201
$ data = require_once $ filePath ;
206
202
@@ -213,6 +209,19 @@ public static function translate(string $key, string $replace = '', string $dirN
213
209
return html_entity_decode (htmlentities ($ data [$ key ]));
214
210
}
215
211
212
+ /**
213
+ * translate Path resolver
214
+ *
215
+ * @param string $baseAppPath
216
+ * base path of your app
217
+ * @param string $dirName
218
+ * @return string
219
+ */
220
+ public static function translatePath (string $ baseAppPath , string $ dirName ): string
221
+ {
222
+ return $ baseAppPath . '/lang/ ' . $ dirName . '/ ' ;
223
+ }
224
+
216
225
/**
217
226
* wrap given string with wrapper param
218
227
*
@@ -230,25 +239,24 @@ public static function wrapper(int|string $string, int|string $wrapper = '*'): s
230
239
}
231
240
232
241
/**
233
- * return path of file from root of project
242
+ * return path of file
234
243
*
235
244
* @param string $path
236
- * path to the file like foo.bar.baz
245
+ * path to the file from root of the project
246
+ * accept dot notation case
237
247
* @param string $pathExtension
238
248
* [optional] declare file extension default is php file extension
239
249
* @return string
240
250
* @throws FileDoesNotExistsException
241
251
*/
242
252
public static function filePath (string $ path , string $ pathExtension = 'php ' ): string
243
253
{
244
-
245
- $ path = $ _SERVER ['DOCUMENT_ROOT ' ] .
246
- str_replace ('. ' , '/ ' , implode ('. ' , explode ('. ' , $ path )));
254
+ $ path = str_replace ('. ' , '/ ' , implode ('. ' , explode ('. ' , $ path )));
247
255
248
256
$ filePath = $ path . '. ' . strtolower ($ pathExtension );
249
257
250
258
if (!is_file ($ filePath ) || !file_exists ($ filePath ))
251
- throw new FileDoesNotExistsException (' File Does Not Exist ' );
259
+ throw new FileDoesNotExistsException ($ filePath . ' Does Not Exist ' );
252
260
253
261
return $ filePath ;
254
262
}
@@ -757,4 +765,39 @@ public static function decrementBy(string $string, ?string $separator = null): s
757
765
758
766
return $ stringPart . $ separator . (string )$ numberPart ;
759
767
}
768
+
769
+ /**
770
+ * remove last word from given string
771
+ *
772
+ * @param string $string
773
+ * @return string
774
+ */
775
+ public static function rmLastWord (string $ string ): string
776
+ {
777
+ return preg_replace ('/\W\w+\s*(\W*)$/ ' , '$1 ' , trim ($ string ));
778
+ }
779
+
780
+ /**
781
+ * remove first word from given string
782
+ *
783
+ * @param string $string
784
+ * @return string
785
+ */
786
+ public static function rmFirstWord (string $ string ): string
787
+ {
788
+ return preg_replace ('/^(\w+\s)/ ' , '' , trim ($ string ));
789
+ }
790
+
791
+ /**
792
+ * find whether the type of a given string is slug
793
+ *
794
+ * @param string $slug
795
+ * @return bool
796
+ */
797
+ public static function is_slug (string $ slug ): bool
798
+ {
799
+ if (!preg_match ('/^[\w\d][-\w\d]*$/ ' , trim ($ slug )))
800
+ return false ;
801
+ return true ;
802
+ }
760
803
}
0 commit comments