Skip to content

Commit 89d25d1

Browse files
committed
improve docs and exception messages
1 parent 72e82a2 commit 89d25d1

File tree

8 files changed

+57
-19
lines changed

8 files changed

+57
-19
lines changed

README.md

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Require composer autoload file
3030
require './vendor/autoload.php';
3131
```
3232

33-
By extending `Gears\Identity\AbstractIdentity` you can easily have an Identity classes
33+
By extending `Gears\Identity\AbstractIdentity` you can easily have an Identity class
3434

3535
```php
3636
use Gears\Identity\AbstractIdentity;
@@ -46,16 +46,54 @@ class CustomIdentity extends AbstractIdentity
4646
}
4747
```
4848

49-
### UUID implementations
49+
### Available implementations
5050

5151
Due to its popularity UUID based identity implementations are provided
5252

53-
Main direct UUID value implementation is available in class `Gears\Identity\UuidIdentity`
53+
##### UuidIdentity
5454

55-
If you want a more concise UUID based identities you can use any of the following
55+
```php
56+
use Gears\Identity\UuidIdentity;
57+
use Ramsey\Uuid\Uuid;
58+
59+
$uuid = Uuid::uuid4()->toString();
60+
$identity = UuidIdentity::fromString($uuid);
61+
```
62+
63+
If you want a more concise UUID based identities you can use any of the following:
64+
65+
##### ShortUuidIdentity
66+
67+
You need to require [pascaldevink/shortuuid](https://github.com/pascaldevink/shortuuid)
68+
69+
```php
70+
use Gears\Identity\ShortUuidIdentity;
71+
use PascalDeVink\ShortUuid\ShortUuid;
72+
73+
$shortUuid = new ShortUuid();
74+
$identity = ShortUuidIdentity::fromString($shortUuid->uuid4());
5675

