Skip to content

Commit 57ae0bc

Browse files
update
1 parent 5277ad4 commit 57ae0bc

File tree

4 files changed

+216
-77
lines changed

4 files changed

+216
-77
lines changed

Str.php

Lines changed: 164 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use Tamedevelopers\Support\Server;
88

9-
class Str{
10-
9+
class Str
10+
{
1111
/**
1212
* If the given value is not an array and not null, wrap it in one.
1313
*
@@ -61,9 +61,9 @@ static public function changeKeysFromArray($data, $fromKey, $toKey)
6161
{
6262
// always convert to an array
6363
$data = Server::toArray($data);
64-
64+
6565
// If you don't want to modify the original array and create a new one without 'id' columns:
66-
return array_map(function($data) use($fromKey, $toKey) {
66+
return array_map(function ($data) use ($fromKey, $toKey) {
6767
if (isset($data[$fromKey])) {
6868
$data[$toKey] = $data[$fromKey];
6969
unset($data[$fromKey]);
@@ -83,19 +83,19 @@ static public function removeKeysFromArray($data, ...$keys)
8383
{
8484
// always convert to an array
8585
$data = Server::toArray($data);
86-
86+
8787
// If you don't want to modify the original array and create a new one without 'id' columns:
88-
return array_map(function($data) use($keys) {
88+
return array_map(function ($data) use ($keys) {
8989
$keys = self::flattenValue($keys);
90-
foreach($keys as $key){
91-
if(isset($data[$key])){
90+
foreach ($keys as $key) {
91+
if (isset($data[$key])) {
9292
unset($data[$key]);
9393
}
9494
}
9595
return $data;
9696
}, $data);
9797
}
98-
98+
9999
/**
100100
* Convert array keys to specified key if available, else return the original array.
101101
*
@@ -137,10 +137,10 @@ static public function mergeBinding(array $bindings)
137137
{
138138
// Extract the values from the associative array
139139
$values = array_values($bindings);
140-
140+
141141
// Merge all the arrays into a single array
142142
$mergedBindings = array_merge(...$values);
143-
143+
144144
// Return the merged bindings
145145
return $mergedBindings;
146146
}
@@ -190,13 +190,13 @@ static public function exceptArray(array $array, $keys)
190190
if (!is_array($keys)) {
191191
$keys = [$keys];
192192
}
193-
193+
194194
// Use array_filter to keep only the elements with keys not present in $keys
195195
return array_filter($array, function ($key) use ($keys) {
196196
return !in_array($key, $keys);
197197
}, ARRAY_FILTER_USE_KEY);
198198
}
199-
199+
200200
/**
201201
* Replace the first occurrence of a substring in a string.
202202
*
@@ -219,7 +219,7 @@ static public function replaceFirst(string $search, string $replace, string $sub
219219
// Return the modified subject string
220220
return $subject;
221221
}
222-
222+
223223
/**
224224
* Replace the last occurrence of a substring in a string.
225225
*
@@ -243,6 +243,104 @@ static public function replaceLast(string $search, string $replace, string $subj
243243
return $subject;
244244
}
245245

246+
/**
247+
* Clean phone string
248+
*
249+
* @param string|null $phone
250+
* @param bool $allow
251+
* - [optional] to allow int format `+` (before number)
252+
*
253+
* @return string
254+
*/
255+
static public function phone($phone = null, ?bool $allow = true)
256+
{
257+
return Tame::cleanPhoneNumber($phone, $allow);
258+
}
259+
260+
/**
261+
* Masks characters in a string.
262+
*
263+
* @param string|null $str
264+
* - The string to be masked.
265+
*
266+
* @param int $length
267+
* - The desired length of the masked string. Default is 4.
268+
*
269+
* @param string $position
270+
* - The position to apply the mask: 'left', 'middle' or 'center', 'right'. Default is 'right'.
271+
*
272+
* @param string $mask
273+
* - The character used for masking. Default is '*'.
274+
*
275+
* @return string
276+
* - The masked string.
277+
*/
278+
static public function mask($str = null, ?int $length = 4, ?string $position = 'right', ?string $mask = '*')
279+
{
280+
return Tame::mask($str, $length, $position, $mask);
281+
}
282+
283+
/**
284+
* Decode entity html strings
285+
*
286+
* @param string|null $string
287+
* @return string
288+
*/
289+
static public function html($string = null)
290+
{
291+
return Tame::html($string);
292+
}
293+
294+
/**
295+
* Convert string to clean text without html tags
296+
*
297+
* @param string|null $string
298+
*
299+
* @return string
300+
* - strip all tags from string content
301+
*/
302+
static public function text($string = null)
303+
{
304+
return Tame::text($string);
305+
}
306+
307+
/**
308+
* Encrypt string
309+
*
310+
* @param string|null $string
311+
* @return string
312+
*/
313+
static public function encrypt($string = null)
314+
{
315+
return Tame::encryptStr($string);
316+
}
317+
318+
/**
319+
* Derypt string
320+
*
321+
* @param string|null $jsonString
322+
* @return mixed
323+
*/
324+
static public function decrypt($jsonString = null)
325+
{
326+
return Tame::decryptStr($jsonString);
327+
}
328+
329+
/**
330+
* Shorten String to Given Limit
331+
*
332+
* @param mixed $string
333+
* @param mixed $limit
334+
* @param mixed $replacer
335+
* [optional]
336+
*
337+
* @return string
338+
*/
339+
static public function shorten($string = null, $limit = 50, $replacer = '...')
340+
{
341+
return Tame::shortenString($string, $limit, $replacer);
342+
}
343+
246344
/**
247345
* Get the plural form of an English word.
248346
*
@@ -287,7 +385,7 @@ static public function pluralize($value = null)
287385
'/(mouse)$/i' => '$1mice',
288386
'/(deer)$/i' => '$1',
289387
'/(sheep)$/i' => '$1',
290-
];
388+
];
291389

292390
foreach ($rules as $pattern => $replacement) {
293391
if (preg_match($pattern, $value)) {
@@ -343,6 +441,17 @@ static public function random(int $length = 16)
343441
return $randomString;
344442
}
345443

444+
/**
445+
* Generate a string with a specified number of random words.
446+
*
447+
* @param int $wordCount
448+
* @return string
449+
*/
450+
static public function randomWords(int $wordCount)
451+
{
452+
return self::generateRandomWords($wordCount);
453+
}
454+
346455
/**
347456
* Generate a UUID (Universally Unique Identifier).
348457
*
@@ -390,12 +499,11 @@ static public function camel(string $value)
390499
$value = preg_replace('/[^a-z0-9]+/i', ' ', $value);
391500

392501
// Convert to camelCase
393-
$value = ucwords(trim($value));
502+
$value = ucwords(self::trim($value));
394503
$value = str_replace(' ', '', $value);
395504
$value = lcfirst($value);
396505

397506
return $value;
398-
399507
}
400508

401509
/**
@@ -408,7 +516,7 @@ static public function camel(string $value)
408516
static public function slug(string $value, string $separator = '-')
409517
{
410518
$value = preg_replace('/[^a-zA-Z0-9]+/', $separator, $value);
411-
$value = trim($value, $separator);
519+
$value = self::trim($value, $separator);
412520
$value = self::lower($value);
413521

414522
return $value;
@@ -473,11 +581,39 @@ static public function slugify(string $value, string $separator = '-')
473581
$value = preg_replace('/[^a-z0-9-]+/', $separator, $value);
474582

475583
// Remove leading and trailing separators
476-
$value = trim($value, $separator);
584+
$value = self::trim($value, $separator);
477585

478586
return $value;
479587
}
480588

589+
/**
590+
* Strip whitespace (or other characters) from the beginning and end of a string
591+
* @param string|null $string — The string that will be trimmed.
592+
*
593+
* @param string $characters
594+
* [optional] Optionally, the stripped characters can also be specified using the charlist parameter.
595+
* Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
596+
*
597+
* @return string
598+
*/
599+
static public function trim($string = null, string $characters = " \n\r\t\v\0")
600+
{
601+
return trim((string) $string, $characters);
602+
}
603+
604+
/**
605+
* Replace all occurrences of the search string with the replacement string
606+
* @param string|string[] $search
607+
* @param string|string[] $replace
608+
* @param string|string[] $subject
609+
*
610+
* @return string
611+
*/
612+
static public function replace(array|string $search, array|string $replace, array|string $subject)
613+
{
614+
return str_replace($search, $replace, $subject);
615+
}
616+
481617
/**
482618
* Convert a string to lowercase.
483619
* @param string|null $value
@@ -486,7 +622,7 @@ static public function slugify(string $value, string $separator = '-')
486622
*/
487623
static public function lower($value = null)
488624
{
489-
return trim(strtolower((string) $value));
625+
return strtolower(self::trim($value));
490626
}
491627

492628
/**
@@ -497,7 +633,7 @@ static public function lower($value = null)
497633
*/
498634
static public function upper($value = null)
499635
{
500-
return trim(strtoupper((string) $value));
636+
return strtoupper(self::trim($value));
501637
}
502638

503639
/**
@@ -644,9 +780,9 @@ static public function after(string $value, string $delimiter)
644780
{
645781
$pos = strpos($value, $delimiter);
646782

647-
return $pos !== false
648-
? substr($value, $pos + strlen($delimiter))
649-
: '';
783+
return $pos !== false
784+
? substr($value, $pos + strlen($delimiter))
785+
: '';
650786
}
651787

652788
/**
@@ -662,9 +798,9 @@ static public function between(string $value, string $start, string $end)
662798
$startPos = strpos($value, $start);
663799
$endPos = strpos($value, $end, $startPos + strlen($start));
664800

665-
return $startPos !== false && $endPos !== false
666-
? substr($value, $startPos + strlen($start), $endPos - $startPos - strlen($start))
667-
: '';
801+
return $startPos !== false && $endPos !== false
802+
? substr($value, $startPos + strlen($start), $endPos - $startPos - strlen($start))
803+
: '';
668804
}
669805

670806
/**
@@ -704,5 +840,4 @@ static public function padString(string $value, int $length, string $padChar = '
704840
{
705841
return str_pad($value, $length, $padChar, $padType);
706842
}
707-
708-
}
843+
}

0 commit comments

Comments
 (0)