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
+46-20Lines changed: 46 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
# PHP ^8.1 Value Objects Redux
2
2
3
-
Opinionated PHP immutable value object example with deep nesting, `\JsonSerializable`, `\IteratorAggregate`, `snake_case` and `camelCase`. Please see source and tests for more details.
3
+
Opinionated PHP immutable value object example with deep nesting, `\JsonSerializable`, `\IteratorAggregate`, `snake_case` and `camelCase`. Please see `example-php`, source and tests for more details.
4
4
5
5
- simple and short immutable value objects
6
-
- plain value objects have 2 methods `fromNative(...)` and `jsonSerialize()`
6
+
- plain value objects have 4 methods `fromNative(...)`, `toNative()`, `equals(...)` and `jsonSerialize()`
7
7
- records have additional 2 methods `with(iterable|self $data)` and `getIterator()`
8
8
- records are traversable, you can iterate over the properties
9
9
- union types for creating value objects from scalar or same value object type
@@ -14,9 +14,11 @@ Opinionated PHP immutable value object example with deep nesting, `\JsonSerializ
14
14
**Example for a String value object:**
15
15
16
16
```php
17
-
final class FirstName implements Immutable, \Stringable
17
+
final readonly class FirstName implements Immutable, \Stringable
18
18
{
19
-
private function __construct(public readonly string $val)
19
+
use FuncTrait;
20
+
21
+
private function __construct(public readonly string $v)
20
22
{
21
23
}
22
24
@@ -25,49 +27,61 @@ final class FirstName implements Immutable, \Stringable
25
27
return $firstName instanceof self ? $firstName : new self($firstName);
26
28
}
27
29
30
+
public function toNative(): string
31
+
{
32
+
return $this->jsonSerialize();
33
+
}
34
+
28
35
public function jsonSerialize(): string
29
36
{
30
-
return $this->val;
37
+
return $this->v;
31
38
}
32
39
33
40
public function __toString(): string
34
41
{
35
-
return $this->val;
42
+
return $this->v;
36
43
}
37
44
}
38
45
```
39
46
40
47
**Example for a date value object:**
41
48
42
49
```php
43
-
final class LastLogin implements \Stringable, Immutable
50
+
final readonly class LastLogin implements \Stringable, Immutable
44
51
{
52
+
use FuncTrait;
53
+
45
54
private const OUTPUT_FORMAT = 'Y-m-d\TH:i:sP';
46
55
47
-
public static function fromNative(string|int|DateTimeImmutable|self $val): self
56
+
public static function fromNative(string|int|DateTimeImmutable|self $v): self
48
57
{
49
-
switch (\gettype($val)) {
58
+
switch (\gettype($v)) {
50
59
case 'integer':
51
-
$datetime = (new DateTimeImmutable())->setTimestamp($val);
60
+
$datetime = (new DateTimeImmutable())->setTimestamp($v);
52
61
53
62
break;
54
63
case 'string':
55
-
$datetime = new DateTimeImmutable($val);
64
+
$datetime = new DateTimeImmutable($v);
56
65
57
66
break;
58
67
default:
59
-
if ($val instanceof self) {
60
-
return $val;
68
+
if ($v instanceof self) {
69
+
return $v;
61
70
}
62
-
$datetime = $val;
71
+
$datetime = $v;
63
72
64
73
break;
65
74
}
66
75
67
76
return new self(self::ensureUtc($datetime));
68
77
}
69
78
70
-
private function __construct(public readonly DateTimeImmutable $val)
79
+
public function toNative(): string
80
+
{
81
+
return $this->jsonSerialize();
82
+
}
83
+
84
+
private function __construct(public readonly DateTimeImmutable $v)
71
85
{
72
86
}
73
87
@@ -87,7 +101,7 @@ final class LastLogin implements \Stringable, Immutable
87
101
88
102
public function jsonSerialize(): string
89
103
{
90
-
return $this->val->format(self::OUTPUT_FORMAT);
104
+
return $this->v->format(self::OUTPUT_FORMAT);
91
105
}
92
106
}
93
107
```
@@ -97,6 +111,8 @@ final class LastLogin implements \Stringable, Immutable
97
111
```php
98
112
final class Account implements ImmutableRecord
99
113
{
114
+
use FuncTrait;
115
+
100
116
private static array $__objectKeys;
101
117
102
118
private function __construct(
@@ -108,11 +124,19 @@ final class Account implements ImmutableRecord
0 commit comments