@@ -27,7 +27,7 @@ composer require georgii-web/php-typed-values:^1.0
2727#### 1. Use existing typed values with validation built in:
2828
2929``` php
30- $id = PositiveInt ::fromString('123');
30+ $id = IntegerPositive ::fromString('123');
3131```
3232
3333instead of duplicating this logic across your application like:
@@ -42,15 +42,15 @@ if ($id <= 0) {
4242#### 2. Create aliases:
4343
4444``` php
45- readonly class Id extends PositiveInt {}
45+ readonly class Id extends IntegerPositive {}
4646
4747Id::fromInt(123);
4848```
4949
5050#### 3. Create a composite value object:
5151
5252``` php
53- final class Profile
53+ final readonly class Profile
5454{
5555 public function __construct(
5656 public readonly IntegerPositive $id,
@@ -65,21 +65,21 @@ final class Profile
6565 ): static {
6666 return new static(
6767 IntegerPositive::fromInt($id),
68- StringNonEmpty::fromString($firstName),
68+ StringNonEmpty::fromString($firstName), // Early fail
6969 $height !== null ? FloatNonNegative::fromString((string) $height) : null,
7070 );
7171 }
7272
7373 public function getHeight(): FloatNonNegative|Undefined { // avoid using NULL, which could mean anything
74- return $this->height ?? Undefined::create();
74+ return $this->height ?? Undefined::create(); // Late fail
7575 }
7676}
7777
7878// Usage
79- Profile ::fromScalars(id: 101, firstName: 'Alice', height: '172.5');
80- Profile ::fromScalars(id: 157, firstName: 'Tom', height: null);
79+ \PhpTypedValues\Usage\Composite ::fromScalars(id: 101, firstName: 'Alice', height: '172.5');
80+ \PhpTypedValues\Usage\Composite ::fromScalars(id: 157, firstName: 'Tom', height: null);
8181// From array
82- $profile = Profile ::fromScalars(...[157, 'Tom', null]);
82+ $profile = \PhpTypedValues\Usage\Composite ::fromScalars(...[157, 'Tom', null]);
8383// Accessing values
8484$profile->getHeight(); // "172.5" OR "Undefined" type class (will throw an exception on trying to get value)
8585```
0 commit comments