Skip to content
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
23 changes: 23 additions & 0 deletions tests/src/Kernel/EdtfUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ class EdtfUtilsTest extends KernelTestBase {
'1900-01-02T01:22:33+05:00' => [],
];

/**
* Array of interval test inputs and expected outputs.
*
* @var array
*/
private $intervalValidations = [
'2000-01-01/2025-01-01' => [],
'2025-01-01/2000-01-01' => ['The start date must be sooner than the end date.'],
'1900/2023' => [],
'2023/1900' => ['The start date must be sooner than the end date.'],
'../2000' => [],
'2000/..' => [],
];

/**
* @covers ::validate
*/
Expand All @@ -60,6 +74,15 @@ public function testEdtfValidate() {
}
}

/**
* @covers ::validate
*/
public function testEdtfIntervalValidate() {
foreach ($this->intervalValidations as $input => $expected) {
$this->assertEquals($expected, EDTFUtils::validate($input, TRUE, FALSE, FALSE));
}
}

/**
* @covers ::iso8601Value
*/
Expand Down
Loading