Skip to content

Commit dc7c031

Browse files
committed
implement rmLastWord(), rmFirstWord() and is_slug() methods
1 parent 1f81cc0 commit dc7c031

File tree

2 files changed

+91
-20
lines changed

2 files changed

+91
-20
lines changed

src/PhpStringHelpers.php

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

105105
if (!function_exists('translate')) {
106-
function translate(string $key, string $replace = '', string $dirName = 'en'): string
106+
function translate(string $key, string $replace = ''): string
107107
{
108-
return strHelpers::translate($key, $replace, $dirName);
108+
return strHelpers::translate($key, $replace);
109+
}
110+
}
111+
112+
if (!function_exists('translatePath')) {
113+
function translatePath(string $baseAppPath, string $dirName): string
114+
{
115+
return strHelpers::translatePath($baseAppPath, $dirName);
109116
}
110117
}
111118

@@ -356,3 +363,24 @@ function decrementBy(string $string, ?string $separator = null): string
356363
return strHelpers::decrementBy($string, $separator);
357364
}
358365
}
366+
367+
if (!function_exists('rmLastWord')) {
368+
function rmLastWord(string $string): string
369+
{
370+
return strHelpers::rmLastWord($string);
371+
}
372+
}
373+
374+
if (!function_exists('rmFirstWord')) {
375+
function rmFirstWord(string $string): string
376+
{
377+
return strHelpers::rmFirstWord($string);
378+
}
379+
}
380+
381+
if (!function_exists('is_slug')) {
382+
function is_slug(string $slug): bool
383+
{
384+
return strHelpers::is_slug($slug);
385+
}
386+
}

src/utility/StrUtility.php

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PhpStringHelpers\exceptions\UrlIsNotValidException;
1616
use PhpStringHelpers\exceptions\FileDoesNotExistsException;
1717
use PhpStringHelpers\exceptions\LanguageFileIsNotArrayException;
18+
use PHPUnit\Util\Json;
1819

1920
class StrUtility
2021
{
@@ -126,7 +127,7 @@ public static function toAdaCase(string $words): string
126127
*/
127128
public static function dotNotation(string $words): string
128129
{
129-
return preg_replace('/\s+/', '.', strtolower(self::regularWords($words)));
130+
return preg_replace('/\s+/', '.', self::regularWords($words));
130131
}
131132

132133
/**
@@ -179,28 +180,23 @@ public static function alternate(?string $string, string $alternate = null): str
179180

180181
/**
181182
* 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
184185
*
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
187188
* title array key in app.php file
188189
* @param string $replace
189190
* [optional]
190-
* @param string $dirName
191-
* [optional] default directory name is en
192191
* @return string
193192
* @throws FileDoesNotExistsException
194193
* @throws LanguageFileIsNotArrayException
195194
*/
196-
public static function translate(string $key, string $replace = '', string $dirName = 'en'): string
195+
public static function translate(string $key, string $replace = ''): string
197196
{
198197
$fileName = explode('.', $key);
199198
$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]);
204200

205201
$data = require_once $filePath;
206202

@@ -213,6 +209,19 @@ public static function translate(string $key, string $replace = '', string $dirN
213209
return html_entity_decode(htmlentities($data[$key]));
214210
}
215211

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+
216225
/**
217226
* wrap given string with wrapper param
218227
*
@@ -230,25 +239,24 @@ public static function wrapper(int|string $string, int|string $wrapper = '*'): s
230239
}
231240

232241
/**
233-
* return path of file from root of project
242+
* return path of file
234243
*
235244
* @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
237247
* @param string $pathExtension
238248
* [optional] declare file extension default is php file extension
239249
* @return string
240250
* @throws FileDoesNotExistsException
241251
*/
242252
public static function filePath(string $path, string $pathExtension = 'php'): string
243253
{
244-
245-
$path = $_SERVER['DOCUMENT_ROOT'] .
246-
str_replace('.', '/', implode('.', explode('.', $path)));
254+
$path = str_replace('.', '/', implode('.', explode('.', $path)));
247255

248256
$filePath = $path . '.' . strtolower($pathExtension);
249257

250258
if (!is_file($filePath) || !file_exists($filePath))
251-
throw new FileDoesNotExistsException('File Does Not Exist');
259+
throw new FileDoesNotExistsException($filePath . ' Does Not Exist');
252260

253261
return $filePath;
254262
}
@@ -757,4 +765,39 @@ public static function decrementBy(string $string, ?string $separator = null): s
757765

758766
return $stringPart . $separator . (string)$numberPart;
759767
}
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+
}
760803
}

0 commit comments

Comments
 (0)