You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/USAGE.md
+88-26Lines changed: 88 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,7 @@ Floats (PhpTypedValues\Float):
35
35
DateTime (PhpTypedValues\DateTime):
36
36
37
37
- DateTimeAtom — immutable DateTime in RFC3339 (ATOM) format
38
-
- DateTimeTimestamp — immutable DateTime represented as Unix timestamp (seconds since epoch, UTC)
38
+
- DateTimeTimestamp — immutable DateTime represented as a Unix timestamp (seconds since epoch, UTC)
39
39
40
40
Static usage examples
41
41
---------------------
@@ -111,7 +111,7 @@ DateTimeAtom::fromString('not-a-date'); // throws: String has no valid datetime
111
111
Create your own type: alias of PositiveInt
112
112
------------------------------------------
113
113
114
-
If you want a domain-specific alias (e.g., UserId) that behaves like PositiveInt, extend PositiveInt and override static factories so they return the subclass instance (because the base uses `new self(...)`).
114
+
If you want a domain-specific alias (e.g., UserId) that behaves like PositiveInt, extend PositiveInt.
115
115
116
116
```php
117
117
<?php
@@ -121,22 +121,7 @@ namespace App\Domain;
121
121
122
122
use PhpTypedValues\Integer\PositiveInt;
123
123
124
-
final class UserId extends PositiveInt
125
-
{
126
-
public static function fromInt(int $value): self
127
-
{
128
-
// PositiveInt's constructor validates (> 0)
129
-
return new self($value);
130
-
}
131
-
132
-
public static function fromString(string $value): self
use PhpTypedValues\Code\Exception\NumericTypeException;
142
+
use PhpTypedValues\Code\Exception\FloatTypeException;
158
143
use PhpTypedValues\Code\Integer\IntType;
159
144
160
145
final class EvenPositiveInt extends IntType
@@ -163,14 +148,18 @@ final class EvenPositiveInt extends IntType
163
148
protected int $value;
164
149
165
150
/**
166
-
* @throws NumericTypeException
151
+
* @throws FloatTypeException
167
152
*/
168
153
public function __construct(int $value)
169
154
{
170
-
Assert::greaterThanEq($value, 1);
171
-
Assert::true($value % 2 === 0, 'Value must be even');
155
+
if ($value <= 0) {
156
+
throw new IntegerTypeException(sprintf('Expected positive integer, got "%d"', $value));
157
+
}
158
+
159
+
if ($value % 2 !== 0) {
160
+
throw new IntegerTypeException(sprintf('Expected even integer, got "%d"', $value));
161
+
}
172
162
173
-
/** @var positive-int $value */
174
163
$this->value = $value;
175
164
}
176
165
@@ -180,16 +169,17 @@ final class EvenPositiveInt extends IntType
180
169
return $this->value;
181
170
}
182
171
183
-
/** @throws NumericTypeException */
172
+
/** @throws IntegerTypeException */
184
173
public static function fromInt(int $value): self
185
174
{
186
175
return new self($value);
187
176
}
188
177
189
-
/** @throws NumericTypeException */
178
+
/** @throws IntegerTypeException */
190
179
public static function fromString(string $value): self
191
180
{
192
-
parent::assertNumericString($value);
181
+
parent::assertIntegerString($value);
182
+
193
183
return new self((int) $value);
194
184
}
195
185
}
@@ -198,6 +188,78 @@ final class EvenPositiveInt extends IntType
198
188
$v = EvenPositiveInt::fromInt(6);
199
189
```
200
190
191
+
Composite value object (with nullable fields)
192
+
--------------------------------------------
193
+
194
+
You can compose several primitive value objects into a richer domain object. The example below shows a simple Profile value object that uses multiple primitives and also supports nullable fields.
195
+
196
+
```php
197
+
<?php
198
+
declare(strict_types=1);
199
+
200
+
namespace App\Domain;
201
+
202
+
use PhpTypedValues\Integer\PositiveInt;
203
+
use PhpTypedValues\String\NonEmptyStr;
204
+
use PhpTypedValues\Float\NonNegativeFloat;
205
+
use PhpTypedValues\DateTime\DateTimeAtom;
206
+
207
+
final class Profile
208
+
{
209
+
public function __construct(
210
+
public readonly PositiveInt $id,
211
+
public readonly NonEmptyStr $firstName,
212
+
public readonly NonEmptyStr $lastName,
213
+
public readonly ?NonEmptyStr $middleName, // nullable field
214
+
public readonly ?DateTimeAtom $birthDate, // nullable field
215
+
public readonly ?NonNegativeFloat $heightM // nullable field
216
+
) {}
217
+
218
+
// Convenience named constructor that accepts raw scalars and builds primitives internally
219
+
public static function fromScalars(
220
+
int $id,
221
+
string $firstName,
222
+
string $lastName,
223
+
?string $middleName,
224
+
?string $birthDateAtom, // e.g. "2025-01-02T03:04:05+00:00"
0 commit comments