Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 4bd8ad4

Browse files
committed
✨ Add __invoke() method
1 parent 85f56ed commit 4bd8ad4

16 files changed

+88
-0
lines changed

src/Concerns/AsBool.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public static function of(mixed $value): self
3535
));
3636
}
3737

38+
public function __invoke(mixed $value = null): bool
39+
{
40+
if ($value !== null) {
41+
return static::of($value)->get();
42+
}
43+
44+
return $this->get();
45+
}
46+
3847
public function isTrue(): bool
3948
{
4049
return $this->get();

src/Concerns/AsFloat.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public static function of(mixed $value): self
3434
get_debug_type($value),
3535
));
3636
}
37+
38+
public function __invoke(mixed $value = null): float
39+
{
40+
if ($value !== null) {
41+
return static::of($value)->get();
42+
}
43+
44+
return $this->get();
45+
}
3746
}

src/Concerns/AsInt.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public static function of(mixed $value): self
3434
get_debug_type($value),
3535
));
3636
}
37+
38+
public function __invoke(mixed $value = null): int
39+
{
40+
if ($value !== null) {
41+
return static::of($value)->get();
42+
}
43+
44+
return $this->get();
45+
}
3746
}

src/Concerns/AsMixed.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,13 @@ public function to(string $type): MixedType
3131
{
3232
return $type::of($this->get());
3333
}
34+
35+
public function __invoke(mixed $value = null): mixed
36+
{
37+
if ($value !== null) {
38+
static::of($value)->get();
39+
}
40+
41+
return $this->get();
42+
}
3443
}

src/Concerns/AsNumber.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public static function of(mixed $value): self
3434
get_debug_type($value),
3535
));
3636
}
37+
38+
public function __invoke(mixed $value = null): int|float
39+
{
40+
if ($value !== null) {
41+
return static::of($value)->get();
42+
}
43+
44+
return $this->get();
45+
}
3746
}

src/Concerns/AsNumeric.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public static function of(mixed $value): self
3434
get_debug_type($value),
3535
));
3636
}
37+
38+
public function __invoke(mixed $value = null): int|float|string
39+
{
40+
if ($value !== null) {
41+
return static::of($value)->get();
42+
}
43+
44+
return $this->get();
45+
}
3746
}

src/Concerns/AsScalar.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,13 @@ public static function of(mixed $value): self
4141
get_debug_type($value),
4242
));
4343
}
44+
45+
public function __invoke(mixed $value = null): bool|int|float|string
46+
{
47+
if ($value !== null) {
48+
return static::of($value)->get();
49+
}
50+
51+
return $this->get();
52+
}
4453
}

src/Concerns/AsString.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,13 @@ public static function of(mixed $value): self
3232
get_debug_type($value),
3333
));
3434
}
35+
36+
public function __invoke(mixed $value = null): string
37+
{
38+
if ($value !== null) {
39+
return static::of($value)->get();
40+
}
41+
42+
return $this->get();
43+
}
3544
}

src/Types/BoolType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ public static function isFalsy(bool $value): bool;
2323
public static function truthify(mixed $value): self;
2424

2525
public static function falsify(mixed $value): self;
26+
27+
public function __invoke(mixed $value = null): bool;
2628
}

src/Types/FloatType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
interface FloatType extends NumberType
88
{
99
public function get(): float;
10+
11+
public function __invoke(mixed $value = null): float;
1012
}

src/Types/IntType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
interface IntType extends NumberType
88
{
99
public function get(): int;
10+
11+
public function __invoke(mixed $value = null): int;
1012
}

src/Types/MixedType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ interface MixedType
99
public function get(): mixed;
1010

1111
public static function of(mixed $value): self;
12+
13+
public function __invoke(mixed $value = null): mixed;
1214
}

src/Types/NumberType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
interface NumberType extends NumericType
88
{
99
public function get(): int|float;
10+
11+
public function __invoke(mixed $value = null): int|float;
1012
}

src/Types/NumericType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
interface NumericType extends ScalarType
88
{
99
public function get(): int|float|string;
10+
11+
public function __invoke(mixed $value = null): int|float|string;
1012
}

src/Types/ScalarType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
interface ScalarType extends \Stringable, MixedType
88
{
99
public function get(): bool|int|float|string;
10+
11+
public function __invoke(mixed $value = null): bool|int|float|string;
1012
}

src/Types/StringType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
interface StringType extends ScalarType
88
{
99
public function get(): string;
10+
11+
public function __invoke(mixed $value = null): string;
1012
}

0 commit comments

Comments
 (0)