11<?php
22
33/**
4- * @Package: some useful php string utility methods
4+ * @Package: Some Useful Php String Utility Methods
55 * @Class : StrUtility
66 * @Author : Nima jahan bakhshian / dvlpr1996 <nimajahanbakhshian@gmail.com>
77 * @URL : https://github.com/dvlpr1996
@@ -178,32 +178,37 @@ public function alternate(?string $string, string $alternate = null): string
178178 }
179179
180180 /**
181- * translation methods
181+ * translation methods just for one level
182182 * create lang folder in root of your project
183183 * then create wrapper function or method based on documentation
184184 *
185185 * @param string $key |
186186 * <your custom wrapper>('app.title') reference to ./lang/en/app.php and
187187 * title array key in app.php file
188- * @param string $replace
188+ * @param string $alternative
189189 * [optional]
190190 * @return string
191191 * @throws FileDoesNotExistsException
192192 * @throws LanguageFileIsNotArrayException
193193 */
194- public function translate (string $ key , string $ replace = '' ): string
194+ public function translate (string $ key , string $ alternative = '' ): string | array
195195 {
196- $ fileName = explode ('. ' , $ key );
197- $ key = $ fileName [1 ];
198- $ filePath = $ this ->filePath ($ fileName [0 ]);
196+ $ keys = explode ('. ' , $ key );
199197
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 ];
201206
202207 if (!is_array ($ data ))
203- throw new LanguageFileIsNotArrayException (" File data should be array " );
208+ throw new LanguageFileIsNotArrayException (' File data should be array ' );
204209
205210 if (!key_exists ($ key , $ data ))
206- return html_entity_decode (htmlentities ($ replace ));
211+ return html_entity_decode (htmlentities ($ alternative ));
207212
208213 return html_entity_decode (htmlentities ($ data [$ key ]));
209214 }
@@ -457,7 +462,7 @@ public function hexToRgb(string $color): ?string
457462 if (strlen ($ hex ) !== 6 )
458463 return null ;
459464
460- if (!preg_match (" /^[0-9ABCDEFabcdef\#]+$/i " , $ hex ))
465+ if (!preg_match (' /^[0-9ABCDEFabcdef\#]+$/i ' , $ hex ))
461466 return null ;
462467
463468 $ r = substr ($ hex , 0 , 2 );
@@ -480,7 +485,7 @@ public function rgbToHex(string $color): ?string
480485 $ rgb = explode ('. ' , $ color );
481486
482487 foreach ($ rgb as $ value ) {
483- if (!preg_match (" /^[0-9]+$/i " , $ value ))
488+ if (!preg_match (' /^[0-9]+$/i ' , $ value ))
484489 return null ;
485490 }
486491
@@ -502,7 +507,7 @@ public function generateAnchor(string|int $content, string $href): string
502507 if (!filter_var ($ href , FILTER_VALIDATE_URL ))
503508 throw new UrlIsNotValidException ('Url Is Not Valid ' );
504509
505- return " <a href= $ href> " . $ this ->clearString ($ content ) . '</a> ' ;
510+ return ' <a href=$href> ' . $ this ->clearString ($ content ) . '</a> ' ;
506511 }
507512
508513 /**
@@ -715,7 +720,7 @@ public function rmEndingNumbers(string $string): string
715720
716721 public function convertToUtf8 (string $ string ): string |bool
717722 {
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 );
719724 return $ converter ? $ converter : false ;
720725 }
721726
0 commit comments