Skip to content

Commit 4522011

Browse files
committed
Add __toString method to BoolType and standardize use of Undefined alias
- Implemented `__toString` in `BoolType`, enabling seamless string conversion. - Introduced `tryFromString` and `tryFromInt` methods accepting `Undefined` alias instead of `UndefinedStandard`. - Updated relevant tests to reflect changes in `Undefined` alias usage.
1 parent f44b09d commit 4522011

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

src/Abstract/Bool/BoolType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ public static function fromBool(bool $value): static
2020
{
2121
return new static($value);
2222
}
23+
24+
public function __toString(): string
25+
{
26+
return $this->toString();
27+
}
2328
}

src/Abstract/Bool/BoolTypeInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@
44

55
namespace PhpTypedValues\Abstract\Bool;
66

7+
use PhpTypedValues\Undefined\Alias\Undefined;
8+
79
/**
810
* @psalm-immutable
911
*/
1012
interface BoolTypeInterface
1113
{
1214
public function value(): bool;
1315

16+
public static function tryFromString(string $value): static|Undefined;
17+
18+
public static function tryFromInt(int $value): static|Undefined;
19+
1420
public static function fromString(string $value): static;
1521

1622
public static function fromBool(bool $value): static;
1723

1824
public function toString(): string;
25+
26+
public function __toString(): string;
1927
}

src/Bool/BoolStandard.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PhpTypedValues\Abstract\Bool\BoolType;
88
use PhpTypedValues\Exception\BoolTypeException;
99
use PhpTypedValues\Exception\TypeException;
10-
use PhpTypedValues\Undefined\UndefinedStandard;
10+
use PhpTypedValues\Undefined\Alias\Undefined;
1111

1212
use function sprintf;
1313

@@ -27,21 +27,21 @@ public function __construct(bool $value)
2727
$this->value = $value;
2828
}
2929

30-
public static function tryFromString(string $value): static|UndefinedStandard
30+
public static function tryFromString(string $value): static|Undefined
3131
{
3232
try {
3333
return static::fromString($value);
3434
} catch (TypeException) {
35-
return UndefinedStandard::create();
35+
return Undefined::create();
3636
}
3737
}
3838

39-
public static function tryFromInt(int $value): static|UndefinedStandard
39+
public static function tryFromInt(int $value): static|Undefined
4040
{
4141
try {
4242
return static::fromInt($value);
4343
} catch (TypeException) {
44-
return UndefinedStandard::create();
44+
return Undefined::create();
4545
}
4646
}
4747

tests/Unit/Bool/BoolStandardTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
->and($ok->value())->toBeTrue();
6565

6666
// Undefined type instance indicates failure without throwing
67-
expect($fail::class)->toBe(PhpTypedValues\Undefined\UndefinedStandard::class);
67+
expect($fail::class)->toBe(PhpTypedValues\Undefined\Alias\Undefined::class);
6868
});
6969

7070
it('tryFromInt returns Undefined on invalid input and BoolStandard on valid', function (): void {
@@ -76,7 +76,7 @@
7676
->and($one->value())->toBeTrue()
7777
->and($zero)->toBeInstanceOf(BoolStandard::class)
7878
->and($zero->value())->toBeFalse()
79-
->and($bad::class)->toBe(PhpTypedValues\Undefined\UndefinedStandard::class);
79+
->and($bad::class)->toBe(PhpTypedValues\Undefined\Alias\Undefined::class);
8080
});
8181

8282
it('parses string values with surrounding whitespace', function (): void {
@@ -85,3 +85,11 @@
8585
->and(BoolStandard::fromString(' on ')->value())->toBeTrue()
8686
->and(BoolStandard::fromString(' off ')->value())->toBeFalse();
8787
});
88+
89+
it('casts to string via __toString magic method', function (): void {
90+
$t = new BoolStandard(true);
91+
$f = BoolStandard::fromBool(false);
92+
93+
expect((string) $t)->toBe('true')
94+
->and((string) $f)->toBe('false');
95+
});

0 commit comments

Comments
 (0)