Skip to content

Commit 0528b4d

Browse files
update
1 parent e255fc9 commit 0528b4d

File tree

9 files changed

+368
-834
lines changed

9 files changed

+368
-834
lines changed

Capsule/TimeHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static public function setPassedDate($date = null)
115115
}
116116

117117
if (is_numeric($date)) {
118-
$date = date('M d Y', (int) $date);
118+
$date = date('M d Y h:ia', (int) $date);
119119
}
120120

121121
// if instance of Carbon

Collections/Collection.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,50 @@ public function merge($items)
345345
return new static(array_merge($this->items, $items));
346346
}
347347

348+
/**
349+
* Needed key from items
350+
* @param array|string|null $keys of input
351+
*
352+
* @return static
353+
*/
354+
public function only(...$keys)
355+
{
356+
$keys = Str::flattenValue($keys);
357+
358+
$data = [];
359+
360+
foreach($keys as $key){
361+
if(in_array($key, array_keys($this->items))){
362+
$data[$key] = $this->items[$key];
363+
}
364+
}
365+
366+
return new static(
367+
$data
368+
);
369+
}
370+
371+
/**
372+
* Remove key from items
373+
* @param array|string|null $keys of input
374+
*
375+
* @return static
376+
*/
377+
public function except(...$keys)
378+
{
379+
$keys = Str::flattenValue($keys);
380+
381+
foreach($keys as $key){
382+
if(in_array($key, array_keys($this->items))){
383+
unset($this->items[$key]);
384+
}
385+
}
386+
387+
return new static(
388+
$this->items
389+
);
390+
}
391+
348392
/**
349393
* Chunk the collection into arrays with a specified size.
350394
*

NumberToWords.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function __cents($cents = false)
9898
*/
9999
public function __iso($code = null)
100100
{
101-
$this->currencyData = self::getCurrencyValue($code);
101+
$this->currencyData = self::getCurrencyByIso3($code);
102102

103103
return $this;
104104
}
@@ -362,8 +362,8 @@ static private function convertChunkToText($number)
362362
*/
363363
static private function removeCurrencyNames($value)
364364
{
365-
// Get all currency and cent names from the CurrencyNames() method
366-
$currencyData = self::CurrencyNames();
365+
// Get all currency and cent names from the allCurrency() method
366+
$currencyData = self::allCurrency();
367367

368368
// Loop through each currency and remove the corresponding names from the value string
369369
foreach ($currencyData as $currency) {

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Support Package For PHP and Laravel
4040
* [shorten](#shorten)
4141
* [random](#random)
4242
* [formatString](#formatString)
43+
* [formatOnlyString](#formatOnlyString)
4344
* [encrypt](#encrypt)
4445
* [decrypt](#decrypt)
4546
* [bindings](#bindings)
@@ -77,6 +78,7 @@ Support Package For PHP and Laravel
7778
* [date](#date)
7879
* [today](#today)
7980
* [yesterday](#yesterday)
81+
* [createFromFormat](#createFromFormat)
8082
* [timestamp](#timestamp)
8183
* [toJsTimer](#toJsTimer)
8284
* [diff](#diff)
@@ -286,20 +288,20 @@ Tame()->unlink(base_path('path/to/directory/avatar.png'), 'default.png');
286288
| Params | Description |
287289
|---------------|-----------------------------------------------------------------------------------------------|
288290
| `$str` | The string to be masked. |
289-
| `$length` | The number of characters to mask (default is 4). |
291+
| `$length` | The number of visible characters. Default is 4. |
290292
| `$position` | The position to apply the mask: `'left'`, `'center'`, or `'right'` (default is `'right'`). |
291293
| `$mask` | The character used for masking (default is `*`). |
292294

293295
#### Example:
294296
```
295297
Tame()->mask('[email protected]', 4, 'left');
296-
// Output: "****ple@email.com"
298+
// Output: "exam***@email.com"
297299
298300
Tame()->mask('[email protected]', 4, 'right');
299-
// Output: "exa****@email.com"
301+
// Output: "e***mple@email.com"
300302
301303
Tame()->mask('shortstring', 4, 'center');
302-
// Output: "sho****ing"
304+
// Output: "sh*******ng"
303305
```
304306

305307
### imageToBase64
@@ -592,6 +594,15 @@ $time->today();
592594
$time->yesterday();
593595
```
594596

597+
### createFromFormat
598+
- Accepts two parameter [date, format]
599+
- only [date] is mandatory and returns the Time(object)
600+
601+
```
602+
$time->createFromFormat('24 Jan 2025 14:00:00', 'm/d/Y h:ia');
603+
// object(Tamedevelopers\Support\Time)
604+
```
605+
595606
### timestamp
596607
- Accepts two parameter [date, format]
597608
- only [date] is mandatory and returns formated timestamp

Str.php

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,15 @@ static public function exceptArray(array $array, $keys)
286286
*
287287
* @param string $search The substring to search for.
288288
* @param string $replace The replacement substring.
289-
* @param string $subject The original string.
289+
* @param string|null $subject The original string.
290290
* @return string
291291
* - The modified string.
292292
*/
293-
static public function replaceFirst(string $search, string $replace, string $subject)
293+
static public function replaceFirst(string $search, string $replace, $subject = null)
294294
{
295+
$subject = self::replaceSubject($subject);
296+
$replace = self::replaceSubject($replace);
297+
295298
// Find the position of the first occurrence of the search string
296299
$pos = strpos($subject, $search);
297300

@@ -309,12 +312,15 @@ static public function replaceFirst(string $search, string $replace, string $sub
309312
*
310313
* @param string $search The substring to search for.
311314
* @param string $replace The replacement substring.
312-
* @param string $subject The original string.
315+
* @param string|null $subject The original string.
313316
* @return string
314317
* - The modified string.
315318
*/
316-
static public function replaceLast(string $search, string $replace, string $subject)
319+
static public function replaceLast(string $search, string $replace, $subject = null)
317320
{
321+
$subject = self::replaceSubject($subject);
322+
$replace = self::replaceSubject($replace);
323+
318324
// Find the position of the first occurrence of the search string
319325
$pos = strrpos($subject, $search);
320326

@@ -342,6 +348,25 @@ static public function formatString($string, $number = 4, $seperator = '-')
342348
return self::replace(' ', '', $string);
343349
}
344350

351+
/**
352+
* Format String Once with Separator
353+
*
354+
* @param string $string
355+
* @param int $number
356+
* @param string $separator
357+
* @return string
358+
*/
359+
static public function formatOnlyString($string, $number = 4, $separator = '-')
360+
{
361+
$string = self::trim($string);
362+
363+
if (strlen($string) > $number) {
364+
$string = substr_replace($string, $separator, $number, 0);
365+
}
366+
367+
return self::replace(' ', '', $string);
368+
}
369+
345370
/**
346371
* Clean phone string
347372
*
@@ -363,7 +388,7 @@ static public function phone($phone = null, ?bool $allow = true)
363388
* - The string to be masked.
364389
*
365390
* @param int $length
366-
* - The desired length of the masked string. Default is 4.
391+
* - The number of visible characters. Default is 4.
367392
*
368393
* @param string $position
369394
* - The position to apply the mask: 'left', 'middle' or 'center', 'right'. Default is 'right'.
@@ -702,14 +727,17 @@ static public function trim($string = null, string $characters = " \n\r\t\v\0")
702727

703728
/**
704729
* Replace all occurrences of the search string with the replacement string
705-
* @param string|string[] $search
706-
* @param string|string[] $replace
707-
* @param string|string[] $subject
730+
* @param array|string $search
731+
* @param array|string $replace
732+
* @param string|null $subject
708733
*
709734
* @return string
710735
*/
711-
static public function replace(array|string $search, array|string $replace, array|string $subject)
736+
static public function replace($search, $replace, $subject = null)
712737
{
738+
$subject = self::replaceSubject($subject);
739+
$replace = self::replaceSubject($replace);
740+
713741
return str_replace($search, $replace, $subject);
714742
}
715743

@@ -950,6 +978,17 @@ static public function padString(string $value, int $length, string $padChar = '
950978
{
951979
return str_pad($value, $length, $padChar, $padType);
952980
}
981+
982+
/**
983+
* Replace Subject
984+
*
985+
* @param mixed $subject
986+
* @return void
987+
*/
988+
static private function replaceSubject($subject = null)
989+
{
990+
return is_null($subject) ? (string) $subject : $subject;
991+
}
953992

954993
/**
955994
* Convert the case of a string based on the specified type.

Tame.php

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,16 +1094,16 @@ static public function imageToBase64($path = null, $url = false)
10941094
}
10951095

10961096
/**
1097-
* Masks characters in a string.
1097+
* Masks characters in a string while keeping a specified number of visible characters.
10981098
*
10991099
* @param string|null $str
11001100
* - The string to be masked.
11011101
*
11021102
* @param int $length
1103-
* - The desired length of the masked string. Default is 4.
1103+
* - The number of visible characters. Default is 4.
11041104
*
11051105
* @param string $position
1106-
* - The position to apply the mask: 'left', 'center', 'right'. Default is 'right'.
1106+
* - The position to keep visible: 'left', 'center', 'right'. Default is 'right'.
11071107
*
11081108
* @param string $mask
11091109
* - The character used for masking. Default is '*'.
@@ -1118,69 +1118,52 @@ static public function mask($str = null, ?int $length = 4, ?string $position = '
11181118
return $str;
11191119
}
11201120

1121-
// trim string
1121+
// Trim string and position input
11221122
$str = Str::trim($str);
11231123
$position = Str::trim($position);
11241124

11251125
// Get the length of the string
11261126
$strLength = mb_strlen($str, 'UTF-8');
11271127

1128+
// If length is greater than or equal to the string length, return the original string (nothing to mask)
1129+
if ($length >= $strLength) {
1130+
return $str;
1131+
}
1132+
1133+
// Calculate the number of masked characters
1134+
$maskedLength = max(0, $strLength - $length);
1135+
11281136
// Check if it's an email by finding the last occurrence of "@"
11291137
$atPosition = mb_strrpos($str, "@", 0, 'UTF-8');
1130-
1131-
// Check if it's an actual email
11321138
$isEmail = self::emailValidator($str, false, false);
11331139

1134-
// Convert length to integer
1135-
$length = (int) $length;
1136-
1137-
// Ensure the mask length does not exceed the string length
1138-
$length = min($length, $strLength);
1139-
1140-
// is length and string length is same, then divide num into two
1141-
if($strLength === $length){
1142-
$length = (int) ceil($length / 2);
1143-
}
1144-
1145-
// Calculate number of unmasked characters
1146-
$unmaskedLength = $strLength - $length;
1147-
1148-
// if valid email address and position of email is found
1140+
// If it's a valid email, mask only the email part (excluding the domain)
11491141
if ($isEmail && $atPosition !== false) {
1150-
// extract email without the tld (top-level domain)
11511142
$email = mb_substr($str, 0, mb_strpos($str, "@"));
1152-
1153-
// extract only the tld, to be added at the end
11541143
$tld = mb_substr($str, mb_strpos($str, "@"));
11551144

1156-
// now mask only the email using the middle position
1157-
$maskedString = self::mask($email, $length, $position);
1158-
1159-
return "{$maskedString}{$tld}";
1145+
// Mask only the email part, keeping visibility as per the $length
1146+
$maskedEmail = self::mask($email, $length, $position);
1147+
return "{$maskedEmail}{$tld}";
11601148
}
11611149

1162-
// For left position
1163-
if ($position == 'left') {
1164-
// Mask the first 'length' characters and leave the rest visible
1165-
return str_repeat($mask, $length) . mb_substr($str, $length, null, 'UTF-8');
1150+
// Left masking: Show first 'length' characters, mask the rest
1151+
if ($position === 'left') {
1152+
return mb_substr($str, 0, $length, 'UTF-8') . str_repeat($mask, $maskedLength);
11661153
}
1167-
elseif ($position == 'right') {
1168-
// Mask the right part of the string
1169-
return mb_substr($str, 0, $unmaskedLength, 'UTF-8') . str_repeat($mask, $length);
1154+
// Right masking: Mask everything except the last 'length' characters
1155+
elseif ($position === 'right') {
1156+
return str_repeat($mask, $maskedLength) . mb_substr($str, -$length, null, 'UTF-8');
11701157
}
1158+
// Center masking: Keep equal parts visible on both sides
11711159
else {
1172-
// Calculate how much to keep visible on both sides
1173-
$halfLength = (int) floor(($strLength - $length) / 2); // Evenly distribute the mask
1174-
1175-
// Ensure the remaining visible part accounts for the total length correctly
1176-
$start = mb_substr($str, 0, $halfLength);
1177-
$end = mb_substr($str, - $halfLength);
1178-
1179-
// Mask the middle portion
1180-
return $start . str_repeat($mask, $length) . $end;
1160+
$halfVisible = (int) floor($length / 2);
1161+
$start = mb_substr($str, 0, $halfVisible, 'UTF-8');
1162+
$end = mb_substr($str, -$halfVisible, null, 'UTF-8');
1163+
return $start . str_repeat($mask, $maskedLength) . $end;
11811164
}
11821165
}
1183-
1166+
11841167
/**
11851168
* Validate an email address.
11861169
*

Tests/numberToWords.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
->toNumber(),
2222

2323

24-
NumberToWords()->getCurrencyValue('nga'),
25-
// NumberToWords()->CurrencyNames(),
24+
NumberToWords()->getCurrencyByIso3('nga'),
25+
// NumberToWords()->allCurrency(),
2626
// NumberToWords::getUnits(),
2727
);

Time.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,20 @@ public function subYears($value = 0)
233233
{
234234
return $this->buildTimeModifier('year', $value, true);
235235
}
236+
237+
/**
238+
* Create date from Format
239+
*
240+
* @param int|string $datetime
241+
* @param string $format
242+
* @return void
243+
*/
244+
public function createFromFormat($datetime, $format = 'm/d/Y h:i:sa')
245+
{
246+
return $this->__setDate(
247+
self::timestamp($datetime, $format)
248+
);
249+
}
236250

237251
/**
238252
* Set custom time

0 commit comments

Comments
 (0)