Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify ValueObject Interface #29

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,20 @@ Value object must be defined as:

Think about implementing it like this:
```Php
final class Speed implements ValueObject
final class Speed implements ValueObject, ValueObjectFactory
{
private static $instances = [];

private function __constructor(private float $speed)
{
}

public function from(mixed $speedValue): static
/**
* @param mixed $value
* @return static
* @throws InvalidArgumentException
*/
public static function from(mixed $speedValue): static
{
if (! self::$instances[$speedValue]) {
if ($speedValue < 0) {
Expand Down
13 changes: 1 addition & 12 deletions src/Domain/ValueObject/ValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,10 @@

namespace JeckelLab\Contract\Domain\ValueObject;

use JeckelLab\Contract\Domain\Equality;
use JeckelLab\Contract\Domain\ValueObject\Exception\InvalidArgumentException;
use JsonSerializable;
use Stringable;

/**
* Interface ValueObject
* @psalm-immutable
*/
interface ValueObject extends Stringable, Equality, JsonSerializable
interface ValueObject
{
/**
* @param mixed $value
* @return static
* @throws InvalidArgumentException
*/
public static function from(mixed $value): static;
}
18 changes: 18 additions & 0 deletions src/Domain/ValueObject/ValueObjectFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace JeckelLab\Contract\Domain\ValueObject;

use JeckelLab\Contract\Domain\ValueObject\Exception\InvalidArgumentException;

/**
* @psalm-immutable
*/
interface ValueObjectFactory
{
/**
* @param mixed $value
* @return static
* @throws InvalidArgumentException
*/
public static function from(mixed $value): static;
}