spatie / period

Complex period comparisons
https://spatie.be/open-source
MIT License
1.56k stars 72 forks source link

Period Collection to accept array of periods #56

Closed kreierson closed 4 years ago

kreierson commented 4 years ago

I think it would be really nice if we could pass an array or collection of periods into a Period Collection.

Rather than having to do

$collection = new PeriodCollection(
    Period::make('2018-01-01', '2018-01-05'),
    Period::make('2018-01-10', '2018-01-15'),
    Period::make('2018-01-20', '2018-01-25'),
    Period::make('2018-01-30', '2018-01-31')
);

We could do something like this:

$event_periods = $events->map(function($event) {
     return Period::make($event->start, $event->end);
});
$event_period_collection = new PeriodCollection($event_periods);

Let me know if that doesn't make sense.

kreierson commented 4 years ago

I guess my issue is I can get an array/collection of periods, but I am lacking knowledge on how to get these periods into the comma separated form to pass into the period collection properly. There might be some cool trick I am unaware of to accomplish this.

The trouble is, I don't know the exact number of periods that will be passed into the Period Collection. In my example, I might have a calendar of events stored in a database, so if I want to check the overlap for a certain date range against existing events, it's easy to get a collection of events and create a collection of periods. But the Period Collection does not accept a collection/array.

brendt commented 4 years ago

You should use the spread operator:

new PeriodCollection(...$event_periods);