2016-05-24 14:10:39 +02:00
|
|
|
<?php namespace Moserware\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
|
|
|
|
{
|
|
|
|
private $_min;
|
|
|
|
private $_max;
|
|
|
|
|
|
|
|
public function __construct($min, $max)
|
|
|
|
{
|
|
|
|
if ($min > $max)
|
|
|
|
{
|
|
|
|
throw new Exception("min > max");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_min = $min;
|
|
|
|
$this->_max = $max;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMin()
|
|
|
|
{
|
|
|
|
return $this->_min;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMax()
|
|
|
|
{
|
|
|
|
return $this->_max;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function create($min, $max)
|
|
|
|
{
|
|
|
|
return new Range($min, $max);
|
|
|
|
}
|
|
|
|
|
|
|
|
// REVIEW: It's probably bad form to have access statics via a derived class, but the syntax looks better :-)
|
|
|
|
|
|
|
|
public static function inclusive($min, $max)
|
|
|
|
{
|
|
|
|
return static::create($min, $max);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function exactly($value)
|
|
|
|
{
|
|
|
|
return static::create($value, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function atLeast($minimumValue)
|
|
|
|
{
|
|
|
|
return static::create($minimumValue, PHP_INT_MAX );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isInRange($value)
|
|
|
|
{
|
|
|
|
return ($this->_min <= $value) && ($value <= $this->_max);
|
|
|
|
}
|
2016-05-24 14:10:39 +02:00
|
|
|
}
|