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
Extend `Gears\value-object\AbstractValueObject`. Make your class final, value objects should always be final
33
+
Extend from `Gears\value-object\AbstractValueObject`. Make your class final, value objects should always be final
34
34
35
-
Be aware constructor is protected, you should create a "named constructor" for your value object
35
+
Be aware of the protected constructor, you should create a "named constructor" for your value object
36
36
37
37
```php
38
-
use Gears\value-object\AbstractValueObject;
38
+
use Gears\ValueObject\AbstractValueObject;
39
39
40
-
final class CustomValueObject extends AbstractValueObject
40
+
final class CustomStringValueObject extends AbstractValueObject
41
41
{
42
42
private $value;
43
43
44
44
public static function fromString(string $value)
45
45
{
46
-
$valueObject = new self();
47
-
$valueObject->value = $value;
46
+
$stringObject = new self();
47
+
$stringObject->value = $value;
48
48
49
-
return $valueObject;
49
+
return $stringObject;
50
50
}
51
51
52
52
public function getValue(): string
@@ -61,6 +61,57 @@ final class CustomValueObject extends AbstractValueObject
61
61
}
62
62
```
63
63
64
+
### Serialization
65
+
66
+
Extending AbstractValueObject does not automatically define serialization mechanisms in your value objects because value objects can be composed of several values, other value objects and even other objects such as enums
67
+
68
+
For this consider adding serialization methods in your value objects to control how serialization takes place
69
+
70
+
```php
71
+
use Gears\ValueObject\AbstractValueObject;
72
+
73
+
final class Money extends AbstractValueObject implements \Serializable
74
+
{
75
+
private const CURRENCY_EUR = 'eur';
76
+
77
+
private $value;
78
+
private $precision;
79
+
private $currency;
80
+
81
+
public static function fromEuro(int $value, int $precision)
82
+
{
83
+
$money = new self();
84
+
$money->value = $value;
85
+
$money->precision = $precision;
86
+
$money->currency = static::CURRENCY_EUR; // Should be an enum
Enums and Value Objects get along perfectly, consider using [phpgears/enum](https://github.com/phpgears/enum) for enumerations
114
+
64
115
## Contributing
65
116
66
117
Found a bug or have a feature request? [Please open a new issue](https://github.com/phpgears/value-object/issues). Have a look at existing issues before.
0 commit comments