-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
18 changed files
with
909 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
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
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
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
129 changes: 129 additions & 0 deletions
129
...tracts/Repository/Values/Content/Query/Aggregation/Ranges/DateTimeStepRangesGenerator.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Ranges; | ||
|
||
use DateInterval; | ||
use DateTime; | ||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Range; | ||
|
||
final class DateTimeStepRangesGenerator implements RangesGeneratorInterface | ||
{ | ||
private DateTimeInterface $start; | ||
|
||
private DateTimeInterface $end; | ||
|
||
private DateInterval $step; | ||
|
||
private bool $isLeftOpen = true; | ||
|
||
private bool $isRightOpen = true; | ||
|
||
public function __construct(DateTimeInterface $start, DateTimeInterface $end) | ||
{ | ||
$this->start = $start; | ||
$this->end = $end; | ||
$this->step = new DateInterval('P1D'); | ||
} | ||
|
||
public function getStart(): DateTimeInterface | ||
{ | ||
return $this->start; | ||
} | ||
|
||
public function setStart(DateTimeInterface $start): self | ||
{ | ||
$this->start = $start; | ||
|
||
return $this; | ||
} | ||
|
||
public function getEnd(): DateTimeInterface | ||
{ | ||
return $this->end; | ||
} | ||
|
||
public function setEnd(DateTimeInterface $end): self | ||
{ | ||
$this->end = $end; | ||
|
||
return $this; | ||
} | ||
|
||
public function getStep(): DateInterval | ||
{ | ||
return $this->step; | ||
} | ||
|
||
public function setStep(DateInterval $step): self | ||
{ | ||
$this->step = $step; | ||
|
||
return $this; | ||
} | ||
|
||
public function isLeftOpen(): bool | ||
{ | ||
return $this->isLeftOpen; | ||
} | ||
|
||
public function setLeftOpen(bool $isLeftOpen): void | ||
{ | ||
$this->isLeftOpen = $isLeftOpen; | ||
} | ||
|
||
public function isRightOpen(): bool | ||
{ | ||
return $this->isRightOpen; | ||
} | ||
|
||
public function setRightOpen(bool $isRightOpen): self | ||
{ | ||
$this->isRightOpen = $isRightOpen; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Range[] | ||
*/ | ||
public function generate(): array | ||
{ | ||
if ($this->start == $this->end && $this->isLeftOpen && $this->isRightOpen) { | ||
return [ | ||
new Range(Range::INF, Range::INF), | ||
]; | ||
} | ||
|
||
$ranges = []; | ||
|
||
if ($this->isLeftOpen) { | ||
$ranges[] = Range::ofDateTime(Range::INF, $this->start); | ||
} | ||
|
||
/** @var \DateTimeImmutable $current */ | ||
$current = $this->start; | ||
if ($current instanceof DateTime) { | ||
$current = DateTimeImmutable::createFromMutable($current); | ||
} | ||
|
||
while ($current < $this->end) { | ||
$next = $current->add($this->step); | ||
$ranges[] = Range::ofDateTime($current, $next); | ||
$current = $next; | ||
} | ||
|
||
if ($this->isRightOpen) { | ||
$ranges[] = Range::ofDateTime($this->end, Range::INF); | ||
} | ||
|
||
return $ranges; | ||
} | ||
} |
Oops, something went wrong.