新增全局流水号

This commit is contained in:
2026-08-25 05:33:01 +08:00
parent 90fd59b647
commit 07c4e969c3
33 changed files with 1176 additions and 73 deletions
+36
View File
@@ -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);
}
}