Skip to content

Commit 371031d

Browse files
update
1 parent d9f9673 commit 371031d

File tree

9 files changed

+100
-80
lines changed

9 files changed

+100
-80
lines changed

src/Capsule/FileCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static function clear(): void
164164
* @param string $key
165165
* @return string
166166
*/
167-
static protected function getCachePath(string $key): string
167+
protected static function getCachePath(string $key): string
168168
{
169169
return self::$cachePath . '/' . md5($key) . '.cache';
170170
}

src/Capsule/TimeHelper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ public static function setPassedDate($date = null)
118118
$default = 'Jan 01 1970';
119119

120120
if(empty($date)){
121-
// $date = date('M d Y', strtotime('this year January'));
122121
$date = $default;
123122
}
124123

src/Cookie.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,38 @@ class Cookie{
1313
* Site name
1414
* @var string
1515
*/
16-
static protected $name;
16+
protected static $name;
1717

1818
/**
1919
* Expire Cookie name
2020
* @var string
2121
*/
22-
static protected $expireName;
22+
protected static $expireName;
2323

2424
/**
2525
* Time Cookie name
2626
* @var string
2727
*/
28-
static protected $timeName;
28+
protected static $timeName;
2929

3030
/**
3131
* Time format
3232
* @var string
3333
*/
34-
static protected $timeFormat;
34+
protected static $timeFormat;
3535

3636
/**
3737
* Expire time format
3838
* @var mixed
3939
*/
40-
static protected $expireFormat;
40+
protected static $expireFormat;
4141

4242
/**
4343
* Create Sitename From .env
4444
*
4545
* @return $this
4646
*/
47-
static protected function init()
47+
protected static function init()
4848
{
4949
self::$name = Str::lower(Str::replace([' '], '', env('APP_NAME', '')));
5050
self::$timeName = "__time_" . self::$name;

src/Time.php

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* - Add/subtract helpers: addSeconds/Minutes/Hours/Days/Weeks/Months/Years and sub*
2020
* - Formatting: format(), toDateTimeString(), toJsTimer(), timestamp()
2121
* - Range helper: dateRange()
22-
* - Text features/config: config(), greeting (via __greeting), timeAgo (via __timeAgo)
22+
* - Text features/config: config(), greeting(), timeAgo()
2323
*
2424
* All methods are documented at their definitions for clarity.
2525
*/
@@ -82,25 +82,17 @@ public function __construct($date = null, $timezone = null)
8282
}
8383

8484
if (empty($this->date)) {
85-
$clone = $this->setDate($date);
85+
$date = $date ?: 'now';
86+
$clone = self::date($date);
8687

8788
$this->date = $clone->date;
8889
$this->timestamp = $clone->timestamp;
8990
$this->timezone = $clone->timezone;
9091
}
91-
92-
// Always refresh the static instance binding to the latest constructed instance
93-
// so subsequent static calls use the most recent timezone/date context
94-
self::$staticData = $this->copy();
92+
93+
$this->keepStaticBinding($this->clone());
9594
}
9695

97-
/**
98-
* Handle the calls to non-existent instance methods.
99-
* @param string $name
100-
* @param mixed $args
101-
*
102-
* @return mixed
103-
*/
10496
/**
10597
* Magic: instance dynamic calls map to supported methods.
10698
*
@@ -122,14 +114,11 @@ public function __call($name, $args)
122114
*/
123115
public static function __callStatic($name, $args)
124116
{
125-
// Use a cloned bound instance if available; otherwise seed a fresh one
126-
$instance = static::$staticData instanceof static
127-
? static::$staticData->copy()
128-
: new static();
129-
130-
return self::nonExistMethod($name, $args, $instance);
117+
return self::nonExistMethod($name, $args, self::$staticData);
131118
}
132119

