mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-01 11:55:51 -05:00
44 lines
932 B
PHP
44 lines
932 B
PHP
<?php
|
|||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Pterodactyl\Data;
|
||
|
|
|
||
|
|
use InvalidArgumentException;
|
||
|
|
|
||
|
|
final readonly class AllocationRange
|
||
|
|
{
|
||
|
|
public function __construct(
|
||
|
|
public int $start,
|
||
|
|
public int $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'),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function integerLike(mixed $value, string $name): int
|
||
|
|
{
|
||
|
|
if (is_int($value)) {
|
||
|
|
return $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (is_string($value) && preg_match('/^-?\d+$/', $value)) {
|
||
|
|
return (int) $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new InvalidArgumentException(sprintf(
|
||
|
|
'Expected allocation range %s to be an integer-like value.',
|
||
|
|
$name,
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|