SplittableDate.php 5.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php


namespace Source\Entities;

use Carbon\Carbon;

class SplittableDate
{
    /**
     * @var \Carbon\Carbon
     */
    protected Carbon $begin;

    /**
     * @var \Carbon\Carbon
     */
    protected Carbon $end;

    /**
     * SplittableDate constructor.
     *
     * @param \Carbon\Carbon $begin
     * @param \Carbon\Carbon $end
     */
    public function __construct(Carbon $begin, Carbon $end)
    {
        $this->begin = $begin;
        $this->end = $end;
    }

    /**
     * @return \Carbon\Carbon
     */
    public function getBegin(): Carbon
    {
        return $this->begin;
    }

    /**
     * @return \Carbon\Carbon
     */
    public function getEnd(): Carbon
    {
        return $this->end;
    }

    /**
     * @param \Carbon\Carbon $begin
     * @return \Source\Entities\SplittableDate
     */
    public function setBegin(Carbon $begin): static
    {
        $this->begin = $begin;

        return $this;
    }

    /**
     * @param \Carbon\Carbon $end
     * @return \Source\Entities\SplittableDate
     */
    public function setEnd(Carbon $end): static
    {
        $this->end = $end;

        return $this;
    }

    /**
     * Determines if the current date set engulfs another
     *
     * @param \Source\Entities\SplittableDate $s
     * @return bool
     */
76
    public function engulfs(SplittableDate $s): bool
77
78
79
80
81
82
83
84
    {
        return $this->getBegin() <= $s->getBegin() && $this->getEnd() >= $s->getEnd();
    }

    /**
     * @param \Source\Entities\SplittableDate $s
     * @return bool
     */
85
    public function hasNoOverlapWith(SplittableDate $s): bool
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    {
        return $this->getBegin() >= $s->getEnd() || $this->getEnd() <= $s->getBegin();
    }

    /**
     * Mangles the set passed in.
     * Ensures there are no date sets.
     * This method squashes any overlapping date sets into one continuous range.
     *
     * REQUIRES THAT THE SET IS ORDERED BY BEGIN DATE.
     *
     * @param static[] $set
     * @return static[]
     */
    public static function mergeSet(array $set): array
    {
        if (count($set) < 1) {
            return [];
        }

        $merged = [];

        $prev = array_shift($set);

        foreach ($set as $item) {
            if ($item->getBegin() <= $prev->getEnd()) {
                if ($prev->getEnd() < $item->getEnd()) {
                    $prev->setEnd($item->getEnd());
                }
            } else {
                $merged[] = $prev;
                $prev = $item;
            }
        }

121
        $merged[] = $prev;
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

        return $merged;
    }

    /**
     * This method splits a date range into segments based upon a list of date ranges. It will split itself until
     * there is no overlap between the passed in set of date ranges.
     * THE SET PASSED IN MUST BE SORTED BY BEGIN
     *
     * Do not use the object after calling this method on it.
     *
     * @param static[] $sortedSet
     * @param bool     $merged Has the set been squashed to have no overlaps?
     * @return static[]
     */
    public function split(array $sortedSet, bool $merged = false): array
    {
        /*
         * We will advance our begin through the ranges until we are done.
         */

        $splits = [];

        if (!$merged) {
            $sortedSet = self::mergeSet($sortedSet);
        }

        foreach ($sortedSet as $item) {
            if ($this->hasNoOverlapWith($item)) {
                // If we have processed all in our range break so we can return the split.
                if ($item->getBegin() >= $this->getEnd()) {
                    break;
                }
            } /*
             * Closed engulfs an open
             *     |----this---|
             * |-----item--------|
             * In this case return as no more ranges are allowed.
             */
            elseif ($item->engulfs($this)) {
                // No need to process other items on this as we throw the rest out.
                // Return the splits up to this point and call it a day.
                return $splits;
            } /*
             * This engulfs an item
             * |--------this---------|
             *     |---item---|
             * In this case add the split item to the return set and advance our start.
             */
            elseif ($this->engulfs($item)) {
                /*
                 * |-this-a-|
                 *          |--item--|
                 */
                $splits[] = (clone $this)->setEnd($item->getBegin());

                /*
                 * Move our begin date along.
                 *          |-this-|
                 * |--item--|
                 */
                $this->setBegin($item->getEnd());
            } /*
             * we are at the right side of an item
             *          |---this---|
             * |-----item----|
             * Advance our start and continue on.
             */
            elseif ($this->getEnd() > $item->getEnd()) {
                $this->setBegin($item->getEnd());
            } /*
             * Open is at the left side of a closed
             * |---this---|
             *       |-----item----|
             * In this case cut to the beginning of the item
             */
            elseif ($this->getBegin() < $item->getBegin()) {
                $splits[] = $this->setEnd($item->getBegin());

                // We end here because since all items are supposed to be sorted by start date
                // we have done all the processing we need to as nothing after this is going to have any overlap
                // with us.
                return $splits;
            } else {
                // Skip it. IDK what it would be. I think we cover all other cases???
                continue;
            }
        }

        // Add last split.
        $splits[] = $this;

        return $splits;
    }
}