Files
trueskill/src/Guard.php

30 lines
760 B
PHP
Raw Normal View History

2022-07-05 15:55:47 +02:00
<?php
declare(strict_types=1);
2022-07-05 15:55:47 +02:00
namespace DNW\Skills;
use Exception;
/**
* Verifies argument contracts.
*
* @see http://www.moserware.com/2008/01/borrowing-ideas-from-3-interesting.html
*/
class Guard
{
2023-08-01 11:26:38 +00:00
public static function argumentIsValidIndex(int $index, int $count, string $parameterName): void
{
if (($index < 0) || ($index >= $count)) {
2023-08-01 13:35:44 +00:00
throw new Exception($parameterName . ' is an invalid index');
}
}
2023-08-01 11:26:38 +00:00
public static function argumentInRangeInclusive(float $value, float $min, float $max, string $parameterName): void
{
if (($value < $min) || ($value > $max)) {
2023-08-01 13:35:44 +00:00
throw new Exception($parameterName . ' is not in the valid range [' . $min . ', ' . $max . ']');
}
}
2022-07-05 15:55:47 +02:00
}