Skip to content

Commit f173129

Browse files
committed
fix: Allow usage of class without static methods
1 parent 4e798f4 commit f173129

7 files changed

Lines changed: 54 additions & 40 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ $ composer require plook/type-guard
4242

4343
### Setting the default target time zone of `DateTimeImmutable` objects
4444
```php
45-
Convert::timeZone('Australia/Adelaide');
46-
Convert::timeZone(new DateTimeZone('Australia/Adelaide'));
45+
Convert::instance()->timeZone('Australia/Adelaide');
46+
Convert::instance()->timeZone(new DateTimeZone('Australia/Adelaide'));
4747
```
4848

4949
### Setting the default format of date time strings
5050
```php
51-
Convert::dateTimeFormat(DateTimeInterface::ATOM);
51+
Convert::instance()->dateTimeFormat(DateTimeInterface::ATOM);
5252
```

src/Assert/Assert.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66

77
final class Assert
88
{
9+
public static function instance(): self
10+
{
11+
static $instance = new self();
12+
13+
return $instance;
14+
}
15+
916
/**
1017
* @param ?T $value
1118
*
1219
* @return T
1320
*
1421
* @template T
1522
*/
16-
public static function notNull(mixed $value): mixed
23+
public function notNull(mixed $value): mixed
1724
{
1825
if ($value === null) {
1926
throw InvalidValue::null();

src/Assert/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
*/
2020
function notNull(mixed $value): mixed
2121
{
22-
return Assert::notNull($value);
22+
return Assert::instance()->notNull($value);
2323
}
2424
}

src/Convert/Convert.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@
1717

1818
final class Convert
1919
{
20-
private static DateTimeZone|null $dateTimeZone = null;
20+
private DateTimeZone|null $dateTimeZone = null;
2121

22-
private static string $dateTimeFormat = 'c';
22+
private string $dateTimeFormat = 'c';
23+
24+
public static function instance(): self
25+
{
26+
static $instance = new self();
27+
28+
return $instance;
29+
}
2330

2431
/** @return ($value is null ? null : bool) */
25-
public static function asBool(mixed $value): bool|null
32+
public function asBool(mixed $value): bool|null
2633
{
2734
if ($value === null) {
2835
return null;
@@ -44,7 +51,7 @@ public static function asBool(mixed $value): bool|null
4451
}
4552

4653
/** @return ($value is null ? null : float) */
47-
public static function asFloat(mixed $value): float|null
54+
public function asFloat(mixed $value): float|null
4855
{
4956
if ($value === null) {
5057
return null;
@@ -66,7 +73,7 @@ public static function asFloat(mixed $value): float|null
6673
}
6774

6875
/** @return ($value is null ? null : int) */
69-
public static function asInt(mixed $value): int|null
76+
public function asInt(mixed $value): int|null
7077
{
7178
if ($value === null) {
7279
return null;
@@ -88,7 +95,7 @@ public static function asInt(mixed $value): int|null
8895
}
8996

9097
/** @return ($value is null ? null : string) */
91-
public static function asString(mixed $value): string|null
98+
public function asString(mixed $value): string|null
9299
{
93100
if ($value === null) {
94101
return null;
@@ -110,18 +117,18 @@ public static function asString(mixed $value): string|null
110117
}
111118

112119
/** @return ($value is null ? null : DateTimeImmutable) */
113-
public static function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
120+
public function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
114121
{
115122
if ($value === null) {
116123
return null;
117124
}
118125

119126
if ($value instanceof DateTimeImmutable) {
120-
if ($value->getTimezone()->getName() === self::timeZone()->getName()) {
127+
if ($value->getTimezone()->getName() === $this->timeZone()->getName()) {
121128
return $value;
122129
}
123130

124-
return $value->setTimezone(self::timeZone());
131+
return $value->setTimezone($this->timeZone());
125132
}
126133

127134
if ($value instanceof Stringable) {
@@ -132,40 +139,40 @@ public static function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
132139
throw NotConvertable::toDateTime($value);
133140
}
134141

135-
return new DateTimeImmutable(asString($value), self::timeZone());
142+
return new DateTimeImmutable(asString($value), $this->timeZone());
136143
}
137144

138145
/** @return ($value is null ? null : string) */
139-
public static function asDateTimeString(mixed $value): string|null
146+
public function asDateTimeString(mixed $value): string|null
140147
{
141148
if ($value === null) {
142149
return null;
143150
}
144151

145152
$value = asDateTimeImmutable($value);
146153

147-
return $value->format(self::dateTimeFormat());
154+
return $value->format($this->dateTimeFormat());
148155
}
149156

150-
public static function timeZone(DateTimeZone|string|null $timeZone = null): DateTimeZone
157+
public function timeZone(DateTimeZone|string|null $timeZone = null): DateTimeZone
151158
{
152159
if (is_string($timeZone)) {
153-
self::$dateTimeZone = new DateTimeZone($timeZone);
160+
$this->dateTimeZone = new DateTimeZone($timeZone);
154161
} elseif ($timeZone instanceof DateTimeZone) {
155-
self::$dateTimeZone = $timeZone;
156-
} elseif (!self::$dateTimeZone instanceof DateTimeZone) {
157-
self::$dateTimeZone = new DateTimeZone(ini_get('date.timezone'));
162+
$this->dateTimeZone = $timeZone;
163+
} elseif (!$this->dateTimeZone instanceof DateTimeZone) {
164+
$this->dateTimeZone = new DateTimeZone(ini_get('date.timezone'));
158165
}
159166

160-
return self::$dateTimeZone;
167+
return $this->dateTimeZone;
161168
}
162169

163-
public static function dateTimeFormat(string|null $dateTimeFormat = null): string
170+
public function dateTimeFormat(string|null $dateTimeFormat = null): string
164171
{
165172
if ($dateTimeFormat !== null) {
166-
self::$dateTimeFormat = $dateTimeFormat;
173+
$this->dateTimeFormat = $dateTimeFormat;
167174
}
168175

169-
return self::$dateTimeFormat;
176+
return $this->dateTimeFormat;
170177
}
171178
}

src/Convert/functions.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/** @return ($value is null ? null : bool) */
1616
function asBool(mixed $value): bool|null
1717
{
18-
return Convert::asBool($value);
18+
return Convert::instance()->asBool($value);
1919
}
2020
}
2121

@@ -24,7 +24,7 @@ function asBool(mixed $value): bool|null
2424
/** @return ($value is null ? null : float) */
2525
function asFloat(mixed $value): float|null
2626
{
27-
return Convert::asFloat($value);
27+
return Convert::instance()->asFloat($value);
2828
}
2929
}
3030

@@ -33,7 +33,7 @@ function asFloat(mixed $value): float|null
3333
/** @return ($value is null ? null : int) */
3434
function asInt(mixed $value): int|null
3535
{
36-
return Convert::asInt($value);
36+
return Convert::instance()->asInt($value);
3737
}
3838
}
3939

@@ -42,7 +42,7 @@ function asInt(mixed $value): int|null
4242
/** @return ($value is null ? null : string) */
4343
function asString(mixed $value): string|null
4444
{
45-
return Convert::asString($value);
45+
return Convert::instance()->asString($value);
4646
}
4747
}
4848

@@ -51,7 +51,7 @@ function asString(mixed $value): string|null
5151
/** @return ($value is null ? null : DateTimeImmutable) */
5252
function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
5353
{
54-
return Convert::asDateTimeImmutable($value);
54+
return Convert::instance()->asDateTimeImmutable($value);
5555
}
5656
}
5757

@@ -60,6 +60,6 @@ function asDateTimeImmutable(mixed $value): DateTimeImmutable|null
6060
/** @return ($value is null ? null : string) */
6161
function asDateTimeString(mixed $value): string|null
6262
{
63-
return Convert::asDateTimeString($value);
63+
return Convert::instance()->asDateTimeString($value);
6464
}
6565
}

tests/Convert/AsDateTimeImmutableTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ final class AsDateTimeImmutableTest extends TestCase
2727

2828
protected function setUp(): void
2929
{
30-
$this->originalTimeZone = Convert::timeZone();
30+
$this->originalTimeZone = Convert::instance()->timeZone();
3131
}
3232

3333
protected function tearDown(): void
3434
{
35-
Convert::timeZone($this->originalTimeZone);
35+
Convert::instance()->timeZone($this->originalTimeZone);
3636
}
3737

3838
public function testConvertsStrings(): void
@@ -53,7 +53,7 @@ public function testConvertsStringables(): void
5353

5454
public function testConvertsDateTimeImmutableWithSameTimeZone(): void
5555
{
56-
$result = asDateTimeImmutable(new DateTimeImmutable('2010-09-08T07:06:05', Convert::timeZone()));
56+
$result = asDateTimeImmutable(new DateTimeImmutable('2010-09-08T07:06:05', Convert::instance()->timeZone()));
5757

5858
self::assertInstanceOf(DateTimeImmutable::class, $result);
5959
self::assertSame('2010-09-08T07:06:05+00:00', $result->format('c'));
@@ -71,7 +71,7 @@ public function testConvertsDateTimeImmutableWithDifferentTimeZone(): void
7171

7272
public function testConvertsDateTimeImmutableDefaultTimeZoneCanBeChangedByDateTimeZone(): void
7373
{
74-
Convert::timeZone(new DateTimeZone('Australia/Adelaide'));
74+
Convert::instance()->timeZone(new DateTimeZone('Australia/Adelaide'));
7575

7676
$result = asDateTimeImmutable(new DateTimeImmutable('2010-09-08T07:06:05+00:00'));
7777

@@ -81,7 +81,7 @@ public function testConvertsDateTimeImmutableDefaultTimeZoneCanBeChangedByDateTi
8181

8282
public function testConvertsDateTimeImmutableDefaultTimeZoneCanBeChangedByTimeZoneName(): void
8383
{
84-
Convert::timeZone('Australia/Adelaide');
84+
Convert::instance()->timeZone('Australia/Adelaide');
8585

8686
$result = asDateTimeImmutable(new DateTimeImmutable('2010-09-08T07:06:05+00:00'));
8787

tests/Convert/AsDateTimeStringTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ final class AsDateTimeStringTest extends TestCase
2626

2727
protected function setUp(): void
2828
{
29-
$this->originalDateTimeFormat = Convert::dateTimeFormat();
29+
$this->originalDateTimeFormat = Convert::instance()->dateTimeFormat();
3030
}
3131

3232
protected function tearDown(): void
3333
{
34-
Convert::dateTimeFormat($this->originalDateTimeFormat);
34+
Convert::instance()->dateTimeFormat($this->originalDateTimeFormat);
3535
}
3636

3737
public function testConvertsStrings(): void
@@ -49,7 +49,7 @@ public function testConvertsStringables(): void
4949

5050
public function testDateTimeFormatCanBeChanged(): void
5151
{
52-
Convert::dateTimeFormat('Y-m-d');
52+
Convert::instance()->dateTimeFormat('Y-m-d');
5353

5454
self::assertSame('2010-09-08', asDateTimeString('2010-09-08T07:06:05+02:00'));
5555
}

0 commit comments

Comments
 (0)