spatie / period

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

Add/subtract collections.. #55

Closed jeremyswright closed 4 years ago

jeremyswright commented 4 years ago

I've just picked this library up and it's proving really useful. Could you tell me if/how it's possible to join periods? As in if I add a period to a period/collection, it would create a flattened/normalized collection of the two.

/*
 * A              [======]
 * B                   [=======]
 *
 * JOIN           [============]
 *
 * C         [===]
 * D                          [===]
 *
 * JOIN      [===]            [===]
 */

$a = Period::make('2018-01-01', '2018-01-15');
$b = Period::make('2018-01-10', '2018-01-25');

$joinCollection = $a->join($b);

$c = Period::make('2018-01-01', '2018-01-05');
$d = Period::make('2018-01-10', '2018-01-15');

$joinCollection = $c->join($d);

If I add a period to a collection it just holds the period even it they overlap.

I suspect it's a technique that I haven't figured out. A pointer would be very welcome.

Many thanks,

Jerry

brendt commented 4 years ago

I tend to make the collection manually:

$collection = new PeriodCollection();

$c = Period::make('2018-01-01', '2018-01-05');
$d = Period::make('2018-01-10', '2018-01-15');

$collection[] = $c;
$collection[] = $d;

Or, in its shortest form:

$collection = PeriodCollection::make(
    Period::make('2018-01-01', '2018-01-05'),
    Period::make('2018-01-10', '2018-01-15')
);

If you really want join, I'd be open for a PR.