Skip to content

Commit 3d597ae

Browse files
committed
update docs
1 parent d5456d5 commit 3d597ae

1 file changed

Lines changed: 59 additions & 8 deletions

File tree

README.md

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
# value-object
1414

15-
Immutable value object for PHP
15+
Immutable Value Objects for PHP
1616

1717
## Installation
1818

@@ -30,23 +30,23 @@ Require composer autoload file
3030
require './vendor/autoload.php';
3131
```
3232

33-
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
3434

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
3636

3737
```php
38-
use Gears\value-object\AbstractValueObject;
38+
use Gears\ValueObject\AbstractValueObject;
3939

40-
final class CustomValueObject extends AbstractValueObject
40+
final class CustomStringValueObject extends AbstractValueObject
4141
{
4242
private $value;
4343

4444
public static function fromString(string $value)
4545
{
46-
$valueObject = new self();
47-
$valueObject->value = $value;
46+
$stringObject = new self();
47+
$stringObject->value = $value;
4848

49-
return $valueObject;
49+
return $stringObject;
5050
}
5151

5252
public function getValue(): string
@@ -61,6 +61,57 @@ final class CustomValueObject extends AbstractValueObject
6161
}
6262
```
6363

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
87+
88+
return $money;
89+
}
90+
91+
// [...]
92+
93+
final public function serialize(): string
94+
{
95+
return serialize([
96+
$this->value,
97+
$this->precision,
98+
$this->currency,
99+
]);
100+
}
101+
102+
public function unserialize($serialized): void
103+
{
104+
list(
105+
$this->value,
106+
$this->precision,
107+
$this->currency
108+
) = \unserialize($serialized, ['allowed_classes' => false]);
109+
}
110+
}
111+
```
112+
113+
Enums and Value Objects get along perfectly, consider using [phpgears/enum](https://github.com/phpgears/enum) for enumerations
114+
64115
## Contributing
65116

66117
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

Comments
 (0)