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