I have this function in my code, this way I validate if a new movie session can be inserted, it works, but I wanted to find a better way to do it.
public static function validateSession($newSession)
{
$sessions = Sessions::get();
foreach($sessions as $session){
//Validates if the time and date of sessions are the same as the existing ones
if (
$newSession['session_date'] == $session->session_date
&& $newSession['room_id'] == $session->room_id
) {
//Validates if the session time is the same as an existing one
if ($newSession['session_id'] == $session->session_id)
{
return false;
}
//Validates if the time of the new session is greater than the start time and less than the end time of existing sessions
elseif (
$newSession['session_hour'] > $session->session->session_hour
&& $newSession['session_hour'] < $session->end_of_session
) {
return false;
}
//Validates if the time of the new session is less than the start time and the end time of the new session is greater than the start time of the existing sessions
elseif (
$newSession['session_hour'] < $session->session->session_hour
&& $newSession['end_of_session'] > $session->session->session_hour
) {
return false;
}
}
}
return true;
}
I tried to make a switch structure and even use the spaceship operator, but I was not successful, I wanted to find a better way to do this type of validation.