Last active
December 20, 2016 14:53
-
-
Save ahsio/cf3bf184418b0552bb62875df21fff60 to your computer and use it in GitHub Desktop.
test.php
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
{ | |
"require": { | |
"simshaun/recurr": "^2.1" | |
} | |
} |
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"; | |
use Recurr\Rule; | |
use Recurr\Transformer\ArrayTransformer; | |
use Recurr\Transformer\Constraint\BetweenConstraint; | |
class WeekDayAvailability | |
{ | |
private $weekDay; | |
private $availabilityList = []; | |
public function __construct($weekday, $availabilityList) | |
{ | |
$this->weekDay = $weekday; | |
$this->availabilityList = $availabilityList; | |
} | |
} | |
$availabilities = [ | |
[ | |
"maxCovers" => null, | |
"timeSlotRangeList" => [ | |
[720, 780], | |
[1020, 150] | |
], | |
"recurrence" => "FREQ=WEEKLY;BYDAY=MO" | |
], | |
[ | |
"maxCovers" => 20, | |
"timeSlotRangeList" => [ | |
[820, 880] | |
], | |
"recurrence" => "FREQ=WEEKLY;BYDAY=MO,TU" | |
], | |
[ | |
"maxCovers" => 30, | |
"timeSlotRangeList" => [ | |
[760, 820] | |
], | |
"recurrence" => "FREQ=WEEKLY;BYDAY=MO,TU,WE" | |
] | |
]; | |
function transformRecurrences($availabilityList) | |
{ | |
// 1 - Explode availability recurrences by weekday | |
$explodedAvailabilityList = []; | |
$transformer = new ArrayTransformer(); | |
$constraint = new BetweenConstraint( | |
new \DateTime(), | |
new \DateTime("+6 days"), | |
true | |
); | |
foreach ($availabilityList as $availability) { | |
$rule = new Rule($availability['recurrence'], null, null, 'UTC'); | |
$explodedDates = $transformer->transform($rule, $constraint); | |
$maxCovers = isset($availability['maxCovers']) ? $availability['maxCovers'] : -1; | |
foreach ($explodedDates as $date) { | |
$weekDay = $date->getStart()->format('w'); | |
$explodedAvailabilityList[$weekDay][$maxCovers] = $availability['timeSlotRangeList']; | |
} | |
} | |
// 2 - build and set 7 weekday availabilities | |
$list = []; | |
for ($weekDayNum = 0; $weekDayNum < 7; $weekDayNum++) { | |
$weekDayTimeSlotList = []; | |
// Fill weekdayAvailabilities with provided $timeSlotListData if it exists | |
if (array_key_exists($weekDayNum, $explodedAvailabilityList)) { | |
foreach ($explodedAvailabilityList[$weekDayNum] as $maxCovers => $timeSlotListData) { | |
$explodedAvailabilityList[] = [ | |
'maxCovers' => -1 === $maxCovers ? null : $maxCovers, | |
'slots' => $timeSlotListData, | |
]; | |
} | |
} | |
$list[$weekDayNum] = new WeekDayAvailability($weekDayNum, $weekDayTimeSlotList); | |
} | |
return $list; | |
} | |
$time_start = microtime(true); | |
$list = transformRecurrences($availabilities); | |
$time_end = microtime(true); | |
$time = $time_end - $time_start; | |
echo 'Durée : '.$time.' secondes<br/>'; | |
//var_dump($list);die; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment