-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #454 from patchlevel/projector-id
Add ProjectorId as VO
- Loading branch information
Showing
11 changed files
with
233 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Projection\Projector; | ||
|
||
use InvalidArgumentException; | ||
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId; | ||
|
||
use function array_pop; | ||
use function count; | ||
use function ctype_digit; | ||
use function explode; | ||
use function implode; | ||
use function sprintf; | ||
|
||
/** @psalm-immutable */ | ||
final class ProjectorId | ||
{ | ||
public function __construct( | ||
private readonly string $name, | ||
private readonly int $version, | ||
) { | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return sprintf('%s-%s', $this->name, $this->version); | ||
} | ||
|
||
public function name(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function version(): int | ||
{ | ||
return $this->version; | ||
} | ||
|
||
public function equals(self $other): bool | ||
{ | ||
return $this->name === $other->name && $this->version === $other->version; | ||
} | ||
|
||
public static function fromString(string $value): self | ||
{ | ||
$parts = explode('-', $value); | ||
|
||
if (count($parts) < 2) { | ||
throw new InvalidArgumentException(); | ||
} | ||
|
||
$version = array_pop($parts); | ||
|
||
if (!ctype_digit($version)) { | ||
throw new InvalidArgumentException(); | ||
} | ||
|
||
$name = implode('-', $parts); | ||
|
||
return new self($name, (int)$version); | ||
} | ||
|
||
public function toProjectionId(): ProjectionId | ||
{ | ||
return new ProjectionId($this->name, $this->version); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.