Add checks for time logs

This commit is contained in:
David Bomba 2023-02-08 10:59:36 +11:00
parent eb1cac7098
commit ebf99689d1
4 changed files with 99 additions and 8 deletions

View File

@ -195,4 +195,56 @@ class Request extends FormRequest
public function prepareForValidation()
{
}
public function checkTimeLog(array $log): bool
{
if(count($log) == 0)
return true;
/*Get first value of all arrays*/
$result = array_column($log, 0);
/*Sort the array in ascending order*/
asort($result);
$new_array = [];
/*Rebuild the array in order*/
foreach($result as $key => $value)
$new_array[] = $log[$key];
/*Iterate through the array and perform checks*/
foreach($new_array as $key => $array)
{
/*Flag which helps us know if there is a NEXT timelog*/
$next = false;
/* If there are more than 1 time log in the array, ensure the last timestamp is not zero*/
if(count($new_array) >1 && $array[1] == 0)
return false;
/* Check if the start time is greater than the end time */
/* Ignore the last value for now, we'll do a separate check for this */
if($array[0] > $array[1] && $array[1] != 0)
return false;
/* Find the next time log value - if it exists */
if(array_key_exists($key+1, $new_array))
$next = $new_array[$key+1];
/* check the next time log and ensure the start time is GREATER than the end time of the previous record */
if($next && $next[0] < $array[1])
return false;
/* Get the last row of the timelog*/
$last_row = end($new_array);
/*If the last value is NOT zero, ensure start time is not GREATER than the endtime */
if($last_row[1] != 0 && $last_row[0] > $last_row[1])
return false;
return true;
}
}
}

View File

@ -53,6 +53,9 @@ class StoreTaskRequest extends Request
$fail('The '.$attribute.' - '.print_r($k,1).' is invalid. Unix timestamps only.');
}
if(!$this->checkTimeLog($values))
$fail('Please correct overlapping values');
}];

View File

@ -59,6 +59,9 @@ class UpdateTaskRequest extends Request
$fail('The '.$attribute.' - '.print_r($k,1).' is invalid. Unix timestamps only.');
}
if(!$this->checkTimeLog($values))
$fail('Please correct overlapping values');
}];
return $this->globalRules($rules);

View File

@ -55,29 +55,25 @@ class TaskApiTest extends TestCase
foreach($result as $key => $value)
$new_array[] = $log[$key];
foreach($new_array as $key => $array)
{
$next = false;
if(count($new_array) >1 && $array[1] == 0)
return false;
/* First test is to check if the start time is greater than the end time */
/* Ignore the last value for now, we'll do a separate check for this */
if($array[0] > $array[1] && $array[1] != 0){
nlog("1");
return false;
}
if(array_key_exists($key+1, $new_array)){
nlog("2");
$next = $new_array[$key+1];
}
/* check the next time log and ensure the start time is GREATER than the end time of the previous record */
if($next && $next[0] < $array[1]){
nlog("3");
return false;
}
@ -85,14 +81,12 @@ class TaskApiTest extends TestCase
if($last_row[1] != 0 && $last_row[0] > $last_row[1]){
nlog($last_row[0]. " ".$last_row[1]);
nlog("4");
return false;
}
return true;
}
}
public function testTimeLogChecker1()
@ -186,7 +180,7 @@ class TaskApiTest extends TestCase
}
public function testTimeLogChecker8()
public function testTimeLogChecker8()
{
$log = [
@ -198,6 +192,45 @@ class TaskApiTest extends TestCase
}
public function testTimeLogChecker9()
{
$log = [
[4,5,'bb'],
[50,0,'aa'],
];
$this->assertTrue($this->checkTimeLog($log));
}
public function testTimeLogChecker10()
{
$log = [
[4,5,'5'],
[50,0,'3'],
];
$this->assertTrue($this->checkTimeLog($log));
}
public function testTimeLogChecker11()
{
$log = [
[1,2,'a'],
[3,4,'d'],
];
$this->assertTrue($this->checkTimeLog($log));
}
public function testTaskListClientStatus()
{