-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
139 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,9 @@ | |
use Streak\Domain; | ||
|
||
/** | ||
* UUID v4. | ||
* | ||
* @author Alan Gabriel Bem <[email protected]> | ||
* | ||
* @see https://gist.github.com/tdomarkas/c5fbc10385ae004cbde6 | ||
*/ | ||
class UUID implements Domain\Id | ||
abstract class UUID implements Domain\Id | ||
{ | ||
private $value; | ||
|
||
|
@@ -46,14 +42,7 @@ final public function __construct(string $value) | |
$this->value = $value; | ||
} | ||
|
||
public static function create() | ||
{ | ||
$uuid = \Ramsey\Uuid\Uuid::uuid4()->toString(); | ||
|
||
return new static($uuid); | ||
} | ||
|
||
public function equals($uuid) : bool | ||
final public function equals($uuid) : bool | ||
{ | ||
if (!$uuid instanceof self) { | ||
return false; | ||
|
@@ -66,13 +55,13 @@ public function equals($uuid) : bool | |
return true; | ||
} | ||
|
||
public function toString() : string | ||
final public function toString() : string | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public static function fromString(string $uuid) : Domain\Id | ||
final public static function fromString(string $uuid) : Domain\Id | ||
{ | ||
return new self($uuid); | ||
return new static($uuid); | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the streak package. | ||
* | ||
* (C) Alan Gabriel Bem <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Streak\Domain\Id; | ||
|
||
/** | ||
* UUID v3 | ||
* | ||
* @author Alan Gabriel Bem <[email protected]> | ||
*/ | ||
abstract class UUID3 extends UUID | ||
{ | ||
/** | ||
* Namespace UUID required for UUID v5 generation. | ||
* | ||
* @return UUID | ||
*/ | ||
abstract protected static function namespace() : UUID; | ||
|
||
/** | ||
* Generates deterministic UUID based on MD5 of static namespace (also UUID) and name. | ||
* | ||
* Use it only if backward compatibility with e.g. outside system is required, use v5 otherwise. | ||
* | ||
* @param string $name | ||
* | ||
* @return static | ||
*/ | ||
final public static function create(string $name) | ||
{ | ||
$uuid = \Ramsey\Uuid\Uuid::uuid3(self::namespace()->toString(), $name)->toString(); | ||
|
||
return new static($uuid); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the streak package. | ||
* | ||
* (C) Alan Gabriel Bem <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Streak\Domain\Id; | ||
|
||
/** | ||
* UUID v4 | ||
* | ||
* @author Alan Gabriel Bem <[email protected]> | ||
*/ | ||
class UUID4 extends UUID | ||
{ | ||
/** | ||
* Generates (pseudo-)random UUID. | ||
* | ||
* @return UUID4 | ||
*/ | ||
final public static function create() | ||
{ | ||
$uuid = \Ramsey\Uuid\Uuid::uuid4()->toString(); | ||
|
||
return new static($uuid); | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the streak package. | ||
* | ||
* (C) Alan Gabriel Bem <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Streak\Domain\Id; | ||
|
||
/** | ||
* UUID v5 | ||
* | ||
* @author Alan Gabriel Bem <[email protected]> | ||
*/ | ||
abstract class UUID5 extends UUID | ||
{ | ||
/** | ||
* Namespace UUID required for UUID v5 generation. | ||
* | ||
* @return UUID | ||
*/ | ||
abstract protected static function namespace() : UUID; | ||
|
||
/** | ||
* Generates deterministic UUID based on SHA1 of static namespace (also UUID) and name. | ||
* | ||
* @param string $name | ||
* | ||
* @return static | ||
*/ | ||
final public static function create(string $name) | ||
{ | ||
$uuid = \Ramsey\Uuid\Uuid::uuid5(self::namespace()->toString(), $name)->toString(); | ||
|
||
return new static($uuid); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
use Streak\Domain\EventStore; | ||
use Streak\Domain\Exception; | ||
use Streak\Domain\Id\UUID; | ||
use Streak\Domain\Id\UUID4; | ||
|
||
/** | ||
* @author Alan Gabriel Bem <[email protected]> | ||
|
@@ -147,7 +148,7 @@ public function add(Domain\Id $producerId, ?int $version, Event ...$events) : vo | |
throw new Exception\EventAlreadyInStore($event); | ||
} | ||
|
||
$uuid = UUID::create(); | ||
$uuid = UUID4::create(); | ||
|
||
$version = $this->bumpUp($version); | ||
$row = $this->toRow($producerId, $version, $uuid, $event); | ||
|
@@ -199,7 +200,7 @@ public function add(Domain\Id $producerId, ?int $version, Event ...$events) : vo | |
$sequence = (string) $returned['number']; | ||
$uuid = $returned['uuid']; | ||
$uuid = mb_strtoupper($uuid); | ||
$uuid = new UUID($uuid); | ||
$uuid = new UUID4($uuid); | ||
$metadata = $returned['metadata']; | ||
$metadata = json_decode($metadata, true); | ||
$metadata = Event\Metadata::fromArray($metadata); | ||
|
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