2022-10-10 11:21:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
2024-10-18 14:28:45 +02:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
2022-10-10 11:21:42 +02:00
|
|
|
class Helpers
|
|
|
|
{
|
2022-11-22 15:15:52 +01:00
|
|
|
/**
|
|
|
|
* Clean a version number string
|
|
|
|
*/
|
2022-12-13 12:07:29 +01:00
|
|
|
public static function cleanVersionNumber(?string $release) : string|false
|
2022-10-12 11:10:51 +02:00
|
|
|
{
|
2022-12-09 10:52:17 +01:00
|
|
|
// We use the regex for semver detection (see https://semver.org/)
|
|
|
|
return preg_match('/(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/', $release, $version) ? $version[0] : false;
|
2022-10-12 11:10:51 +02:00
|
|
|
}
|
2022-12-13 09:05:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a string to comply with Base32 format
|
|
|
|
*
|
|
|
|
* @return string The filename
|
|
|
|
*/
|
2022-12-13 12:07:29 +01:00
|
|
|
public static function PadToBase32Format(?string $str) : string
|
2022-12-13 09:05:56 +01:00
|
|
|
{
|
|
|
|
return blank($str) ? '' : strtoupper(str_pad($str, (int) ceil(strlen($str) / 8) * 8, '='));
|
|
|
|
}
|
2023-02-22 20:21:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Identify comma separated list of values and explode it to an array of values
|
|
|
|
*
|
|
|
|
* @param mixed $ids
|
|
|
|
*/
|
|
|
|
public static function commaSeparatedToArray($ids) : mixed
|
|
|
|
{
|
|
|
|
if (is_string($ids)) {
|
|
|
|
$regex = "/^\d+(,{1}\d+)*$/";
|
|
|
|
if (preg_match($regex, $ids)) {
|
|
|
|
$ids = explode(',', $ids);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ids;
|
|
|
|
}
|
2024-10-18 14:28:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a unique filename with the given extension
|
|
|
|
*/
|
|
|
|
public static function getRandomFilename(string $extension, int $length = 40) : string
|
|
|
|
{
|
|
|
|
return Str::random($length) . '.' . $extension;
|
|
|
|
}
|
2022-10-10 11:21:42 +02:00
|
|
|
}
|