2022-07-05 15:55:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DNW\Skills\Numerics;
|
2010-08-28 22:05:41 -04:00
|
|
|
|
|
|
|
// The whole purpose of this class is to make the code for the SkillCalculator(s)
|
|
|
|
// look a little cleaner
|
|
|
|
|
2016-05-24 14:10:39 +02:00
|
|
|
use Exception;
|
|
|
|
|
2010-08-28 22:05:41 -04:00
|
|
|
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)
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
2022-07-05 15:55:47 +02:00
|
|
|
if ($min > $max) {
|
|
|
|
throw new Exception('min > max');
|
2010-08-28 22:05:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_min = $min;
|
|
|
|
$this->_max = $max;
|
|
|
|
}
|
|
|
|
|
2023-08-01 12:13:24 +00:00
|
|
|
public function getMin(): int
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
|
|
|
return $this->_min;
|
2022-07-05 15:33:34 +02:00
|
|
|
}
|
|
|
|
|
2023-08-01 12:13:24 +00:00
|
|
|
public function getMax(): int
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
|
|
|
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
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
|
|
|
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
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
|
|
|
return static::create($min, $max);
|
|
|
|
}
|
|
|
|
|
2023-08-01 12:13:24 +00:00
|
|
|
public static function exactly(int $value): self
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
|
|
|
return static::create($value, $value);
|
|
|
|
}
|
|
|
|
|
2023-08-01 12:13:24 +00:00
|
|
|
public static function atLeast(int $minimumValue): self
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
2022-07-05 15:55:47 +02:00
|
|
|
return static::create($minimumValue, PHP_INT_MAX);
|
2010-08-28 22:05:41 -04:00
|
|
|
}
|
|
|
|
|
2023-08-01 12:13:24 +00:00
|
|
|
public function isInRange(int $value): bool
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
|
|
|
return ($this->_min <= $value) && ($value <= $this->_max);
|
|
|
|
}
|
2022-07-05 15:55:47 +02:00
|
|
|
}
|