Publier Globinours 1.0.0-rc.3
This commit is contained in:
parent
ea8c24d622
commit
9a2b4068da
325 changed files with 38230 additions and 20 deletions
50
app/Services/Ids.php
Normal file
50
app/Services/Ids.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
final class Ids
|
||||
{
|
||||
public static function slugifyUpper(string $s): string
|
||||
{
|
||||
$s = trim($s);
|
||||
if ($s === '') {
|
||||
return 'SANS-NOM';
|
||||
}
|
||||
|
||||
// translit: accents -> ascii (si dispo)
|
||||
$t = @iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $s);
|
||||
if ($t !== false) {
|
||||
$s = $t;
|
||||
}
|
||||
|
||||
$s = strtoupper($s);
|
||||
$s = preg_replace('/[^A-Z0-9]+/', '-', $s) ?? $s;
|
||||
$s = trim($s, '-');
|
||||
|
||||
return $s !== '' ? $s : 'SANS-NOM';
|
||||
}
|
||||
|
||||
public static function nextAnimalCode(string $name, ?int $year = null): string
|
||||
{
|
||||
$year = $year ?? (int) date('Y');
|
||||
$slug = self::slugifyUpper($name);
|
||||
|
||||
$db = DB::pdo();
|
||||
$stmt = $db->prepare(
|
||||
'SELECT internal_code FROM animals WHERE internal_code LIKE :p ORDER BY internal_code DESC LIMIT 1',
|
||||
);
|
||||
$stmt->execute([':p' => $year . '-%']);
|
||||
$last = (string) ($stmt->fetchColumn() ?: '');
|
||||
|
||||
$n = 1;
|
||||
if ($last !== '') {
|
||||
// format attendu: YYYY-NNN-...
|
||||
$parts = explode('-', $last);
|
||||
if (count($parts) >= 2 && ctype_digit($parts[1])) {
|
||||
$n = (int) $parts[1] + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return sprintf('%d-%03d-%s', $year, $n, $slug);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue