Skip to content

Commit ffc9a83

Browse files
committed
feat: add some new id string generator methods
Signed-off-by: inhere <in.798@qq.com>
1 parent d0b3098 commit ffc9a83

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

src/Obj/ObjectBox.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Obj;
4+
5+
/**
6+
* Class ObjectBox
7+
*
8+
* An simple object containers
9+
*
10+
* @package Toolkit\Stdlib\Obj
11+
*/
12+
class ObjectBox
13+
{
14+
/**
15+
* @var array
16+
*/
17+
private $config = [];
18+
19+
/**
20+
* @var array
21+
*/
22+
private $objects = [];
23+
}

src/Str/StringHelper.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,29 @@
99

1010
namespace Toolkit\Stdlib\Str;
1111

12+
use DateTime;
1213
use Exception;
1314
use Toolkit\Stdlib\Str\Traits\StringCaseHelperTrait;
1415
use Toolkit\Stdlib\Str\Traits\StringCheckHelperTrait;
1516
use Toolkit\Stdlib\Str\Traits\StringLengthHelperTrait;
1617
use Toolkit\Stdlib\Str\Traits\StringConvertTrait;
1718
use Toolkit\Stdlib\Str\Traits\StringTruncateHelperTrait;
1819
use Toolkit\Stdlib\Util\UUID;
20+
use function abs;
1921
use function array_merge;
2022
use function base64_encode;
2123
use function count;
24+
use function crc32;
25+
use function gethostname;
2226
use function hash;
2327
use function hex2bin;
2428
use function is_int;
2529
use function is_string;
2630
use function mb_strwidth;
31+
use function microtime;
2732
use function preg_split;
2833
use function random_bytes;
34+
use function random_int;
2935
use function str_pad;
3036
use function str_repeat;
3137
use function str_replace;
@@ -173,6 +179,16 @@ public static function genUid(int $length = 7): string
173179
return substr(hash('md5', uniqid('', true)), 0, $length);
174180
}
175181

182+
/**
183+
* @param string $prefix
184+
*
185+
* @return string
186+
*/
187+
public static function uniqId(string $prefix = ''): string
188+
{
189+
return uniqid($prefix, true);
190+
}
191+
176192
/**
177193
* gen UUID
178194
*
@@ -181,12 +197,63 @@ public static function genUid(int $length = 7): string
181197
* @param null $ns
182198
*
183199
* @return UUID
200+
* @throws Exception
184201
*/
185-
public static function genUUID(int $version = 1, $node = null, $ns = null)
202+
public static function genUUID(int $version = 1, $node = null, $ns = null): UUID
186203
{
187204
return UUID::generate($version, $node, $ns);
188205
}
189206

207+
/**
208+
* Generate order number
209+
*
210+
* @param string|int $prefix
211+
*
212+
* @return string If no prefix, default length is 20
213+
* @throws Exception
214+
*/
215+
public static function genNOV1($prefix = '', array $randomRange = []): string
216+
{
217+
$host = gethostname();
218+
$time = microtime(true) * 10000;
219+
220+
$id = (string)abs(crc32($host) % 100);
221+
$id = str_pad($id, 2, '0', STR_PAD_LEFT);
222+
223+
$randomRange = $randomRange ?: [1000, 9999];
224+
[$min, $max] = $randomRange;
225+
/** @noinspection PhpUnhandledExceptionInspection */
226+
$random = random_int($min, $max);
227+
228+
return $prefix . $time . $id . $random;
229+
}
230+
231+
/**
232+
* Generate order number v2
233+
*
234+
* @param string|int $prefix
235+
*
236+
* @return string If no prefix, default length is 26
237+
* @throws Exception
238+
*/
239+
public static function genNOV2($prefix = '', array $randomRange = []): string
240+
{
241+
$host = gethostname();
242+
// u - 可以打印微妙,但是使用 date 函数时无效
243+
// $date = date('YmdHisu');
244+
$date = (new DateTime())->format('YmdHisu');
245+
246+
$id = (string)abs(crc32($host) % 100);
247+
$id = str_pad($id, 2, '0', STR_PAD_LEFT);
248+
249+
$randomRange = $randomRange ?: [100, 999];
250+
[$min, $max] = $randomRange;
251+
/** @noinspection PhpUnhandledExceptionInspection */
252+
$random = random_int($min, $max);
253+
254+
return $prefix . $date . $id . $random;
255+
}
256+
190257
////////////////////////////////////////////////////////////////////////
191258
/// Format
192259
////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)