Skip to content

Commit

Permalink
Add time guards (#59)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Kolb <[email protected]>
  • Loading branch information
christian-kolb and Christian Kolb authored Jun 23, 2024
1 parent d3459ef commit 4c22053
Show file tree
Hide file tree
Showing 64 changed files with 1,241 additions and 149 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- **[Breaking change](./UPGRADE.md#removed-deprecated-isdate-methods-from-moment)**: Removed deprecated `isDate*` methods from `Moment`.
- Added `Clock` implementation with `SystemClock` and `FrozenClock`.
- Added guard methods for comparison methods to `Moment` and `Date` like `mustBeEqualTo` with option to throw custom exception.
- Added guard methods for comparison methods to `Moment`, `Date` and `Time` like `mustBeEqualTo` with option to throw custom exception.

## 0.10.0

Expand Down
8 changes: 4 additions & 4 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function weekday(): Weekday
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsNotEqual
* @throws Exception\DateIsNotEqualTo
*/
public function mustBeEqualTo(
self $date,
Expand All @@ -171,15 +171,15 @@ public function mustBeEqualTo(
if ($this->isNotEqualTo($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsNotEqual();
: new Exception\DateIsNotEqualTo();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsEqual
* @throws Exception\DateIsEqualTo
*/
public function mustNotBeEqualTo(
self $date,
Expand All @@ -188,7 +188,7 @@ public function mustNotBeEqualTo(
if ($this->isEqualTo($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsEqual();
: new Exception\DateIsEqualTo();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @codeCoverageIgnore
*/
final class DateIsEqual extends \InvalidArgumentException
final class DateIsEqualTo extends \InvalidArgumentException
{
public function __construct()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @codeCoverageIgnore
*/
final class DateIsNotEqual extends \InvalidArgumentException
final class DateIsNotEqualTo extends \InvalidArgumentException
{
public function __construct()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @codeCoverageIgnore
*/
final class MomentIsEqual extends \InvalidArgumentException
final class MomentIsEqualTo extends \InvalidArgumentException
{
public function __construct()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @codeCoverageIgnore
*/
final class MomentIsNotEqual extends \InvalidArgumentException
final class MomentIsNotEqualTo extends \InvalidArgumentException
{
public function __construct()
{
Expand Down
18 changes: 18 additions & 0 deletions src/Exception/TimeIsAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Exception;

/**
* @psalm-immutable
*
* @codeCoverageIgnore
*/
final class TimeIsAfter extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The time is after but must not be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/TimeIsAfterOrEqualTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Exception;

/**
* @psalm-immutable
*
* @codeCoverageIgnore
*/
final class TimeIsAfterOrEqualTo extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The time is after or equal to but must not be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/TimeIsBefore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Exception;

/**
* @psalm-immutable
*
* @codeCoverageIgnore
*/
final class TimeIsBefore extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The time is before but must not be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/TimeIsBeforeOrEqualTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Exception;

/**
* @psalm-immutable
*
* @codeCoverageIgnore
*/
final class TimeIsBeforeOrEqualTo extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The time is before or equal to but must not be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/TimeIsEqualTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Exception;

/**
* @psalm-immutable
*
* @codeCoverageIgnore
*/
final class TimeIsEqualTo extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The time is equal but must not be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/TimeIsNotAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Exception;

/**
* @psalm-immutable
*
* @codeCoverageIgnore
*/
final class TimeIsNotAfter extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The time is not after but must be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/TimeIsNotAfterOrEqualTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Exception;

/**
* @psalm-immutable
*
* @codeCoverageIgnore
*/
final class TimeIsNotAfterOrEqualTo extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The time is not after or equal to but must be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/TimeIsNotBefore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Exception;

/**
* @psalm-immutable
*
* @codeCoverageIgnore
*/
final class TimeIsNotBefore extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The time is not before but must be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/TimeIsNotBeforeOrEqualTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Exception;

/**
* @psalm-immutable
*
* @codeCoverageIgnore
*/
final class TimeIsNotBeforeOrEqualTo extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The time is not before or equal to but must be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/TimeIsNotEqualTo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Exception;

/**
* @psalm-immutable
*
* @codeCoverageIgnore
*/
final class TimeIsNotEqualTo extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The time is not equal but must be.');
}
}
16 changes: 8 additions & 8 deletions src/Moment.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public function midnightInTimeZone(\DateTimeZone $timeZone): self
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\MomentIsNotEqual
* @throws Exception\MomentIsNotEqualTo
*/
public function mustBeEqualTo(
self $moment,
Expand All @@ -417,15 +417,15 @@ public function mustBeEqualTo(
if ($this->isNotEqualTo($moment)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\MomentIsNotEqual();
: new Exception\MomentIsNotEqualTo();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\MomentIsNotEqual
* @throws Exception\MomentIsNotEqualTo
*/
public function mustBeEqualToInTimeZone(
Time | Weekday | Date | Month | Year $moment,
Expand All @@ -435,15 +435,15 @@ public function mustBeEqualToInTimeZone(
if ($this->isNotEqualToInTimeZone($moment, $timeZone)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\MomentIsNotEqual();
: new Exception\MomentIsNotEqualTo();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\MomentIsEqual
* @throws Exception\MomentIsEqualTo
*/
public function mustNotBeEqualTo(
self $moment,
Expand All @@ -452,15 +452,15 @@ public function mustNotBeEqualTo(
if ($this->isEqualTo($moment)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\MomentIsEqual();
: new Exception\MomentIsEqualTo();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\MomentIsEqual
* @throws Exception\MomentIsEqualTo
*/
public function mustNotBeEqualToInTimeZone(
Time | Weekday | Date | Month | Year $moment,
Expand All @@ -470,7 +470,7 @@ public function mustNotBeEqualToInTimeZone(
if ($this->isEqualToInTimeZone($moment, $timeZone)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\MomentIsEqual();
: new Exception\MomentIsEqualTo();
}
}

Expand Down
Loading

0 comments on commit 4c22053

Please sign in to comment.