generated from rawilk/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 17
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 #68 from rawilk/hash-key-generator
[Feature]: Hash key generator
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Support\KeyGenerators; | ||
|
||
use Rawilk\Settings\Contracts\ContextSerializer; | ||
use Rawilk\Settings\Contracts\KeyGenerator as KeyGeneratorContract; | ||
use Rawilk\Settings\Support\Context; | ||
use RuntimeException; | ||
|
||
class HashKeyGenerator implements KeyGeneratorContract | ||
{ | ||
protected ContextSerializer $serializer; | ||
|
||
public function generate(string $key, ?Context $context = null): string | ||
{ | ||
return hash( | ||
config('settings.hash_algorithm', 'xxh128'), | ||
$key . $this->serializer->serialize($context), | ||
); | ||
} | ||
|
||
public function removeContextFromKey(string $key): string | ||
{ | ||
throw new RuntimeException('HashKeyGenerator does not support removing the context from the key.'); | ||
} | ||
|
||
public function setContextSerializer(ContextSerializer $serializer): static | ||
{ | ||
$this->serializer = $serializer; | ||
|
||
return $this; | ||
} | ||
|
||
public function contextPrefix(): string | ||
{ | ||
throw new RuntimeException('HashKeyGenerator does not support a context prefix.'); | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
use Rawilk\Settings\Support\Context; | ||
use Rawilk\Settings\Support\ContextSerializers\ContextSerializer; | ||
use Rawilk\Settings\Support\ContextSerializers\DotNotationContextSerializer; | ||
use Rawilk\Settings\Support\KeyGenerators\HashKeyGenerator; | ||
|
||
beforeEach(function () { | ||
$this->keyGenerator = (new HashKeyGenerator) | ||
->setContextSerializer(new ContextSerializer); | ||
|
||
config([ | ||
'settings.hash_algorithm' => 'xxh128', | ||
]); | ||
}); | ||
|
||
it('generates a hash of a key', function () { | ||
// N; is for a serialized null context object | ||
expect($this->keyGenerator->generate('my-key'))->toBe(hash('xxh128', 'my-keyN;')); | ||
}); | ||
|
||
it('generates a hash of a key and context object', function () { | ||
$context = new Context([ | ||
'id' => 123, | ||
]); | ||
|
||
expect($this->keyGenerator->generate('my-key', $context)) | ||
->toBe(hash('xxh128', 'my-key' . serialize($context))); | ||
}); | ||
|
||
it('works with other context serializers', function () { | ||
$this->keyGenerator->setContextSerializer(new DotNotationContextSerializer); | ||
|
||
$context = new Context([ | ||
'id' => 123, | ||
'bool-value' => false, | ||
]); | ||
|
||
expect($this->keyGenerator->generate('my-key', $context)) | ||
->toBe(hash('xxh128', 'my-keyid:123::bool-value:0')); | ||
}); |