Skip to content

Commit 62ccdca

Browse files
committed
feat!: Replace Factory with Clock; Remove redundant factory methods
1 parent 594c552 commit 62ccdca

10 files changed

+116
-201
lines changed

src/ClockInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pauci\DateTime;
6+
7+
interface ClockInterface
8+
{
9+
public function now(): DateTimeInterface;
10+
}

src/DateTime.php

+43-40
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,85 @@
44

55
namespace Pauci\DateTime;
66

7+
use DateTimeImmutable;
78
use DateTimeZone;
9+
use Exception;
810

9-
class DateTime extends \DateTimeImmutable implements DateTimeInterface
11+
class DateTime extends DateTimeImmutable implements DateTimeInterface
1012
{
11-
private static ?DateTimeFactoryInterface $factory;
13+
protected static ?ClockInterface $clock = null;
1214

13-
public static function getFactory(): DateTimeFactoryInterface
15+
public static function getClock(): ClockInterface
1416
{
15-
return self::$factory ??= new DateTimeFactory();
17+
return static::$clock ??= new SystemClock();
1618
}
1719

18-
public static function setFactory(DateTimeFactoryInterface $factory): void
20+
public static function setClock(ClockInterface $clock): void
1921
{
20-
self::$factory = $factory;
22+
static::$clock = $clock;
2123
}
2224

23-
public static function now(): DateTimeInterface
25+
final public function __construct(string $datetime = 'now', ?DateTimeZone $timezone = null)
2426
{
25-
return self::getFactory()->now();
26-
}
27+
if ('now' === $datetime
28+
&& null !== static::$clock
29+
&& !static::$clock instanceof SystemClock
30+
) {
31+
$datetime = static::getClock()->now()->toString();
32+
}
2733

28-
public static function microsecondsNow(): DateTimeInterface
29-
{
30-
return self::getFactory()->microsecondsNow();
34+
parent::__construct($datetime, $timezone);
3135
}
3236

33-
public static function fromString(string $time, DateTimeZone $timezone = null): DateTimeInterface
37+
public static function now(): DateTimeInterface
3438
{
35-
return self::getFactory()->fromString($time, $timezone);
39+
return static::getClock()->now();
3640
}
3741

38-
public static function fromFormat(string $format, string $time, DateTimeZone $timezone = null): DateTimeInterface
42+
/**
43+
* @throws Exception
44+
*/
45+
public static function fromString(string $time, DateTimeZone $timezone = null): static
3946
{
40-
return self::getFactory()->fromFormat($format, $time, $timezone);
47+
return new static($time, $timezone);
4148
}
4249

43-
public static function fromTimestamp(int $timestamp, DateTimeZone $timezone = null): DateTimeInterface
50+
/**
51+
* @throws Exception
52+
*/
53+
public static function fromTimestamp(int $timestamp, DateTimeZone $timezone = null): static
4454
{
45-
return self::getFactory()->fromTimestamp($timestamp, $timezone);
55+
return (new static('@' . $timestamp))
56+
->setTimezone($timezone ?? new DateTimeZone(date_default_timezone_get()));
4657
}
4758

48-
public static function fromFloatTimestamp(float $timestamp, DateTimeZone $timezone = null): DateTimeInterface
59+
public static function fromFloatTimestamp(float $timestamp, DateTimeZone $timezone = null): static
4960
{
50-
return self::getFactory()->fromFloatTimestamp($timestamp, $timezone);
51-
}
61+
$integerPart = (int) floor($timestamp);
62+
$fractionalPart = substr(sprintf('%.6f', $timestamp - $integerPart), 2);
5263

53-
public static function fromDateTime(\DateTimeInterface $dateTime): DateTimeInterface
54-
{
55-
return self::getFactory()->fromDateTime($dateTime);
64+
$dateTime = new static(date('Y-m-d H:i:s.' . $fractionalPart, $integerPart));
65+
66+
return $timezone ? $dateTime->setTimezone($timezone) : $dateTime;
5667
}
5768

5869
#[\ReturnTypeWillChange]
5970
public static function createFromFormat(
6071
string $format,
6172
string $datetime,
6273
DateTimeZone $timezone = null
63-
): DateTimeInterface {
64-
return self::fromFormat($format, $datetime, $timezone);
74+
): static|false {
75+
return parent::createFromFormat($format, $datetime, $timezone);
6576
}
6677

6778
#[\ReturnTypeWillChange]
68-
public static function createFromMutable(\DateTime $object): DateTimeInterface
79+
public static function createFromMutable(\DateTime $object): static
6980
{
70-
return self::fromDateTime($object);
81+
return parent::createFromMutable($object);
7182
}
7283

7384
/**
74-
* @throws \Exception
85+
* @throws Exception
7586
*/
7687
public function diff(\DateTimeInterface $targetObject, bool $absolute = false): DateInterval
7788
{
@@ -90,17 +101,9 @@ public function sub(\DateInterval $interval): static
90101
return parent::sub($interval);
91102
}
92103

93-
public function modify(string $modifier): static
104+
public function modify(string $modifier): static|false
94105
{
95-
try {
96-
$dateTime = parent::modify($modifier);
97-
} catch (\Throwable $e) {
98-
$message = strtr($e->getMessage(), [\DateTimeImmutable::class => static::class]);
99-
100-
throw new Exception\FailedToModifyException($message, previous: $e);
101-
}
102-
103-
return $dateTime;
106+
return parent::modify($modifier);
104107
}
105108

106109
public function setDate(int $year, int $month, int $day): static
@@ -133,7 +136,7 @@ public function equals(DateTimeInterface $dateTime): bool
133136
return $this == $dateTime;
134137
}
135138

