Skip to content
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
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
timeoutForSmallTests="1"
timeoutForMediumTests="10"
timeoutForLargeTests="60"
displayDetailsOnPhpunitDeprecations="true"
>
<testsuites>
<testsuite name="Unit">
Expand Down
42 changes: 35 additions & 7 deletions src/DateTime/LocalDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ final private function __construct(
) {
// NOTE: 不変条件(invariant)
assert(static::isValid($from, $to)->isOk());
assert(static::isValidRange($from, $to)->isOk());
}

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -97,32 +98,59 @@ final public static function tryFrom(
mixed $to,
): Result {
return static::isValid($from, $to)
->andThen(static fn () => self::isValidRange($from, $to))
->andThen(static fn () => Result\ok(static::from($from, $to)));
}

// -------------------------------------------------------------------------
// MARK: validation methods
// -------------------------------------------------------------------------
/**
* 有効な値かどうか
* 日付範囲が有効であることを検証
*
* @param TStart $from 開始日付
* @param TEnd $to 終了日付
*
* @return Result<bool,ValueObjectError>
*/
protected static function isValid(mixed $from, mixed $to): Result
final protected static function isValidRange(mixed $from, mixed $to): Result
{
if ($from->isAfter($to)) {
return Result\err(ValueObjectError::of(
code: 'value_object.date_range.invalid_range',
message: '開始日付は終了日付以前である必要があります'
));
[$isValidRange, $errorCode, $message] = match (static::rangeType()) {
RangeType::CLOSED => [
$from->isBeforeOrEqualTo($to),
'value_object.date_range.invalid_range',
'開始日付は終了日付以前である必要があります',
],
RangeType::OPEN,
RangeType::HALF_OPEN_RIGHT,
RangeType::HALF_OPEN_LEFT => [
$from->isBefore($to),
'value_object.date_range.invalid_range',
'開始日付は終了日付より前である必要があります',
],
};

if (!$isValidRange) {
return Result\err(ValueObjectError::of(code: $errorCode, message: $message));
}

return Result\ok(true);
}

/**
* 有効な値かどうか
* NOTE: 実装クラスでのオーバーライド用メソッド
*
* @param TStart $from 開始日付
* @param TEnd $to 終了日付
*
* @return Result<bool,ValueObjectError>
*/
protected static function isValid(mixed $from, mixed $to): Result
{
return Result\ok(true);
}

// -------------------------------------------------------------------------
// MARK: public methods
// -------------------------------------------------------------------------
Expand Down
Loading
Loading