57-
* `Gears\Identity\ShortUuidIdentity`. You need to composer require [pascaldevink/shortuuid](https://github.com/pascaldevink/shortuuid)
58-
* `Gears\Identity\HashUuidIdentity`. You need to composer require [hashids/hashids](https://github.com/ivanakimov/hashids.php). Be aware that original UUID should be hashed as hexadecimal strings, so no dashes, review Hashids documentation
76+
// Should you need to get original UUID
77+
$originalUuid = $shortUuid->decode($identity->getValue())->toString();
78+
```
79+
80+
##### HashUuidIdentity
81+
82+
You need to require [hashids/hashids](https://github.com/ivanakimov/hashids.php)
83+
84+
```php
85+
use Gears\Identity\HashUuidIdentity;
86+
use Hashids\Hashids;
87+
use Ramsey\Uuid\Uuid;
88+
89+
$hashIds = new Hashids();
90+
$uuid = Uuid::uuid4()->toString();
91+
$hashedUuid = $hashIds->encodeHex(\str_replace('-', '', $uuid));
92+
$identity = HashUuidIdentity::fromString($hashedUuid);
93+
94+
// Should you need to get original UUID, mind that UUID has no dash separators
95+
$originalUuid = $hashIds->decodeHex($identity->getValue());
96+
```
5997

6098
## Contributing
6199

src/AbstractIdentity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ final public function __toString(): string
7070
/**
7171
* {@inheritdoc}
7272
*/
73-
public function serialize(): string
73+
final public function serialize(): string
7474
{
7575
return \serialize($this->value);
7676
}
@@ -80,7 +80,7 @@ public function serialize(): string
8080
*
8181
* @param mixed $serialized
8282
*/
83-
public function unserialize($serialized): void
83+
final public function unserialize($serialized): void
8484
{
8585
$this->value = \unserialize($serialized, [static::class]);
8686
}

src/HashUuidIdentity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ public static function fromString(string $value)
3434
$uuid = Uuid::fromString((new Hashids())->decodeHex($value));
3535
} catch (InvalidUuidStringException $exception) {
3636
throw new InvalidIdentityException(
37-
\sprintf('Provided identifier "%s" is not a valid hashed UUID', $value),
37+
\sprintf('Provided identity value "%s" is not a valid hashed UUID', $value),
3838
0,
3939
$exception
4040
);
4141
}
4242

4343
if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) {
4444
throw new InvalidIdentityException(
45-
\sprintf('Provided identifier "%s" is not a valid hashed UUID', $value)
45+
\sprintf('Provided identity value "%s" is not a valid hashed UUID', $value)
4646
);
4747
}
4848

src/ShortUuidIdentity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function fromString(string $value)
3232
$uuid = (new ShortUuid())->decode($value);
3333
if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) {
3434
throw new InvalidIdentityException(
35-
\sprintf('Provided identifier "%s" is not a valid short UUID', $value)
35+
\sprintf('Provided identity value "%s" is not a valid short UUID', $value)
3636
);
3737
}
3838

src/UuidIdentity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public static function fromString(string $value)
3333
$uuid = Uuid::fromString($value);
3434
} catch (InvalidUuidStringException $exception) {
3535
throw new InvalidIdentityException(
36-
\sprintf('Provided identifier "%s" is not a valid UUID', $value),
36+
\sprintf('Provided identity value "%s" is not a valid UUID', $value),
3737
0,
3838
$exception
3939
);
4040
}
4141

4242
if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) {
4343
throw new InvalidIdentityException(
44-
\sprintf('Provided identifier "%s" is not a valid UUID', $value)
44+
\sprintf('Provided identity value "%s" is not a valid UUID', $value)
4545
);
4646
}
4747

tests/Identity/HashUuidIdentityTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testCreation(): void
3232

3333
/**
3434
* @expectedException \Gears\Identity\Exception\InvalidIdentityException
35-
* @expectedExceptionMessage Provided identifier "invalidHashedUUID" is not a valid hashed UUID
35+
* @expectedExceptionMessage Provided identity value "invalidHashedUUID" is not a valid hashed UUID
3636
*/
3737
public function testInvalidShortUuid(): void
3838
{
@@ -41,7 +41,7 @@ public function testInvalidShortUuid(): void
4141

4242
/**
4343
* @expectedException \Gears\Identity\Exception\InvalidIdentityException
44-
* @expectedExceptionMessage Provided identifier "gGJqXEqR7AFZjBkzP9MLtWYP9AA" is not a valid hashed UUID
44+
* @expectedExceptionMessage Provided identity value "gGJqXEqR7AFZjBkzP9MLtWYP9AA" is not a valid hashed UUID
4545
*/
4646
public function testNonRFC4122Uuid(): void
4747
{

tests/Identity/ShortUuidIdentityTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testCreation(): void
3232

3333
/**
3434
* @expectedException \Gears\Identity\Exception\InvalidIdentityException
35-
* @expectedExceptionMessage Provided identifier "invalidShortUUID" is not a valid short UUID
35+
* @expectedExceptionMessage Provided identity value "invalidShortUUID" is not a valid short UUID
3636
*/
3737
public function testInvalidShortUuid(): void
3838
{
@@ -41,7 +41,7 @@ public function testInvalidShortUuid(): void
4141

4242
/**
4343
* @expectedException \Gears\Identity\Exception\InvalidIdentityException
44-
* @expectedExceptionMessage Provided identifier "zaDP55gm3yL9cV2D" is not a valid short UUID
44+
* @expectedExceptionMessage Provided identity value "zaDP55gm3yL9cV2D" is not a valid short UUID
4545
*/
4646
public function testNonRFC4122Uuid(): void
4747
{

tests/Identity/UuidIdentityTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testCreation(): void
3232

3333
/**
3434
* @expectedException \Gears\Identity\Exception\InvalidIdentityException
35-
* @expectedExceptionMessage Provided identifier "invalidUUID" is not a valid UUID
35+
* @expectedExceptionMessage Provided identity value "invalidUUID" is not a valid UUID
3636
*/
3737
public function testInvalidUuid(): void
3838
{
@@ -41,7 +41,7 @@ public function testInvalidUuid(): void
4141

4242
/**
4343
* @expectedException \Gears\Identity\Exception\InvalidIdentityException
44-
* @expectedExceptionMessage Provided identifier "00000000-07bf-961b-abd8-c4716f92fcc0" is not a valid UUID
44+
* @expectedExceptionMessage Provided identity value "00000000-07bf-961b-abd8-c4716f92fcc0" is not a valid UUID
4545
*/
4646
public function testNonRFC4122Uuid(): void
4747
{

0 commit comments

Comments
 (0)