Creating a TimeslotCollection - gpaddis/timeslot GitHub Wiki

The TimeslotCollection Class

The TimeslotCollection class allows you to manage of groups of Timeslots. The arguments accepted are a Timeslot instance and (optionally) an integer indicating how many timeslots the collection will contain.

$timeslot = Timeslot::create('2018-12-23 10:00:00');
$collection = TimeslotCollection::create($timeslot, 8);
// Creates a collection containing eight 1-hour timeslots, starting at 10:00:00 and ending at 17:59:59.

Adding timeslots to the collection

You can add timeslots to the collection calling ->add($timeslot) from the instance:

$timeslot = Timeslot::create('2018-12-23 18:00:00');
$collection->add($timeslot);
// Appends a timeslot to the collection: now the end time is set at 18:59:59.

Getting a timeslot from a collection

You can use ->get($offset) to get a Timeslot:

$timeslot = Timeslot::create('2018-12-23 10:00:00');
$collection = TimeslotCollection::create($timeslot, 8);

$collection->get(1)->start(); // 2018-12-23 11:00:00

You can also nest an arbitrary number of TimeslotCollections and retrieve them:

$hours = Timeslot::create('2018-12-23 10:00:00');
$tenMinutes = Timeslot::create('2018-12-23 13:00:00', 0, 10);
$tenMinutesCollection = TimeslotCollection::create($tenMinutes, 6);

$collection = TimeslotCollection::create($hours, 3);
$collection->add($tenMinutesCollection);

$collection->get(0)->start(); // 2018-12-23 10:00:00
$collection->get(1)->start(); // 2018-12-23 11:00:00
$collection->get(2)->start(); // 2018-12-23 12:00:00
$collection->get(3); // TimeslotCollection, 6 * 10 minutes
$collection->get(3)->get(0)->start(); // 2018-12-23 13:00:00
$collection->get(3)->get(1)->start(); // 2018-12-23 13:10:00

...and so on!