新增全局流水号
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Finance;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class BusinessNumber {
|
||||
|
||||
public const MAX_PREFIX_LENGTH = 12;
|
||||
|
||||
public static function normalize_prefix(mixed $value, string $fallback): string {
|
||||
$prefix = strtoupper(trim(sanitize_text_field(is_scalar($value) ? (string) $value : '')));
|
||||
$prefix = (string) preg_replace('/[^A-Z0-9_-]/', '', $prefix);
|
||||
$prefix = substr($prefix, 0, self::MAX_PREFIX_LENGTH);
|
||||
if ($prefix !== '') {
|
||||
return $prefix;
|
||||
}
|
||||
|
||||
$fallback = strtoupper((string) preg_replace('/[^A-Z0-9_-]/', '', $fallback));
|
||||
return substr($fallback !== '' ? $fallback : 'AK', 0, self::MAX_PREFIX_LENGTH);
|
||||
}
|
||||
|
||||
public static function candidate(string $prefix, ?\DateTimeInterface $now = null, ?int $random = null): string {
|
||||
$prefix = self::normalize_prefix($prefix, 'AK');
|
||||
if ($now === null) {
|
||||
$now = new \DateTimeImmutable('now', wp_timezone());
|
||||
}
|
||||
$random = $random ?? random_int(0, 999999);
|
||||
$random = max(0, min(999999, $random));
|
||||
|
||||
return $prefix . $now->format('YmdHis') . str_pad((string) $random, 6, '0', STR_PAD_LEFT);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user