60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
class SupplierStatementReminderTargets
|
|
{
|
|
private const SETTING_KEY = 'supplier_reconciliation.statement_reminder_suppliers';
|
|
|
|
/** @return list<string> */
|
|
public function names(): array
|
|
{
|
|
$names = setting(self::SETTING_KEY, []);
|
|
if (is_string($names)) {
|
|
$names = json_decode($names, true) ?: [];
|
|
}
|
|
if (! is_array($names)) {
|
|
return [];
|
|
}
|
|
|
|
return $this->normalize($names);
|
|
}
|
|
|
|
/** @return list<string> */
|
|
public function replace(array $names): array
|
|
{
|
|
$names = $this->normalize($names);
|
|
if (count($names) > 5000) {
|
|
throw new InvalidArgumentException('催办名单不能超过 5000 家供应商。');
|
|
}
|
|
|
|
setting()->set(self::SETTING_KEY, json_encode($names, JSON_UNESCAPED_UNICODE));
|
|
setting()->save();
|
|
|
|
return $names;
|
|
}
|
|
|
|
/** @return list<string> */
|
|
private function normalize(array $names): array
|
|
{
|
|
$normalized = [];
|
|
foreach ($names as $name) {
|
|
$name = trim((string) $name);
|
|
if ($name === '') {
|
|
continue;
|
|
}
|
|
if (mb_strlen($name) > 191) {
|
|
throw new InvalidArgumentException('供应商名称超过 191 个字符。');
|
|
}
|
|
$normalized[$name] = true;
|
|
}
|
|
|
|
$normalized = array_keys($normalized);
|
|
natcasesort($normalized);
|
|
|
|
return array_values($normalized);
|
|
}
|
|
}
|