1
1
<?php
2
2
3
3
/**
4
- * @Package: some useful php string utility methods
4
+ * @Package: Some Useful Php String Utility Methods
5
5
* @Class : StrUtility
6
6
* @Author : Nima jahan bakhshian / dvlpr1996 <nimajahanbakhshian@gmail.com>
7
7
* @URL : https://github.com/dvlpr1996
@@ -178,32 +178,37 @@ public function alternate(?string $string, string $alternate = null): string
178
178
}
179
179
180
180
/**
181
- * translation methods
181
+ * translation methods just for one level
182
182
* create lang folder in root of your project
183
183
* then create wrapper function or method based on documentation
184
184
*
185
185
* @param string $key |
186
186
* <your custom wrapper>('app.title') reference to ./lang/en/app.php and
187
187
* title array key in app.php file
188
- * @param string $replace
188
+ * @param string $alternative
189
189
* [optional]
190
190
* @return string
191
191
* @throws FileDoesNotExistsException
192
192
* @throws LanguageFileIsNotArrayException
193
193
*/
194
- public function translate (string $ key , string $ replace = '' ): string
194
+ public function translate (string $ key , string $ alternative = '' ): string | array
195
195
{
196
- $ fileName = explode ('. ' , $ key );
197
- $ key = $ fileName [1 ];
198
- $ filePath = $ this ->filePath ($ fileName [0 ]);
196
+ $ keys = explode ('. ' , $ key );
199
197
200
- $ data = require_once $ filePath ;
198
+ $ filePath = $ this ->filePath ($ keys [0 ]);
199
+
200
+ $ data = require $ filePath ;
201
+
202
+ if (!isset ($ keys [1 ]))
203
+ return $ data ;
204
+
205
+ $ key = $ keys [1 ];
201
206
202
207
if (!is_array ($ data ))
203
- throw new LanguageFileIsNotArrayException (" File data should be array " );
208
+ throw new LanguageFileIsNotArrayException (' File data should be array ' );
204
209
205
210
if (!key_exists ($ key , $ data ))
206
- return html_entity_decode (htmlentities ($ replace ));
211
+ return html_entity_decode (htmlentities ($ alternative ));
207
212
208
213
return html_entity_decode (htmlentities ($ data [$ key ]));
209
214
}
@@ -457,7 +462,7 @@ public function hexToRgb(string $color): ?string
457
462
if (strlen ($ hex ) !== 6 )
458
463
return null ;
459
464
460
- if (!preg_match (" /^[0-9ABCDEFabcdef\#]+$/i " , $ hex ))
465
+ if (!preg_match (' /^[0-9ABCDEFabcdef\#]+$/i ' , $ hex ))
461
466
return null ;
462
467
463
468
$ r = substr ($ hex , 0 , 2 );
@@ -480,7 +485,7 @@ public function rgbToHex(string $color): ?string
480
485
$ rgb = explode ('. ' , $ color );
481
486
482
487
foreach ($ rgb as $ value ) {
483
- if (!preg_match (" /^[0-9]+$/i " , $ value ))
488
+ if (!preg_match (' /^[0-9]+$/i ' , $ value ))
484
489
return null ;
485
490
}
486
491
@@ -502,7 +507,7 @@ public function generateAnchor(string|int $content, string $href): string
502
507
if (!filter_var ($ href , FILTER_VALIDATE_URL ))
503
508
throw new UrlIsNotValidException ('Url Is Not Valid ' );
504
509
505
- return " <a href= $ href> " . $ this ->clearString ($ content ) . '</a> ' ;
510
+ return ' <a href=$href> ' . $ this ->clearString ($ content ) . '</a> ' ;
506
511
}
507
512
508
513
/**
@@ -715,7 +720,7 @@ public function rmEndingNumbers(string $string): string
715
720
716
721
public function convertToUtf8 (string $ string ): string |bool
717
722
{
718
- $ converter = iconv (mb_detect_encoding ($ string , mb_detect_order (), true ), " UTF-8 " , $ string );
723
+ $ converter = iconv (mb_detect_encoding ($ string , mb_detect_order (), true ), ' UTF-8 ' , $ string );
719
724
return $ converter ? $ converter : false ;
720
725
}
721
726
0 commit comments