-
Notifications
You must be signed in to change notification settings - Fork 0
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
11 changed files
with
261 additions
and
16 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
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,51 @@ | ||
<?php | ||
|
||
/* | ||
* aggregate (https://github.com/phpgears/aggregate). | ||
* Aggregate base. | ||
* | ||
* @license MIT | ||
* @link https://github.com/phpgears/aggregate | ||
* @author Julián Gutiérrez <juliangut@gmail.com> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gears\Aggregate; | ||
|
||
use Gears\Identity\Exception\InvalidIdentityException; | ||
use Hashids\Hashids; | ||
use Ramsey\Uuid\Exception\InvalidUuidStringException; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
/** | ||
* Base immutable hashed UUID aggregate identity. | ||
*/ | ||
class HashUuidAggregateIdentity extends AbstractAggregateIdentity | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws InvalidIdentityException | ||
*/ | ||
public static function fromString(string $value) | ||
{ | ||
try { | ||
$uuid = Uuid::fromString((new Hashids())->decodeHex($value)); | ||
} catch (InvalidUuidStringException $exception) { | ||
throw new InvalidIdentityException( | ||
\sprintf('Provided identity value "%s" is not a valid hashed UUID', $value), | ||
0, | ||
$exception | ||
); | ||
} | ||
|
||
if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) { | ||
throw new InvalidIdentityException( | ||
\sprintf('Provided identity value "%s" is not a valid hashed UUID', $value) | ||
); | ||
} | ||
|
||
return new static($value); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
/* | ||
* aggregate (https://github.com/phpgears/aggregate). | ||
* Aggregate base. | ||
* | ||
* @license MIT | ||
* @link https://github.com/phpgears/aggregate | ||
* @author Julián Gutiérrez <juliangut@gmail.com> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gears\Aggregate; | ||
|
||
use Gears\Identity\Exception\InvalidIdentityException; | ||
use PascalDeVink\ShortUuid\ShortUuid; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
/** | ||
* Base immutable short UUID aggregate identity. | ||
*/ | ||
class ShortUuidAggregateIdentity extends AbstractAggregateIdentity | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws InvalidIdentityException | ||
*/ | ||
public static function fromString(string $value) | ||
{ | ||
$uuid = (new ShortUuid())->decode($value); | ||
if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) { | ||
throw new InvalidIdentityException( | ||
\sprintf('Provided identity value "%s" is not a valid short UUID', $value) | ||
); | ||
} | ||
|
||
return new static($value); | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
/* | ||
* aggregate (https://github.com/phpgears/aggregate). | ||
* Aggregate base. | ||
* | ||
* @license MIT | ||
* @link https://github.com/phpgears/aggregate | ||
* @author Julián Gutiérrez <juliangut@gmail.com> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gears\Aggregate\Tests; | ||
|
||
use Gears\Aggregate\HashUuidAggregateIdentity; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Hashed UUID aggregate identity test. | ||
*/ | ||
class HashUuidAggregateIdentityTest extends TestCase | ||
{ | ||
public function testCreation(): void | ||
{ | ||
$hashedUuid = 'gqYxv3lMPXSEkGoonzDZtMNwE4Q'; | ||
$stub = HashUuidAggregateIdentity::fromString($hashedUuid); | ||
|
||
$this->assertSame($hashedUuid, $stub->getValue()); | ||
$this->assertSame($hashedUuid, (string) $stub); | ||
} | ||
|
||
/** | ||
* @expectedException \Gears\Identity\Exception\InvalidIdentityException | ||
* @expectedExceptionMessage Provided identity value "invalidHashedUUID" is not a valid hashed UUID | ||
*/ | ||
public function testInvalidShortUuid(): void | ||
{ | ||
HashUuidAggregateIdentity::fromString('invalidHashedUUID'); | ||
} | ||
|
||
/** | ||
* @expectedException \Gears\Identity\Exception\InvalidIdentityException | ||
* @expectedExceptionMessage Provided identity value "gGJqXEqR7AFZjBkzP9MLtWYP9AA" is not a valid hashed UUID | ||
*/ | ||
public function testNonRFC4122Uuid(): void | ||
{ | ||
HashUuidAggregateIdentity::fromString('gGJqXEqR7AFZjBkzP9MLtWYP9AA'); | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
/* | ||
* aggregate (https://github.com/phpgears/aggregate). | ||
* Aggregate base. | ||
* | ||
* @license MIT | ||
* @link https://github.com/phpgears/aggregate | ||
* @author Julián Gutiérrez <juliangut@gmail.com> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gears\Aggregate\Tests; | ||
|
||
use Gears\Aggregate\ShortUuidAggregateIdentity; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Short UUID aggregate identity test. | ||
*/ | ||
class ShortUuidAggregateIdentityTest extends TestCase | ||
{ | ||
public function testCreation(): void | ||
{ | ||
$shortUuid = 'QuwMZbb9f3ccpacCPmVRaF'; | ||
$stub = ShortUuidAggregateIdentity::fromString($shortUuid); | ||
|
||
$this->assertSame($shortUuid, $stub->getValue()); | ||
$this->assertSame($shortUuid, (string) $stub); | ||
} | ||
|
||
/** | ||
* @expectedException \Gears\Identity\Exception\InvalidIdentityException | ||
* @expectedExceptionMessage Provided identity value "invalidShortUUID" is not a valid short UUID | ||
*/ | ||
public function testInvalidShortUuid(): void | ||
{ | ||
ShortUuidAggregateIdentity::fromString('invalidShortUUID'); | ||
} | ||
|
||
/** | ||
* @expectedException \Gears\Identity\Exception\InvalidIdentityException | ||
* @expectedExceptionMessage Provided identity value "zaDP55gm3yL9cV2D" is not a valid short UUID | ||
*/ | ||
public function testNonRFC4122Uuid(): void | ||
{ | ||
ShortUuidAggregateIdentity::fromString('zaDP55gm3yL9cV2D'); | ||
} | ||
} |
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