Skip to content

Commit

Permalink
feat(Math): method interval support DatetimeInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed May 23, 2024
1 parent 445b96e commit 0d0ed97
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/Date/Interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace h4kuna\DataType\Date;

use DateInterval;
use DateTimeInterface;
use h4kuna\DataType\Number\Math;
use Nette\Utils\DateTime;

final class Interval
Expand All @@ -14,4 +16,22 @@ public static function toSeconds(DateInterval $dateInterval): int
+ $dateInterval->i * DateTime::MINUTE
+ $dateInterval->s;
}


/**
* @template T of DateTimeInterface
* @param T $date
* @param T|null $from
* @param T|null $to
*
* @return T
*/
public static function interval(
DateTimeInterface $date,
?DateTimeInterface $from,
?DateTimeInterface $to = null
): DateTimeInterface
{
return Math::interval($date, $to, $from);
}
}
27 changes: 21 additions & 6 deletions src/Number/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace h4kuna\DataType\Number;

use DateTimeInterface;
use h4kuna\DataType;
use h4kuna\DataType\Exceptions\InvalidArgumentsException;

Expand All @@ -10,18 +11,32 @@ final class Math

/**
* Allow number in interval and correct it.
* @return ($number is int ? int : float)
* @template T of float|int|DateTimeInterface
* @param T $number
* @param T|null $max
* @param T|null $min
*
* @return T
*/
public static function interval(float|int $number, float|int $max, float|int $min = 0): float|int
public static function interval(
float|int|DateTimeInterface $number,
float|int|DateTimeInterface|null $max,
float|int|DateTimeInterface|null $min = 0
): float|int|DateTimeInterface
{
if ($max < $min) {
if ($max !== null && $min !== null && $max < $min) {
throw new InvalidArgumentsException('Maximum is less than minimum.');
}
if (is_int($number)) {
return max((int) $min, min((int) $max, $number));

if ($min === null && $max === null) {
return $number;
} elseif ($min === null) {
return min($max, $number);
} elseif ($max === null) {
return max($min, $number);
}

return max((float) $min, min((float) $max, $number));
return max($min, min($max, $number));
}


Expand Down
14 changes: 14 additions & 0 deletions tests/src/Unit/Number/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace h4kuna\DataType\Tests\Unit\Number;

use DateTime;
use h4kuna;
use h4kuna\DataType\Number\Math;
use Tester;
Expand All @@ -24,8 +25,21 @@ public function testInterval(): void
Assert::same(2, Math::interval(2, 4, 1));
Assert::same(2, Math::interval(2, 4, 2));
Assert::same(3, Math::interval(2, 4, 3));
Assert::same(2, Math::interval(2, 4, null));
Assert::same(4, Math::interval(5, 4, null));
Assert::same(2, Math::interval(2, null, 1));
Assert::same(6, Math::interval(5, null, 6));
Assert::same(5, Math::interval(5, null, null));

Assert::same(-5, Math::interval(-5, -4, -6));

Assert::same(3.0, Math::interval(2.0, 4.0, 3.0));

$min = new DateTime('1986-12-30 00:00:00');
Assert::same($min, Math::interval(new DateTime('1986-12-29 23:59:59'), new DateTime('1986-12-31 00:00:00'), $min));

$max = new DateTime('1986-12-31 00:00:00');
Assert::same($max, Math::interval(new DateTime('1986-12-31 00:00:01'), $max, new DateTime('1986-12-30 00:00:00')));
}

/**
Expand Down

0 comments on commit 0d0ed97

Please sign in to comment.