Files
trueskill/src/Numerics/Range.php

56 lines
1.2 KiB
PHP
Raw Normal View History

2022-07-05 15:55:47 +02:00
<?php
namespace DNW\Skills\Numerics;
// The whole purpose of this class is to make the code for the SkillCalculator(s)
// look a little cleaner
use Exception;
class Range
{
public function __construct(private int $min, private int $max)
{
2022-07-05 15:55:47 +02:00
if ($min > $max) {
throw new Exception('min > max');
}
}
2023-08-01 12:13:24 +00:00
public function getMin(): int
{
return $this->min;
2022-07-05 15:33:34 +02:00
}
2023-08-01 12:13:24 +00:00
public function getMax(): int
{
return $this->max;
}
2022-07-05 15:33:34 +02:00
2023-08-02 13:19:35 +00:00
protected static function create(int $min, int $max): static
{
return new Range($min, $max);
}
// REVIEW: It's probably bad form to have access statics via a derived class, but the syntax looks better :-)
2023-08-02 09:36:44 +00:00
public static function inclusive(int $min, int $max): static
{
return static::create($min, $max);
}
2023-08-02 09:36:44 +00:00
public static function exactly(int $value): static
{
return static::create($value, $value);
}
2023-08-02 09:36:44 +00:00
public static function atLeast(int $minimumValue): static
{
2022-07-05 15:55:47 +02:00
return static::create($minimumValue, PHP_INT_MAX);
}
2023-08-01 12:13:24 +00:00
public function isInRange(int $value): bool
{
return ($this->min <= $value) && ($value <= $this->max);
}
2022-07-05 15:55:47 +02:00
}