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 14:02:12 +00:00
|
|
|
public function __construct(private int $min, private 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 12:13:24 +00:00
|
|
|
public function getMin(): int
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
2023-08-01 14:02:12 +00: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
|
|
|
{
|
2023-08-01 14:02:12 +00:00
|
|
|
return $this->max;
|
2010-08-28 22:05:41 -04:00
|
|
|
}
|
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
|
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-02 09:36:44 +00:00
|
|
|
public static function inclusive(int $min, int $max): static
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
|
|
|
return static::create($min, $max);
|
|
|
|
}
|
|
|
|
|
2023-08-02 09:36:44 +00:00
|
|
|
public static function exactly(int $value): static
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
|
|
|
return static::create($value, $value);
|
|
|
|
}
|
|
|
|
|
2023-08-02 09:36:44 +00:00
|
|
|
public static function atLeast(int $minimumValue): static
|
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
|
|
|
{
|
2023-08-01 14:02:12 +00:00
|
|
|
return ($this->min <= $value) && ($value <= $this->max);
|
2010-08-28 22:05:41 -04:00
|
|
|
}
|
2022-07-05 15:55:47 +02:00
|
|
|
}
|