Skip to content

Commit 6f01a30

Browse files
committed
Add tryFromMixed implementation to type interfaces and classes, update usage examples, and refine convertMixedToString handling.
1 parent 64b0a4c commit 6f01a30

38 files changed

+311
-3
lines changed

src/Abstract/AbstractTypeInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ interface AbstractTypeInterface
3434
*/
3535
public static function fromString(string $value): static;
3636

37-
// public static function tryFromMixed(mixed $value): static|Undefined; //todo enable it
38-
3937
/**
4038
* Returns a normalized string representation of the underlying value.
4139
*/

src/Abstract/Bool/BoolTypeInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public static function tryFromString(string $value): static|Undefined;
2626

2727
public static function tryFromInt(int $value): static|Undefined;
2828

29+
public static function tryFromMixed(mixed $value): static|Undefined;
30+
2931
/**
3032
* @throws BoolTypeException
3133
*/

src/Abstract/DateTime/DateTimeTypeInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ public function value(): DateTimeImmutable;
2626

2727
public static function fromDateTime(DateTimeImmutable $value): static;
2828

29+
public static function tryFromMixed(mixed $value): static|Undefined;
30+
2931
public static function tryFromString(string $value): static|Undefined;
3032
}

src/Abstract/Float/FloatTypeInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ public function value(): float;
2424

2525
public static function fromFloat(float $value): static;
2626

27+
public static function tryFromMixed(mixed $value): static|Undefined;
28+
2729
public static function tryFromString(string $value): static|Undefined;
2830
}

src/Abstract/Integer/IntTypeInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ public static function fromInt(int $value): static;
2626

2727
public static function tryFromInt(int $value): static|Undefined;
2828

29+
public static function tryFromMixed(mixed $value): static|Undefined;
30+
2931
public static function tryFromString(string $value): static|Undefined;
3032
}

src/Abstract/String/StrTypeInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ interface StrTypeInterface
2323
public function value(): string;
2424

2525
public static function tryFromString(string $value): static|Undefined;
26+
27+
public static function tryFromMixed(mixed $value): static|Undefined;
2628
}

src/Abstract/Undefined/UndefinedTypeInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpTypedValues\Abstract\Undefined;
66

77
use PhpTypedValues\Exception\UndefinedTypeException;
8+
use PhpTypedValues\Undefined\Alias\Undefined;
89

910
/**
1011
* Contract for the special Undefined typed value.
@@ -23,6 +24,8 @@ interface UndefinedTypeInterface
2324
{
2425
public static function create(): static;
2526

27+
public static function tryFromMixed(mixed $value): Undefined;
28+
2629
/**
2730
* @throws UndefinedTypeException
2831
*/

src/Bool/BoolStandard.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ public function __construct(bool $value)
3434
$this->value = $value;
3535
}
3636

37+
public static function tryFromMixed(mixed $value): static|Undefined
38+
{
39+
try {
40+
return static::fromString(
41+
static::convertMixedToString($value)
42+
);
43+
} catch (TypeException) {
44+
return Undefined::create();
45+
}
46+
}
47+
3748
public static function tryFromString(string $value): static|Undefined
3849
{
3950
try {

src/Bool/FalseStandard.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public function __construct(bool $value)
4444
$this->value = false;
4545
}
4646

47+
public static function tryFromMixed(mixed $value): static|Undefined
48+
{
49+
try {
50+
return static::fromString(
51+
static::convertMixedToString($value)
52+
);
53+
} catch (TypeException) {
54+
return Undefined::create();
55+
}
56+
}
57+
4758
public static function tryFromString(string $value): static|Undefined
4859
{
4960
try {

src/Bool/TrueStandard.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public function __construct(bool $value)
4444
$this->value = true;
4545
}
4646

47+
public static function tryFromMixed(mixed $value): static|Undefined
48+
{
49+
try {
50+
return static::fromString(
51+
static::convertMixedToString($value)
52+
);
53+
} catch (TypeException) {
54+
return Undefined::create();
55+
}
56+
}
57+
4758
public static function tryFromString(string $value): static|Undefined
4859
{
4960
try {

0 commit comments

Comments
 (0)