mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-30 11:25:52 -05:00
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
50 lines
1.1 KiB
PHP
50 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pterodactyl\Data;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
final readonly class AllocationRange
|
|
{
|
|
public function __construct(
|
|
public int $start,
|
|
public int $end,
|
|
) {}
|
|
|
|
/**
|
|
* @param JsonInputValue $start
|
|
* @param JsonInputValue $end
|
|
*/
|
|
public static function fromConfig(mixed $start, mixed $end): ?self
|
|
{
|
|
if (! $start || ! $end) {
|
|
return null;
|
|
}
|
|
|
|
return new self(
|
|
self::integerLike($start, 'start'),
|
|
self::integerLike($end, 'end'),
|
|
);
|
|
}
|
|
|
|
/** @param JsonInputValue $value */
|
|
private static function integerLike(mixed $value, string $name): int
|
|
{
|
|
if (is_int($value)) {
|
|
return $value;
|
|
}
|
|
|
|
if (is_string($value) && preg_match('/^-?\d+$/', $value)) {
|
|
// SAFETY: the anchored decimal-integer pattern proves the string has an integer representation.
|
|
return (int) $value;
|
|
}
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
'Expected allocation range %s to be an integer-like value.',
|
|
$name,
|
|
));
|
|
}
|
|
}
|