Created
June 17, 2017 21:21
-
-
Save devmsh/82f5abd495baf6127cc85b9527a30f63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
const FREE_TIME = 1; | |
$user = [ | |
"workHours" => [ | |
["from" => "08:00:00", "to" => "16:00:00"], | |
[], | |
["from" => "08:00:00", "to" => "16:00:00"], | |
[], | |
["from" => "08:00:00", "to" => "16:00:00"], | |
[], | |
["from" => "08:00:00", "to" => "16:00:00"], | |
], | |
"sessions" => [ | |
[ | |
"date" => "17/6/2017", "status" => "approved", | |
"from" => "09:00:00", "to" => "11:00:00", | |
], | |
[ | |
"date" => "17/6/2017", "status" => "approved", | |
"from" => "16:00:00", "to" => "18:00:00", | |
], | |
[ | |
"date" => "17/6/2017", "status" => "new", | |
"from" => "12:00:00", "to" => "14:00:00", | |
], | |
], | |
]; | |
function toPeriod($workingHours) | |
{ | |
$period = new stdClass(); | |
$period->from = timeToNumber($workingHours['from']); | |
$period->to = timeToNumber($workingHours['to']); | |
return $period; | |
} | |
function timeToNumber($time) | |
{ | |
return date('H', strtotime($time)) + date('i', strtotime($time)) / 60; | |
} | |
function freeAt($user, $newSession) | |
{ | |
$workingDays = collect([ | |
"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", | |
])->combine($user['workHours']); | |
$weekDay = date('l', strtotime($newSession['date'])); | |
$newSessionPeriod = toPeriod($newSession); | |
$workingHours = $workingDays[$weekDay]; | |
if (empty($workingHours)) | |
throw new Exception("The user not working on {$weekDay}"); | |
$workingHoursPeriod = toPeriod($workingHours); | |
if (!($newSessionPeriod->from >= $workingHoursPeriod->from | |
&& $newSessionPeriod->to <= $workingHoursPeriod->to) | |
) { | |
throw new Exception("The user work only from {$workingHours['from']} to {$workingHours['to']}"); | |
} | |
$conflictSession = collect($user['sessions']) | |
->where('date', $newSession['date']) | |
->where('status', "!=", "new") | |
->first(function ($approvedSession) use ($newSessionPeriod) { | |
$approvedSessionPeriod = toPeriod($approvedSession); | |
if ($newSessionPeriod->to + FREE_TIME <= $approvedSessionPeriod->from) { | |
} elseif ($newSessionPeriod->from - FREE_TIME >= $approvedSessionPeriod->to) { | |
} else { | |
return true; | |
} | |
}); | |
if ($conflictSession) | |
throw new Exception("This session conflict with a session from {$conflictSession['from']} to {$conflictSession['to']}"); | |
return true; | |
} | |
try { | |
freeAt($user, ["date" => "17/6/2017", "from" => "12:00:00", "to" => "15:00:00"]); | |
echo "Yahoo!"; | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment