Skip to content

Commit 44c319d

Browse files
update
1 parent b2dfaf4 commit 44c319d

File tree

5 files changed

+318
-190
lines changed

5 files changed

+318
-190
lines changed

Capsule/TimeHelper.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tamedevelopers\Support\Capsule;
6+
7+
use Tamedevelopers\Support\Country;
8+
9+
10+
class TimeHelper {
11+
12+
/**
13+
* Set the timezone.
14+
* @param string|null $timezone
15+
*
16+
* @return string
17+
*/
18+
static public function setPassedTimezone($timezone = null)
19+
{
20+
if(in_array($timezone, Country::timeZone())){
21+
$timezone = $timezone;
22+
} else{
23+
$timezone = date_default_timezone_get() ?? 'UTC';
24+
}
25+
26+
return $timezone;
27+
}
28+
29+
/**
30+
* Set Date Time
31+
* @param int|string|null $date
32+
*
33+
* @return int|false
34+
*/
35+
static public function setPassedDate($date = null)
36+
{
37+
if(empty($date)){
38+
$date = date('M d Y', strtotime('this year January'));
39+
$date = "Jan 01 1970";
40+
}
41+
42+
if (is_numeric($date)) {
43+
$date = date('M d Y', (int) $date);
44+
}
45+
46+
return strtotime($date);
47+
}
48+
49+
/**
50+
* Check if an Instance of Carbon
51+
* - When using in Laravel, the Default Time Class automatically changed to Carbon by Laravel
52+
*
53+
* @param mixed $date
54+
* @return int
55+
*/
56+
static public function carbonInstance($date)
57+
{
58+
return $date?->timestamp ?? $date;
59+
}
60+
61+
}

Tame.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ static public function mask($str = null, ?int $length = 4, ?string $position = '
956956
return $str;
957957
}
958958

959+
// trim string
959960
$str = Str::trim($str);
960961

961962
// Get the length of the string
@@ -980,25 +981,33 @@ static public function mask($str = null, ?int $length = 4, ?string $position = '
980981
$strMinusLength = abs(1);
981982
}
982983

984+
// to int
985+
$length = (int) $length;
986+
983987
// For left position
984988
if ($position == 'left') {
985-
$length = (int) $length;
986989
if ($isEmail && $atPosition !== false) {
987-
// Mask the left part of the string, including the "@" symbol
988-
return mb_substr(mb_substr($str, 0, $atPosition, 'UTF-8'), 0, $length, 'UTF-8') . str_repeat($mask, $atPosition - $length) . mb_substr($str, $atPosition, null, 'UTF-8');
990+
// extract email without the tld (top level domain)
991+
$email = mb_substr($str, 0, mb_strpos($str, "@"));
992+
993+
// extract only the tld, to be added at the end
994+
$tld = mb_substr($str, mb_strpos($str, "@"));
995+
996+
// now mask only the email using the middle position
997+
$maskedString = self::mask($email, $length, 'middle');
998+
999+
return "{$maskedString}{$tld}";
9891000
} else {
990-
// Mask the entire string if it's not an email
991-
return mb_substr($str, 0, $length, 'UTF-8') . str_repeat($mask, $strMinusLength);
1001+
return str_repeat($mask, $strMinusLength) . mb_substr($str, -$length, null, 'UTF-8');
9921002
}
993-
} elseif ($position == 'middle' || $position == 'center') {
1003+
} elseif (in_array($position, ['middle', 'center'])) {
9941004
// Mask the middle part of the string
9951005
$length = (int) round($length / 2);
9961006

9971007
return mb_substr($str, 0, $length, 'UTF-8') . str_repeat($mask, $strMinusLength) . mb_substr($str, -$length, null, 'UTF-8');
9981008
} else {
9991009
// Mask the right part of the string
1000-
$length = (int) $length;
1001-
return str_repeat($mask, $strMinusLength) . mb_substr($str, -$length, null, 'UTF-8');
1010+
return mb_substr($str, 0, $length, 'UTF-8') . str_repeat($mask, $strMinusLength);
10021011
}
10031012
}
10041013

Tests/time.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,32 @@
88

99
// set the default time and timezone
1010
// helper function
11-
TameTime(
11+
$mainTime = TameTime(
1212
timezone: 'Africa/Lagos',
13-
time: 'now',
13+
time: 'now',
1414
);
1515

16-
1716
// Time::setDate('last week');
18-
// Time::setTimezone('Asia/Tokyo');
19-
20-
// TameTime()->setDate('last week');
21-
// TameTime()->setTimezone('Asia/Tokyo');
17+
// Time::setTimezone('Pacific/Pago_Pago');
2218

23-
// Time::setDate('last week');
2419
// TameTime('yesterday')->time();
2520
// TameTime('last week')->sec();
2621

22+
$time2 = Time::setTimezone('Atlantic/South_Georgia');
23+
$time3 = Time::setTimezone('Indian/Antananarivo');
24+
2725
dd(
26+
TameTime()->format('yesterday')->ago(),
27+
TameTime()->toJs('now'),
2828

29-
Time::setTimezone('Asia/Tokyo')
30-
->format('now')
31-
->ago()
32-
,
29+
Time::setTimezone('Asia/Tokyo'),
3330

34-
TameTime()->format('yesterday')->ago(),
31+
$mainTime->format('last year december'),
32+
$mainTime->getTimeZone(),
33+
34+
$time2->format('this year october')->ago(),
35+
$time2->getTimeZone(),
3536

36-
TameTime()->toJs('now')
37+
$time3,
38+
$time3->greetings()
3739
);

0 commit comments

Comments
 (0)