belongsTo(Customers::class); } // * FUNCTIONS /** * Generate code_unique attribute * * @return string|null */ public function getCodeUniqueAttribute() { if (!$this->id or !\is_integer($this->id) or $this->id < 1) { return null; } return self::intToSerial($this->id); } /** * Generate a serial from an int. * * @param integer $int * @return string The serial * @throws SerialCasterException If an error happens du to config. */ public static function intToSerial(int $int): string { return SerialCaster::encode( $int, \abs(\crc32(config('app.key'))), config('opbrcharcut.codeLength'), config('opbrcharcut.codeCharList') ); } /** * Generate a serial from an int. * * @param string $serial * @return integer The ID represented * @throws SerialCasterException If an error happens due to config or wrong serial is given. */ public static function serialToInt(string $serial): int { try { return SerialCaster::decode( $serial, \abs(\crc32(config('app.key'))), config('opbrcharcut.codeCharList') ); } catch (SerialCasterException $e) { Log::error($e->getMessage() . " code utilisé : " . $serial); // return 0 invalid value for code return 0; } } }