Snowflake
in package
implements
Stringable
uses
DynamicPropertyMutatorTrait
Snowflake is a helper class for parsing Discord snowflake IDs into their component parts, and for generating snowflake IDs from a given timestamp.
https://docs.discord.com/developers/reference#snowflakes
Tags
Table of Contents
Interfaces
- Stringable
Constants
- DISCORD_EPOCH : mixed = 1420070400000
- Discord Epoch (2015-01-01T00:00:00Z), in milliseconds since the Unix Epoch.
Properties
- $datetime : Carbon
- $increment : int
- $process_id : int
- $timestamp : int|string
- $worker_id : int
- $id : string
- The snowflake ID as a numeric string.
Methods
- __construct() : mixed
- __get() : mixed
- Handles dynamic get calls onto the part.
- __set() : void
- Handles dynamic set calls onto the part.
- __toString() : string
- fromTimestamp() : self
- Creates a new Snowflake from a timestamp and optional internal fields.
- new() : self
- checkForGetMutator() : string|false
- Checks if there is a get mutator present.
- checkForSetMutator() : string|false
- Checks if there is a set mutator present.
- getDatetime() : Carbon
- getId() : string
- getIncrement() : int
- getProcessId() : int
- getProperty() : mixed
- Gets a property on the parent part.
- getTimestamp() : int|string
- Returned as a numeric string on 32-bit PHP, since the value overflows a native `int` there.
- getWorkerId() : int
- normalizeTimestampToMs() : int|string
- Normalizes a `DateTimeInterface` or numeric timestamp to milliseconds since the Unix Epoch.
- setId() : void
- Normalizes and sets the `id` attribute.
- setProperty() : void
- Sets an property on the parent part.
- studly() : string
- Converts a string to studlyCase.
Constants
DISCORD_EPOCH
Discord Epoch (2015-01-01T00:00:00Z), in milliseconds since the Unix Epoch.
public
mixed
DISCORD_EPOCH
= 1420070400000
Properties
$datetime read-only
public
Carbon
$datetime
The Carbon instance the snowflake was generated at.
$increment read-only
public
int
$increment
Increment for the ID generated on that worker/process, 0-4095.
$process_id read-only
public
int
$process_id
Internal process ID, 0-31.
$timestamp read-only
public
int|string
$timestamp
Milliseconds since the Unix Epoch that the snowflake was generated at. Returned as a numeric string on 32-bit PHP, where it does not fit in a native int.
$worker_id read-only
public
int
$worker_id
Internal worker ID, 0-31.
$id
The snowflake ID as a numeric string.
protected
string
$id
Methods
__construct()
public
__construct(Stringable|int|string $id) : mixed
Parameters
- $id : Stringable|int|string
-
The snowflake ID.
__get()
Handles dynamic get calls onto the part.
public
__get(string $key) : mixed
Parameters
- $key : string
-
The attributes key.
Tags
Return values
mixed —The value of the attribute.
__set()
Handles dynamic set calls onto the part.
public
__set(string $key, mixed $value) : void
Parameters
- $key : string
-
The attributes key.
- $value : mixed
-
The attributes value.
Tags
__toString()
public
__toString() : string
Return values
string —The snowflake ID.
fromTimestamp()
Creates a new Snowflake from a timestamp and optional internal fields.
public
static fromTimestamp(DateTimeInterface|int|string $timestamp[, int $workerId = 0 ][, int $processId = 0 ][, int $increment = 0 ]) : self
Parameters
- $timestamp : DateTimeInterface|int|string
-
A
DateTimeInterfaceor a Unix timestamp in milliseconds. - $workerId : int = 0
-
Internal worker ID, 0-31. Defaults to 0.
- $processId : int = 0
-
Internal process ID, 0-31. Defaults to 0.
- $increment : int = 0
-
Increment for the ID, 0-4095. Defaults to 0.
Tags
Return values
selfnew()
public
static new(Stringable|int|string $id) : self
Parameters
- $id : Stringable|int|string
-
The snowflake ID.
Return values
selfcheckForGetMutator()
Checks if there is a get mutator present.
protected
checkForGetMutator(string $key) : string|false
Parameters
- $key : string
-
The property name to check.
Return values
string|false —Either a string if it is a method or false.
checkForSetMutator()
Checks if there is a set mutator present.
protected
checkForSetMutator(string $key) : string|false
Parameters
- $key : string
-
The property name to check.
Return values
string|false —Either a string if it is a method or false.
getDatetime()
protected
getDatetime() : Carbon
Return values
Carbon —The datetime the snowflake was generated at.
getId()
protected
getId() : string
Return values
string —The snowflake ID as a numeric string.
getIncrement()
protected
getIncrement() : int
Return values
int —Increment for the ID generated on that worker/process, 0-4095.
getProcessId()
protected
getProcessId() : int
Return values
int —Internal process ID, 0-31.
getProperty()
Gets a property on the parent part.
protected
getProperty(string $key) : mixed
Parameters
- $key : string
-
The name of the property.
Tags
Return values
mixed —Either the property if it exists or void.
getTimestamp()
Returned as a numeric string on 32-bit PHP, since the value overflows a native `int` there.
protected
getTimestamp() : int|string
Return values
int|string —Milliseconds since the Unix Epoch that the snowflake was generated at.
getWorkerId()
protected
getWorkerId() : int
Return values
int —Internal worker ID, 0-31.
normalizeTimestampToMs()
Normalizes a `DateTimeInterface` or numeric timestamp to milliseconds since the Unix Epoch.
protected
static normalizeTimestampToMs(DateTimeInterface|int|string $timestamp) : int|string
Returned as a numeric string on 32-bit PHP, since millisecond timestamps overflow a native int there.
Parameters
- $timestamp : DateTimeInterface|int|string
Return values
int|stringsetId()
Normalizes and sets the `id` attribute.
protected
setId(Stringable|int|string $id) : void
Parameters
- $id : Stringable|int|string
setProperty()
Sets an property on the parent part.
protected
setProperty(string $key, mixed $value) : void
Parameters
- $key : string
-
The name of the property.
- $value : mixed
-
The value of the property.
studly()
Converts a string to studlyCase.
private
static studly(string $string) : string
This is a port of updated Laravel's implementation, a non-regex with static cache. This method is private static as we may move it outside this class in future.
Parameters
- $string : string
-
The string to convert.