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

Validate date intervals - start date must be before end date #124

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
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
41 changes: 38 additions & 3 deletions src/EDTFUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,52 @@ public static function validate($edtf_text, $intervals = TRUE, $sets = TRUE, $st
if (strpos($edtf_text, 'T') !== FALSE) {
$msgs[] = "Date intervals cannot include times.";
}
foreach (explode('/', $edtf_text) as $date) {
if (!empty($date) && !($date == '..')) {
$msgs = array_merge($msgs, self::validateDate($date, $strict));
$dates = explode('/', $edtf_text);
if (count($dates) == 2) {
if (!empty($dates[0]) && !($dates[0] == '..')) {
$msgs = array_merge($msgs, self::validateDate($dates[0], $strict));
}
if (!empty($dates[1]) && !($dates[1] == '..')) {
$msgs = array_merge($msgs, self::validateDate($dates[1], $strict));
}
// Check if start date is sooner than end date.
if (self::compareDates($dates[0], $dates[1]) > 0) {
$msgs[] = "The start date must be sooner than the end date.";
}
}
else {
$msgs[] = "Invalid interval format.";
}
return $msgs;
}
// Single date (we assume at this point).
return self::validateDate($edtf_text, $strict);
}

/**
* Compare two EDTF dates.
*
* @param string $date1
* The first date.
* @param string $date2
* The second date.
*
* @return int
* Returns -1, 1 or 0 based on the order of the two dates.
*/
private static function compareDates($date1, $date2) {
$isoDate1 = self::iso8601Value($date1);
$isoDate2 = self::iso8601Value($date2);

if ($isoDate1 < $isoDate2) {
return -1;
}
elseif ($isoDate1 > $isoDate2) {
return 1;
}
return 0;
}

/**
* Validate a single date.
*
Expand Down
1 change: 1 addition & 0 deletions tests/src/Kernel/EdtfUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class EdtfUtilsTest extends KernelTestBase {
'1900-01-02T01:22:33Z' => [],
'1900-01-02T01:22:33+' => ['The date/time \'1900-01-02T01:22:33+\' is invalid.'],
'1900-01-02T01:22:33+05:00' => [],
'2025-01-01/2000-01-01' => ['The start date must be sooner than the end date.'],
];

/**
Expand Down
Loading