@@ -15,17 +15,22 @@ The `SingleValueObjectTrait` implements these methods for you, so in
1515most cases you can just define your class like this and it will work:
1616
1717``` php
18- class MySimpleValueObject implements SingleValueObjectInterface
18+ use SolidPhp\ValueObjects\Value\SingleValueObjectInterface;
19+ use SolidPhp\ValueObjects\Value\SingleValueObjectTrait;
20+
21+ class EmailAddress implements SingleValueObjectInterface
1922{
2023 use SingleValueObjectTrait;
2124}
2225```
2326
24- or extend the ` SimpleValueObject ` abstract class, which is provided for
27+ or extend the ` SingleValueObject ` abstract class, which is provided for
2528convenience and is basically just a shorthand for the above code block:
2629
2730``` php
28- class MySimpleValueObject extends SimpleValueObject
31+ use SolidPhp\ValueObjects\Value\SingleValueObject;
32+
33+ class EmailAddress extends SingleValueObject
2934{
3035}
3136```
@@ -38,13 +43,16 @@ case, you can override the `validateRawValue` method from `SingleValueObjectTrai
3843add any validation you might need:
3944
4045``` php
41- class Email implements SingleValueObjectInterface
46+ use SolidPhp\ValueObjects\Value\SingleValueObjectInterface;
47+ use SolidPhp\ValueObjects\Value\SingleValueObjectTrait;
48+
49+ class EmailAddress implements SingleValueObjectInterface
4250{
4351 use SingleValueObjectTrait;
4452
4553 protected static function validateRawValue($rawValue): void
4654 {
47- if (0 === preg_match('/\w+\@\w.com/', $rawValue) {
55+ if (0 === preg_match('/\w+\@\w.com/', $rawValue)) {
4856 throw new DomainException('Not a valid e-mail address');
4957 }
5058 }
@@ -65,18 +73,22 @@ object, for example to trim any whitespace. For this, you can
6573override the ` normalizeValidRawValue ` method from ` SingleValueObjectTrait ` :
6674
6775``` php
68- class LastName implements SingleValueObjectInterface
76+ use SolidPhp\ValueObjects\Value\SingleValueObjectInterface;
77+ use SolidPhp\ValueObjects\Value\SingleValueObjectTrait;
78+
79+ class EmailAddress implements SingleValueObjectInterface
6980{
7081 use SingleValueObjectTrait;
7182
72- protected static function normalizeValidRawValue($validRawValue)
83+ protected static function normalizeValidRawValue($validRawValue): string
7384 {
7485 return trim($validRawValue);
7586 }
7687}
7788```
7889
79- ` normalizeValidRawValue ` is called after validation, so
90+ ` normalizeValidRawValue ` is called after validation, so you can assume
91+ the parameter is valid according to the rules of your class.
8092
8193Considerations
8294--------------
@@ -86,4 +98,3 @@ value object also apply here.
8698
8799- Don't mutate instance properties
88100- Don't deserialize directly; always use the ` of ` factory method
89- - Use only scalar values; no objects or arrays.
0 commit comments