Skip to content

Commit

Permalink
Merge pull request #29 from Jeckel-Lab/feature/split-vo-interfaces
Browse files Browse the repository at this point in the history
Simplify ValueObject Interface
  • Loading branch information
jeckel authored Dec 20, 2021
2 parents d18f366 + 26872d7 commit 45f2d61
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
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;
}

0 comments on commit 45f2d61

Please sign in to comment.