Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guard methods to Date #58

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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` like `mustBeEqualTo` with option to throw custom exception.
- Added guard methods for comparison methods to `Moment` and `Date` like `mustBeEqualTo` with option to throw custom exception.

## 0.10.0

Expand Down
178 changes: 175 additions & 3 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public static function fromString(string $date): self
}
}

// Stringable
// -- Stringable

public function __toString(): string
{
return $this->format(self::DATE_FORMAT);
}

// Accessors
// -- Accessors

public function isEqualTo(self $date): bool
{
Expand Down Expand Up @@ -156,7 +156,179 @@ public function weekday(): Weekday
return Weekday::fromDateTime($this->toDateTimeImmutable());
}

// Mutations
// -- Guards

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsNotEqual
*/
public function mustBeEqualTo(
self $date,
?callable $otherwiseThrow = null,
): void {
if ($this->isNotEqualTo($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsNotEqual();
}
}

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

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsNotAfter
*/
public function mustBeAfter(
self $date,
?callable $otherwiseThrow = null,
): void {
if ($this->isNotAfter($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsNotAfter();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsAfter
*/
public function mustNotBeAfter(
self $date,
?callable $otherwiseThrow = null,
): void {
if ($this->isAfter($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsAfter();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsNotAfterOrEqualTo
*/
public function mustBeAfterOrEqualTo(
self $date,
?callable $otherwiseThrow = null,
): void {
if ($this->isNotAfterOrEqualTo($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsNotAfterOrEqualTo();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsAfterOrEqualTo
*/
public function mustNotBeAfterOrEqualTo(
self $date,
?callable $otherwiseThrow = null,
): void {
if ($this->isAfterOrEqualTo($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsAfterOrEqualTo();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsNotBefore
*/
public function mustBeBefore(
self $date,
?callable $otherwiseThrow = null,
): void {
if ($this->isNotBefore($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsNotBefore();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsBefore
*/
public function mustNotBeBefore(
self $date,
?callable $otherwiseThrow = null,
): void {
if ($this->isBefore($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsBefore();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsNotBeforeOrEqualTo
*/
public function mustBeBeforeOrEqualTo(
self $date,
?callable $otherwiseThrow = null,
): void {
if ($this->isNotBeforeOrEqualTo($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsNotBeforeOrEqualTo();
}
}

/**
* @param ?callable(): \Throwable $otherwiseThrow
*
* @throws \Throwable
* @throws Exception\DateIsBeforeOrEqualTo
*/
public function mustNotBeBeforeOrEqualTo(
self $date,
?callable $otherwiseThrow = null,
): void {
if ($this->isBeforeOrEqualTo($date)) {
throw $otherwiseThrow !== null
? $otherwiseThrow()
: new Exception\DateIsBeforeOrEqualTo();
}
}

// -- Mutations

public function format(string $format): string
{
Expand Down
18 changes: 18 additions & 0 deletions src/Exception/DateIsAfter.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 DateIsAfter extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The date is after but must not be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/DateIsAfterOrEqualTo.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 DateIsAfterOrEqualTo extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The date is after or equal to but must not be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/DateIsBefore.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 DateIsBefore extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The date is before but must not be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/DateIsBeforeOrEqualTo.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 DateIsBeforeOrEqualTo extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The date is before or equal to but must not be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/DateIsEqual.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 DateIsEqual extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The date is equal but must not be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/DateIsNotAfter.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 DateIsNotAfter extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The date is not after but must be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/DateIsNotAfterOrEqualTo.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 DateIsNotAfterOrEqualTo extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The date is not after or equal to but must be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/DateIsNotBefore.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 DateIsNotBefore extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The date is not before but must be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/DateIsNotBeforeOrEqualTo.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 DateIsNotBeforeOrEqualTo extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The date is not before or equal to but must be.');
}
}
18 changes: 18 additions & 0 deletions src/Exception/DateIsNotEqual.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 DateIsNotEqual extends \InvalidArgumentException
{
public function __construct()
{
parent::__construct('The date is not equal but must be.');
}
}
Loading
Loading