136-
public function inDefaultTimezone(): DateTimeInterface
139+
public function inDefaultTimezone(): static
137140
{
138141
return $this->setTimezone(new DateTimeZone(date_default_timezone_get()));
139142
}

src/DateTimeFactory.php

-80
This file was deleted.

src/DateTimeFactoryInterface.php

-24
This file was deleted.

src/DateTimeInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ public function diff(\DateTimeInterface $targetObject, bool $absolute = false):
1010

1111
public function equals(self $dateTime): bool;
1212

13-
public function inDefaultTimezone(): DateTimeInterface;
13+
public function inDefaultTimezone(): static;
1414

1515
public function toString(): string;
1616

1717
public function add(\DateInterval $interval): static;
1818

1919
public function sub(\DateInterval $interval): static;
2020

21-
public function modify(string $modifier): static;
21+
public function modify(string $modifier): static|false;
2222

2323
public function setDate(int $year, int $month, int $day): static;
2424

src/Exception/FailedToModifyException.php

-9
This file was deleted.

src/Exception/InvalidTimeStringException.php

-11
This file was deleted.

src/FrozenClock.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pauci\DateTime;
6+
7+
class FrozenClock implements ClockInterface
8+
{
9+
private DateTimeInterface $now;
10+
11+
public function __construct(DateTimeInterface $now = null)
12+
{
13+
$this->now = $now ?? new DateTime();
14+
}
15+
16+
public function set(DateTimeInterface $now): void
17+
{
18+
$this->now = $now;
19+
}
20+
21+
public function modify(string $modifier): void
22+
{
23+
$now = $this->now->modify($modifier);
24+
25+
if (false !== $now) {
26+
$this->now = $now;
27+
}
28+
}
29+
30+
public function now(): DateTimeInterface
31+
{
32+
return $this->now;
33+
}
34+
}

src/SystemClock.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pauci\DateTime;
6+
7+
class SystemClock implements ClockInterface
8+
{
9+
public function now(): DateTimeInterface
10+
{
11+
return new DateTime();
12+
}
13+
}

0 commit comments

Comments
 (0)