Files
trueskill/src/Numerics/Range.php

63 lines
1.3 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
{
2023-08-01 12:13:24 +00:00
private int $_min;
2022-07-05 15:55:47 +02:00
2023-08-01 12:13:24 +00:00
private int $_max;
2022-07-05 15:33:34 +02:00
2023-08-01 12:13:24 +00:00
public function __construct(int $min, int $max)
{
2022-07-05 15:55:47 +02:00
if ($min > $max) {
throw new Exception('min > max');
}
$this->_min = $min;
$this->_max = $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-01 12:13:24 +00:00
protected static function create(int $min, int $max): self
{
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-01 12:13:24 +00:00
public static function inclusive(int $min, int $max): self
{
return static::create($min, $max);
}
2023-08-01 12:13:24 +00:00
public static function exactly(int $value): self
{
return static::create($value, $value);
}
2023-08-01 12:13:24 +00:00
public static function atLeast(int $minimumValue): self
{
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
}