### Installing Bits PHP Library Source: https://github.com/glhd/bits/blob/main/README.md This command installs the `glhd/bits` PHP library using Composer, the dependency manager for PHP. It adds the library to your project's `composer.json` and downloads its dependencies, making the library available for use. ```shell composer require glhd/bits ``` -------------------------------- ### Registering Livewire Property Synthesizers for Bits in PHP Source: https://github.com/glhd/bits/blob/main/README.md This code shows how to register property synthesizers for Livewire components to enable the use of Snowflake or other Bits variants. It provides examples for registering `SnowflakeSynth` for Snowflake-only usage or `BitsSynth` for broader compatibility with Sonyflakes or custom Bits variants within the `AppServiceProvider`. ```PHP use GlhdBitsSupportLivewireSnowflakeSynth; use GlhdBitsSupportLivewireBitsSynth; 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); } } ``` -------------------------------- ### Querying Eloquent Models by Snowflake ID Timestamp in PHP Source: https://github.com/glhd/bits/blob/main/README.md This example illustrates how to use Snowflake IDs as a substitute for `created_at` timestamps in Eloquent queries. It shows how to query records created after a specific time by comparing the Snowflake ID with a Snowflake generated for that timestamp, leveraging the time-sortable nature of Snowflakes. ```PHP // Instead of: User::where('created_at', '>', now()); // You can do (assuming users.id is a snowflake) User::where('id', '>', app(MakesSnowflakes::class)->firstForTimestamp(now())); ``` -------------------------------- ### Sonyflake ID Format Structure Source: https://github.com/glhd/bits/blob/main/README.md This diagram outlines the 64-bit structure of a Sonyflake ID. It details the components: a sign bit, a 39-bit timestamp, an 8-bit sequence number, and a 16-bit machine ID, explaining how these parts combine to form a unique and time-sortable identifier, optimized for different distribution needs compared to Snowflakes. ```Diagram 0 000000011001001011101011001101111001010 11010110 1111000000011101 ┳ ━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━┳━ ━┳━━━━━━━━━━━━━━ ┗━ sign ┗ timestamp (39) sequence (8) ┛ ┗ machine (16) ``` -------------------------------- ### Integrating Bits with Eloquent Models in PHP Source: https://github.com/glhd/bits/blob/main/README.md This snippet demonstrates how to integrate the 'Bits' library with Laravel Eloquent models. It shows how to use the `HasSnowflakes` trait for automatic ID generation on insertion and how to cast model attributes to `Snowflake` instances using the `$casts` array, enabling object-oriented access to the ID. ```PHP use GlhdBitsDatabaseHasSnowflakes; use GlhdBitsSnowflake; use IlluminateDatabaseEloquentModel; class Example extends Model { // Auto-generate Snowflake for new models use HasSnowflakes; // Any attribute can be cast to a `Snowflake` (or `Sonyflake`) protected $casts = [ 'id' => Snowflake::class, ]; } $example = Example::create(); $example->id instanceof Snowflake; // true echo $example->id; // 65898467809951744 ``` -------------------------------- ### Snowflake Object Structure in PHP Source: https://github.com/glhd/bits/blob/main/README.md This snippet illustrates the public interface of the `Snowflake` object returned by `Snowflake::make()`. It exposes read-only properties for the timestamp, datacenter ID, worker ID, and sequence number, along with methods to retrieve the full 64-bit integer ID and compare two Snowflake objects. ```php class Snowflake { public readonly int $timestamp; public readonly int $datacenter_id; public readonly int $worker_id; public readonly int $sequence; public function id(): int; public function is(Snowflake $other): bool; } ``` -------------------------------- ### Snowflake ID Format Structure Source: https://github.com/glhd/bits/blob/main/README.md This diagram illustrates the 64-bit structure of a Snowflake ID. It breaks down the ID into its constituent parts: an unused bit, a 41-bit timestamp, a 5-bit datacenter ID, a 5-bit worker ID, and a 12-bit sequence number, highlighting how each component contributes to the uniqueness and sortability of the ID. ```Diagram 0 0000001100100101110101100110111100101011 01011 01111 000000011101 ┳ ━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━ ━━━┳━ ━┳━━━ ━┳━━━━━━━━━━ ┗━ unused bit ┗━ timestamp (41) ┃ ┃ ┗━ sequence (12) datacenter (5) ━┛ ┗━ worker (5) ``` -------------------------------- ### Extracting Timestamp from Snowflake ID in PHP Source: https://github.com/glhd/bits/blob/main/README.md This snippet demonstrates how to extract the creation timestamp directly from a Snowflake ID. Assuming the model's ID is cast to a `Snowflake` object, the `toCarbon()` method can be used to convert the Snowflake ID into a Carbon instance, providing the equivalent of a `created_at` timestamp. ```PHP // Instead of: $created_at = $user->created_at; // You can do (assuming User::id is cast to a Snowflake object) $created_at = $user->id->toCarbon(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.