diff --git a/README.md b/README.md index a31141b..d6c45e7 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,34 @@ $example->id instanceof Snowflake; // true echo $example->id; // 65898467809951744 ``` +### Usage with Livewire + +If you're using Livewire, you can use the `SnowflakeSynth` synthesizer to +make Snowflakes usable in your components. + +All you need to do is to register the synthesizer in your `AppServiceProvider`: + +```php +use Glhd\Bits\Support\Livewire\SnowflakeSynth; +use Glhd\Bits\Support\Livewire\BitsSynth; + +class AppServiceProvider extends ServiceProvider +{ + public function boot(): void + { + // If only using Snowflakes: + Livewire::propertySynthesizer(SnowflakeSynth::class); + + // If using Sonyflakes or a custom Bits variant: + Livewire::propertySynthesizer(BitsSynth::class); + } +} +``` + +If you're using a non-Snowflake variant of Bits, you can use the `BitsSynth` +instead, which supports any version of Bits at the cost of storing a little +more data. + ## About 64-bit Unique IDs ### Snowflake format diff --git a/src/Support/Livewire/BitsSynth.php b/src/Support/Livewire/BitsSynth.php new file mode 100644 index 0000000..1fccd37 --- /dev/null +++ b/src/Support/Livewire/BitsSynth.php @@ -0,0 +1,31 @@ +jsonSerialize(), ['class' => $target::class]]; + } + + /** + * @param string $value + * @param array{ class: class-string } $meta + */ + public function hydrate($value, $meta) + { + return $meta['class']::fromId($value); + } +} diff --git a/src/Support/Livewire/SnowflakeSynth.php b/src/Support/Livewire/SnowflakeSynth.php new file mode 100644 index 0000000..2587454 --- /dev/null +++ b/src/Support/Livewire/SnowflakeSynth.php @@ -0,0 +1,27 @@ +jsonSerialize(), []]; + } + + public function hydrate($value) + { + return Snowflake::fromId($value); + } +}