120+
121+
133122
/**
134123
* Add Second to curent date
135124
* @param int $value
@@ -275,59 +264,80 @@ public function subYears($value = 0)
275264
{
276265
return $this->buildTimeModifier('year', $value, true);
277266
}
267+
268+
/**
269+
* Create timestamp
270+
*
271+
* @param int|string $date
272+
* - string|int|float
273+
*
274+
* @param string $format
275+
* - Your defined format type i.e: Y-m-d H:i:s a
276+
* - Converted TimeStamp
277+
*
278+
* @return string
279+
*/
280+
public static function timestamp($date, $format = "Y-m-d H:i:s")
281+
{
282+
$date = TimeHelper::setPassedDate($date);
283+
284+
return date($format, $date);
285+
}
278286

279287
/**
280-
* Create date from Format
281-
*
282-
* @param int|string $datetime
283-
* @param string $format
288+
* Set custom time
289+
* @param int|string $date
284290
* @return $this
285291
*/
286-
public function createFromFormat($datetime, $format = 'm/d/Y h:i:sa')
292+
public static function date($date)
287293
{
288-
return $this->setDate(
289-
self::timestamp($datetime, $format)
290-
);
294+
$base = self::baseInstance();
295+
296+
return $base->setDate($date);
291297
}
292298

293299
/**
294-
* Set custom time
295-
* @param int|string $date
300+
* Create date from Format
301+
*
302+
* @param int|string $datetime
303+
* @param string $format
296304
* @return $this
297305
*/
298-
public function date($date)
306+
public static function createFromFormat($datetime, $format = 'm/d/Y h:i:sa')
299307
{
300-
return $this->setDate($date);
308+
return self::date(
309+
self::timestamp($datetime, $format)
310+
);
301311
}
302312

303313
/**
304314
* Set time to `now`
305315
*
306316
* @return $this
307317
*/
308-
public function now()
318+
public static function now()
309319
{
310-
return $this->date('now');
320+
return self::date('now');
311321
}
312322

313323
/**
314324
* Set time to `today`
315325
*
316326
* @return $this
317327
*/
318-
public function today()
328+
public static function today()
319329
{
320-
return $this->setDate('today');
330+
return self::date('today');
321331
}
322332

323333
/**
324334
* Set time to `yesterday`
325335
*
326336
* @return $this
327337
*/
328-
public function yesterday()
338+
public static function yesterday()
329339
{
330-
return $this->setDate('yesterday');
340+
return self::date('yesterday');
331341
}
332342

333343
/**
@@ -344,7 +354,7 @@ public function yesterday()
344354
public function format($format = null, $date = null)
345355
{
346356
if (!empty($date)) {
347-
$clone = $this->setDate($date);
357+
$clone = self::date($date);
348358
$this->date = $clone->date;
349359
}
350360

@@ -365,25 +375,6 @@ public function toDateTimeString()
365375
return $this->format();
366376
}
367377

368-
/**
369-
* Create timestamp
370-
*
371-
* @param int|string $date
372-
* - string|int|float
373-
*
374-
* @param string $format
375-
* - Your defined format type i.e: Y-m-d H:i:s a
376-
* - Converted TimeStamp
377-
*
378-
* @return string
379-
*/
380-
public static function timestamp($date, $format = "Y-m-d H:i:s")
381-
{
382-
$date = TimeHelper::setPassedDate($date);
383-
384-
return date($format, $date);
385-
}
386-
387378
/**
388379
* Create Javascript timer
389380
*

src/Traits/NumberToWordsTraits.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ trait NumberToWordsTraits
190190
*
191191
* @return bool
192192
*/
193-
static protected function isWordsInstance()
193+
protected static function isWordsInstance()
194194
{
195195
return self::$staticData instanceof NumberToWords;
196196
}

