php-snowflake icon indicating copy to clipboard operation
php-snowflake copied to clipboard

feat: Helpers for generating ID's for specified time

Open kduma opened this issue 1 year ago • 1 comments

This PR provides helper for generating Snowflake ID for a specified time, for use in tests or when retrospectively adding Snowflake ID's to existing database: d0cac91f1a6a03ee3a71fd0846edaf796a8833f5

  • 9442f82e623b77da630e531d44983356921fd511 Add Snowflake::idFor($timestamp) method
  • d0cac91f1a6a03ee3a71fd0846edaf796a8833f5 Add Sonyflake::idFor($timestamp) method

Second added helper, eases conversion from relative timestamps returned from Snowflake::parseId($id, $transform) method to unix microtime:

  • a830e0f75fb044aefc27ffe921342bfcaeac6bc9 Add Snowflake::toMicrotime($timestamp) method
  • 77c1fb5801a38cb4f0fed915a143779a695b5d64 Add Sonyflake::toMicrotime($timestamp) method

And finally I have extracted an interface for easier playing with dependency injection container:

  • ac2a96032f5eee7d643828f0712ed0dab0609e14 Extract IdGenerator interface

kduma avatar Dec 29 '23 14:12 kduma

Hi @kduma, I review this PR today and there's something I don't quite understand. Could you tell me why we need to specify idFor($currentTime) to generate a snowflake ID? In my understanding, if $currentTime is fixed, it would lead to duplicate IDs being generated. Is this a requirement encountered in actual projects or just for testing purposes? Looking forward to your answer.

godruoyi avatar Jan 09 '24 09:01 godruoyi

Close this PR because it hasn't been updated for over 3 months. If you have plans to continue updating it in the future, feel free to reopen it. Thank you for your contribution.

godruoyi avatar Apr 07 '24 05:04 godruoyi