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: README.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,8 @@ On top of those base traits **Complex Heart** provide ready to use compositions:
30
30
31
31
-**Type-Safe Factory Method**: The `make()` static factory validates constructor parameters at runtime with clear error messages
32
32
-**Automatic Invariant Checking**: When using `make()`, Value Objects and Entities automatically validate invariants after construction (no manual `$this->check()` needed)
33
+
-**Named Parameter Support**: Full support for PHP 8.0+ named parameters for improved readability and flexibility
34
+
-**Union Type Support**: Complete support for PHP 8.0+ union types (e.g., `int|float`, `string|null`)
33
35
-**Readonly Properties Support**: Full compatibility with PHP 8.1+ readonly properties
34
36
-**PHPStan Level 8**: Complete static analysis support
Copy file name to clipboardExpand all lines: wiki/Domain-Modeling-Aggregates.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -87,10 +87,14 @@ final class Order implements Aggregate
87
87
88
88
**Benefits of using `make()` in factory methods:**
89
89
- Automatic invariant checking when using `make()`
90
-
- Type validation at runtime
90
+
- Type validation at runtime with clear error messages
91
+
- Named parameter support for improved readability (as shown above)
92
+
- Union type support (e.g., `int|float`, `string|null`)
91
93
- Cleaner factory method code
92
94
- Consistent with Value Objects and Entities
93
95
96
+
**Why named parameters?** As shown in the example above, using named parameters (`reference:`, `customer:`, etc.) makes the code self-documenting and prevents parameter mix-ups, especially important in Aggregates with many constructor parameters.
97
+
94
98
**Important:** Auto-check ONLY works when using `make()`. In the alternative approach using direct constructor calls, you must manually call `$this->check()` inside the constructor.
95
99
96
100
#### Alternative: Direct Constructor with Manual Check
// Named parameters for improved readability (PHP 8.0+)
54
+
$customer = Customer::make(
55
+
id: UUIDValue::random(),
56
+
name: 'Vincent Vega'
57
+
);
52
58
```
53
59
54
60
**Benefits:**
55
61
- Automatic invariant checking when using `make()`
56
-
- Type validation at runtime
62
+
- Type validation at runtime with clear error messages
63
+
- Named parameter support for improved readability
64
+
- Union type support (e.g., `int|float`, `string|null`)
57
65
- Cleaner constructor code
58
66
59
67
**Important:** Auto-check ONLY works when using `make()`. If you call the constructor directly (`new Customer(...)`), you must manually call `$this->check()` inside the constructor.
- Runtime type validation with clear error messages
99
102
- Automatic invariant checking after construction
103
+
- Named parameter support for improved readability
104
+
- Union type support (e.g., `int|float`, `string|null`)
100
105
- Works seamlessly with readonly properties
101
106
- PHPStan level 8 compliant
102
107
103
108
**Important:** Auto-check ONLY works when using `make()`. Direct constructor calls do NOT trigger automatic invariant checking, so you must manually call `$this->check()` in the constructor.
104
109
110
+
#### Named Parameters Example
111
+
112
+
Named parameters (PHP 8.0+) make code more readable and allow parameters in any order:
113
+
114
+
```php
115
+
final class Money implements ValueObject
116
+
{
117
+
use IsValueObject;
118
+
119
+
public function __construct(
120
+
private readonly int|float $amount,
121
+
private readonly string $currency
122
+
) {}
123
+
124
+
protected function invariantPositiveAmount(): bool
0 commit comments