src/Traits/ServerTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ trait ServerTrait{
1616
* Base directory of the application (normalized with trailing slash).
1717
* @var string
1818
*/
19-
static protected $base_dir = null;
19+
protected static $base_dir = null;
2020

2121
/**
2222
* Lightweight in-memory cache for server and domain values.

src/Traits/TimeExtraTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public function isToday(): bool
356356
*/
357357
public function isTomorrow(): bool
358358
{
359-
$tz = new DateTimeZone((string) $this->__getTimezone());
359+
$tz = new DateTimeZone((string) $this->getTimezone());
360360
$tomorrow = new DateTime('tomorrow', $tz);
361361
$cur = $this->dtInTz((int) $this->date)->format('Y-m-d');
362362
return $tomorrow->format('Y-m-d') === $cur;

src/Traits/TimeTrait.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,40 @@ trait TimeTrait{
2727
*
2828
* @return bool
2929
*/
30-
static protected function isTimeInstance()
30+
protected static function isTimeInstance()
3131
{
32-
return static::$staticData instanceof self;
32+
return self::$staticData instanceof Time;
33+
}
34+
35+
/**
36+
* Added a base resolver to reuse the latest instance context
37+
* @return $this
38+
*/
39+
private static function baseInstance()
40+
{
41+
if(!self::isTimeInstance()){
42+
$instance = self::keepStaticBinding(new static());
43+
} else{
44+
$instance = static::$staticData;
45+
}
46+
47+
return $instance;
48+
}
49+
50+
/**
51+
* Keep static binding in sync with latest produced clone
52+
* - Only if no static data available, bind it to the passed clone object
53+
*
54+
* @param \Time $clone
55+
* @return $this
56+
*/
57+
private static function keepStaticBinding($clone)
58+
{
59+
if(!self::isTimeInstance()){
60+
static::$staticData = $clone;
61+
}
62+
63+
return static::$staticData;
3364
}
3465

3566
/**
@@ -99,9 +130,6 @@ private function buildTimeModifier($mode = 'day', $value = 0, $sub = false)
99130
$clone->date = strtotime("{$date} {$sign} {$value}{$text}");
100131
$clone->timestamp = $clone->buildTimePrint();
101132

102-
// keep static binding in sync with latest produced clone
103-
static::$staticData = $clone->copy();
104-
105133
return $clone;
106134
}
107135

@@ -131,6 +159,8 @@ public function setTimezone($timezone = null)
131159

132160
$clone->setTimeZoneAndTimeStamp($timezone);
133161
$clone->timestamp = $clone->timestampPrint();
162+
163+
self::keepStaticBinding($clone);
134164

135165
return $clone;
136166
}
@@ -145,7 +175,7 @@ public function setDate($date = null)
145175
{
146176
$clone = $this->clone();
147177
$clone->date = TimeHelper::setPassedDate($date);
148-
$clone->timestamp = $clone->timestampPrint();
178+
$clone->timestamp = $clone->timestampPrint();
149179

150180
return $clone;
151181
}
@@ -310,7 +340,7 @@ private static function nonExistMethod($method = null, $args = null, $clone = nu
310340
'endofweek' => 'endOfWeek',
311341
'startofmonth' => 'startOfMonth',
312342
'endofmonth' => 'endOfMonth',
313-
'startofyear', 'startofyears' => 'startOfYear',
343+
'startofyear' => 'startOfYear',
314344
'endofyear' => 'endOfYear',
315345
'issameday' => 'isSameDay',
316346
'issamemonth' => 'isSameMonth',
@@ -330,9 +360,7 @@ private static function nonExistMethod($method = null, $args = null, $clone = nu
330360
default => null //format
331361
};
332362

333-
// this will happen if __construct has not been called
334-
// before calling an existing method
335-
// mostly when using [setglobaltimezone|getglobaltimezone] methods
363+
// this will happen if trying to none existing method
336364
if(empty($clone)){
337365
$clone = new static();
338366
}

tests/time.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
// ];
3737

3838
dd(
39+
Time::now(),
3940
$time->getTimezone(),
40-
$time2->getTimezone(),
41-
$time3->now(),
41+
$time2,
42+
$time3->date('last month')->getWeek(),
43+
$time3->getTimezone(),
4244
$time4->getTimezone(),
4345
);
4446

0 commit comments

Comments
 (0)