Skip to content

Commit 5c7dab6

Browse files
committed
fix StrUtility::translate() method bug
1 parent ab516b1 commit 5c7dab6

File tree

6 files changed

+41
-28
lines changed

6 files changed

+41
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ vendor/
33
/.phpunit.cache
44
.phpunit.result.cache
55
lang/
6+
index.php

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `dvlpr1996/php-string-helpers` will be documented in this file
44

5+
## 2.0.1(2023-05-22)
6+
7+
fix StrUtility::translate() method bug
8+
59
## 2.0.0 (2023-01-18)
610

711
Change the implementation of package structure now user can use this library in 3 different ways

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# helpful set of PHP string helper functions & utilities class
1+
# Helpful Set Of Php String Helper Functions Utilities Class
22

33

44
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
@@ -112,21 +112,22 @@ StrUtility::alternate(?string $string, string $alternate = null): string
112112
- translation methods, for using this method you should create a wrapper function
113113
for example
114114

115-
```
116-
function <your_wrapper_function_name>(string $key, string $replace = '', string $dirName = 'en')
115+
```php
116+
function <your_wrapper_function_name>(string $key, string $alternative = '', string $dirName = 'en')
117117
{
118118
$BASE_PATH = // base (root) path of your project
119119

120120
$translatePath = StrUtility::translatePath($BASE_PATH, $dirName);
121-
return StrUtility::translate($translatePath . $key, $replace);
121+
return StrUtility::translate($translatePath . $key, $alternative);
122122
}
123123
```
124124

125125
< your_wrapper_function_name>('app.title') reference to lang/en/app.php and title array key in app.php
126126
file app.php must only return associative array.
127+
this translation methods only work for one level
127128

128129
```php
129-
StrUtility::translate(string $key, string $replace = '', string $dirName = 'en'): string
130+
StrUtility::translate(string $key, string $alternative = ''): string|array
130131
```
131132

132133
- wrap given string with wrapper param

src/PhpStringHelpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ function alternate(?string $string, ?string $alternate = null): string
103103
}
104104

105105
if (!function_exists('translate')) {
106-
function translate(string $key, string $replace = ''): string
106+
function translate(string $key, string $alternative = ''): string|array
107107
{
108-
return strHelpers::translate($key, $replace);
108+
return strHelpers::translate($key, $alternative);
109109
}
110110
}
111111

src/utility/StrUtility.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

tests/StrUtilityTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ public function testTranslateCanReturnStringValue()
212212
public function testTranslateCanThrowExceptionIfLangFileDoesNotExists()
213213
{
214214
$this->expectException(FileDoesNotExistsException::class);
215-
$string = strHelpersTest::translate('validation.first_name');
215+
216+
$translatePath = strHelpersTest::translatePath(realpath($this->basePath), 'en');
217+
strHelpersTest::translate($translatePath . 'validation.first_name');
216218
}
217219

218220
public function testTranslateCanThrowExceptionIfLangFileDoesNotInArrayType()
@@ -222,12 +224,12 @@ public function testTranslateCanThrowExceptionIfLangFileDoesNotInArrayType()
222224
strHelpersTest::translate($translatePath . 'auth.title');
223225
}
224226

225-
// public function testTranslateCanReturnReplaceParamIfGivenKeyDoesNotExists()
226-
// {
227-
// $translatePath = strHelpersTest::translatePath(realpath($this->basePath), 'en');
228-
// $string = strHelpersTest::translate($translatePath . 'app.site', 'replace text');
229-
// $this->assertEquals('replace text', $string);
230-
// }
227+
public function testTranslateCanReturnReplaceParamIfGivenKeyDoesNotExists()
228+
{
229+
$translatePath = strHelpersTest::translatePath(realpath($this->basePath), 'en');
230+
$string = strHelpersTest::translate($translatePath . 'app.site', 'replace text');
231+
$this->assertEquals('replace text', $string);
232+
}
231233

232234
public function testFilePathCanReturnStringValue()
233235
{

0 commit comments

Comments
 (0)