28 lines
741 B
PHP
28 lines
741 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\pharmacy;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
final class PharmacyLogisticsValue
|
|
{
|
|
public static function normalize(mixed $value, int $maxLength, string $label): string
|
|
{
|
|
$normalized = preg_replace(
|
|
'/^[\s\p{Z}\x{200B}\x{2060}\x{FEFF}]+|[\s\p{Z}\x{200B}\x{2060}\x{FEFF}]+$/u',
|
|
'',
|
|
(string) $value
|
|
);
|
|
if ($normalized === null) {
|
|
throw new InvalidArgumentException($label . '格式无效');
|
|
}
|
|
if (mb_strlen($normalized) > $maxLength) {
|
|
throw new InvalidArgumentException($label . '长度不能超过' . $maxLength . '个字符');
|
|
}
|
|
|
|
return $normalized;
|
|
}
|
|
}
|