generarIsknUnico($contexto); $this->insert(['iskn' => $iskn]); return $iskn; } /** * Genera un ISKN único validado contra la base de datos. */ private function generarIsknUnico(string $contexto): string { do { $iskn = $this->generarIskn($contexto); } while ($this->where('iskn', $iskn)->countAllResults() > 0); return $iskn; } /** * Formato legible de ISKN, ejemplo: iskn_libro_20250419_ab12c */ private function generarIskn(string $contexto): string { $fecha = date('Ymd'); $random = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0, 5); return "iskn_{$contexto}_{$fecha}_{$random}"; } /** * Hook para generar el ISKN automáticamente al insertar. */ protected function agregarIskn(array $data): array { if (!isset($data['data']['iskn']) || empty($data['data']['iskn'])) { $data['data']['iskn'] = $this->generarIsknUnico('registro'); } return $data; } // Bloqueo total de eliminaciones public function delete($id = null, bool $purge = false) { throw new RuntimeException('La eliminación de registros está deshabilitada.'); } public function deleteWhere($where) { throw new RuntimeException('La eliminación de registros está deshabilitada.'); } public function deleteBatch($where) { throw new RuntimeException('La eliminación de registros está deshabilitada.'); } }