Skip to content

Commit 17d92fc

Browse files
authored
Merge pull request #51 from wiz-develop:endou-mame/issue50
閉区間の場合 LocalDateRange isValidRange メソッドの判定が間違っている
2 parents 55e9fa8 + 350b9ba commit 17d92fc

9 files changed

+288
-179
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
timeoutForSmallTests="1"
1212
timeoutForMediumTests="10"
1313
timeoutForLargeTests="60"
14+
displayDetailsOnPhpunitDeprecations="true"
1415
>
1516
<testsuites>
1617
<testsuite name="Unit">

src/DateTime/LocalDateRange.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ final private function __construct(
4343
) {
4444
// NOTE: 不変条件(invariant)
4545
assert(static::isValid($from, $to)->isOk());
46+
assert(static::isValidRange($from, $to)->isOk());
4647
}
4748

4849
// -------------------------------------------------------------------------
@@ -97,32 +98,59 @@ final public static function tryFrom(
9798
mixed $to,
9899
): Result {
99100
return static::isValid($from, $to)
101+
->andThen(static fn () => self::isValidRange($from, $to))
100102
->andThen(static fn () => Result\ok(static::from($from, $to)));
101103
}
102104

103105
// -------------------------------------------------------------------------
104106
// MARK: validation methods
105107
// -------------------------------------------------------------------------
106108
/**
107-
* 有効な値かどうか
109+
* 日付範囲が有効であることを検証
108110
*
109111
* @param TStart $from 開始日付
110112
* @param TEnd $to 終了日付
111113
*
112114
* @return Result<bool,ValueObjectError>
113115
*/
114-
protected static function isValid(mixed $from, mixed $to): Result
116+
final protected static function isValidRange(mixed $from, mixed $to): Result
115117
{
116-
if ($from->isAfter($to)) {
117-
return Result\err(ValueObjectError::of(
118-
code: 'value_object.date_range.invalid_range',
119-
message: '開始日付は終了日付以前である必要があります'
120-
));
118+
[$isValidRange, $errorCode, $message] = match (static::rangeType()) {
119+
RangeType::CLOSED => [
120+
$from->isBeforeOrEqualTo($to),
121+
'value_object.date_range.invalid_range',
122+
'開始日付は終了日付以前である必要があります',
123+
],
124+
RangeType::OPEN,
125+
RangeType::HALF_OPEN_RIGHT,
126+
RangeType::HALF_OPEN_LEFT => [
127+
$from->isBefore($to),
128+
'value_object.date_range.invalid_range',
129+
'開始日付は終了日付より前である必要があります',
130+
],
131+
};
132+
133+
if (!$isValidRange) {
134+
return Result\err(ValueObjectError::of(code: $errorCode, message: $message));
121135
}
122136

123137
return Result\ok(true);
124138
}
125139

140+
/**
141+
* 有効な値かどうか
142+
* NOTE: 実装クラスでのオーバーライド用メソッド
143+
*
144+
* @param TStart $from 開始日付
145+
* @param TEnd $to 終了日付
146+
*
147+
* @return Result<bool,ValueObjectError>
148+
*/
149+
protected static function isValid(mixed $from, mixed $to): Result
150+
{
151+
return Result\ok(true);
152+
}
153+
126154
// -------------------------------------------------------------------------
127155
// MARK: public methods
128156
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)