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.
Add support for custom value and context serializers
- Loading branch information
Showing
24 changed files
with
472 additions
and
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Contracts; | ||
|
||
use Rawilk\Settings\Support\Context; | ||
|
||
interface ContextSerializer | ||
{ | ||
public function serialize(Context $context = null): string; | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Contracts; | ||
|
||
use Rawilk\Settings\Support\Context; | ||
|
||
interface KeyGenerator | ||
{ | ||
public function generate(string $key, Context $context = null): string; | ||
|
||
public function setContextSerializer(ContextSerializer $serializer): self; | ||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Exceptions; | ||
|
||
use Exception; | ||
|
||
final class InvalidContextValue extends Exception | ||
{ | ||
public static function forKey(string $key): self | ||
{ | ||
return new self("An invalid context value was provided for key: {$key}. Only string, integer, and boolean values are allowed."); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Support\ContextSerializers; | ||
|
||
use Rawilk\Settings\Contracts\ContextSerializer as ContextSerializerContract; | ||
use Rawilk\Settings\Support\Context; | ||
|
||
class ContextSerializer implements ContextSerializerContract | ||
{ | ||
public function serialize(Context $context = null): string | ||
{ | ||
return serialize($context); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Support/ContextSerializers/DotNotationContextSerializer.php
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Support\ContextSerializers; | ||
|
||
use Rawilk\Settings\Contracts\ContextSerializer as ContextSerializerContract; | ||
use Rawilk\Settings\Support\Context; | ||
|
||
class DotNotationContextSerializer implements ContextSerializerContract | ||
{ | ||
public function serialize(Context $context = null): string | ||
{ | ||
if (is_null($context)) { | ||
return ''; | ||
} | ||
|
||
return collect($context->toArray()) | ||
->map(function ($value, string $key) { | ||
// Use the model's morph class when possible. | ||
$value = match ($key) { | ||
'model' => rescue(fn () => app($value)->getMorphClass(), $value), | ||
default => $value, | ||
}; | ||
|
||
if ($value === false) { | ||
$value = 0; | ||
} | ||
|
||
return "{$key}:{$value}"; | ||
}) | ||
->implode('::'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,26 @@ | ||
<?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; | ||
|
||
class Md5KeyGenerator implements KeyGeneratorContract | ||
{ | ||
protected ContextSerializer $serializer; | ||
|
||
public function generate(string $key, Context $context = null): string | ||
{ | ||
return md5($key . $this->serializer->serialize($context)); | ||
} | ||
|
||
public function setContextSerializer(ContextSerializer $serializer): self | ||
{ | ||
$this->serializer = $serializer; | ||
|
||
return $this; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Support\KeyGenerators; | ||
|
||
use Illuminate\Support\Str; | ||
use Rawilk\Settings\Contracts\ContextSerializer; | ||
use Rawilk\Settings\Contracts\KeyGenerator as KeyGeneratorContract; | ||
use Rawilk\Settings\Support\Context; | ||
|
||
class ReadableKeyGenerator implements KeyGeneratorContract | ||
{ | ||
protected ContextSerializer $serializer; | ||
|
||
public function generate(string $key, Context $context = null): string | ||
{ | ||
$key = $this->normalizeKey($key); | ||
|
||
if ($context) { | ||
$key .= ':c:::' . $this->serializer->serialize($context); | ||
} | ||
|
||
return $key; | ||
} | ||
|
||
public function setContextSerializer(ContextSerializer $serializer): KeyGeneratorContract | ||
{ | ||
$this->serializer = $serializer; | ||
|
||
return $this; | ||
} | ||
|
||
protected function normalizeKey(string $key): string | ||
{ | ||
// We want to preserve period characters in the key, however everything else is fair game | ||
// to convert to a slug. | ||
return Str::of($key) | ||
->replace('.', '-dot-') | ||
->slug() | ||
->replace('-dot-', '.') | ||
->__toString(); | ||
} | ||
} |
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.