-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add intersects method for date range (#10)
- Loading branch information
Showing
6 changed files
with
590 additions
and
3 deletions.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,124 @@ | ||
<?php | ||
/* | ||
* This file is part of the DateTime library. | ||
* | ||
* (c) Artem Henvald <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fresh\DateTime; | ||
|
||
use Fresh\DateTime\Exception\LogicException; | ||
|
||
/** | ||
* DateTimeRange. | ||
* | ||
* @author Artem Henvald <[email protected]> | ||
*/ | ||
final class DateTimeRange implements DateTimeRangeInterface | ||
{ | ||
/** @var \DateTimeImmutable */ | ||
private $since; | ||
|
||
/** @var \DateTimeImmutable */ | ||
private $till; | ||
|
||
/** | ||
* @param \DateTimeInterface $since | ||
* @param \DateTimeInterface $till | ||
*/ | ||
public function __construct(\DateTimeInterface $since, \DateTimeInterface $till) | ||
{ | ||
$this->assertSameTimezones($since, $till); | ||
|
||
$this->since = DateTimeCloner::cloneIntoDateTimeImmutable($since); | ||
$this->till = DateTimeCloner::cloneIntoDateTimeImmutable($till); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTimezone(): \DateTimeZone | ||
{ | ||
return $this->since->getTimezone(); // Since and till timezones are equal | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTimezoneName(): string | ||
{ | ||
return $this->since->getTimezone()->getName(); // Since and till timezones are equal | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSince(): \DateTimeImmutable | ||
{ | ||
return $this->since; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTill(): \DateTimeImmutable | ||
{ | ||
return $this->till; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isEqual(DateTimeRangeInterface $dateTimeRange): bool | ||
{ | ||
$dateRangeSince = $dateTimeRange->getSince(); | ||
$dateRangeTill = $dateTimeRange->getTill(); | ||
|
||
return $this->since->getTimestamp() === $dateRangeSince->getTimestamp() | ||
&& $this->till->getTimestamp() === $dateRangeTill->getTimestamp() | ||
&& $this->since->getTimezone()->getName() === $dateRangeSince->getTimezone()->getName() | ||
&& $this->till->getTimezone()->getName() === $dateRangeTill->getTimezone()->getName(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function intersects(DateTimeRangeInterface $dateTimeRange): bool | ||
{ | ||
if ($this->getTimezoneName() !== $dateTimeRange->getTimezoneName()) { | ||
throw new LogicException('Timezones of datetime ranges are different'); | ||
} | ||
|
||
$givenDateRangeSince = $dateTimeRange->getSince(); | ||
$givenDateRangeTill = $dateTimeRange->getTill(); | ||
|
||
switch (true) { | ||
case $this->since === $givenDateRangeSince && $this->till === $givenDateRangeTill: // Current date range is equal to the given date range | ||
case $givenDateRangeSince < $this->since && $this->till < $givenDateRangeTill: // Current date range is fully inside the given date range | ||
case $this->since < $givenDateRangeSince && $givenDateRangeTill < $this->till: // Given date range is fully inside the current date range | ||
case $givenDateRangeSince <= $this->since && $this->since < $givenDateRangeTill: // Current date range beginning is inside the given date range | ||
case $givenDateRangeSince < $this->till && $this->till <= $givenDateRangeTill: // Current date range ending is inside the given date range | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* @param \DateTimeInterface $since | ||
* @param \DateTimeInterface $till | ||
* | ||
* @throws LogicException | ||
*/ | ||
private function assertSameTimezones(\DateTimeInterface $since, \DateTimeInterface $till): void | ||
{ | ||
if ($since->getTimezone()->getName() !== $till->getTimezone()->getName()) { | ||
throw new LogicException('Datetimes have different timezones'); | ||
} | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
/* | ||
* This file is part of the DateTime library. | ||
* | ||
* (c) Artem Henvald <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fresh\DateTime; | ||
|
||
/** | ||
* DateTimeRangeInterface. | ||
* | ||
* @author Artem Henvald <[email protected]> | ||
*/ | ||
interface DateTimeRangeInterface | ||
{ | ||
/** | ||
* @return \DateTimeZone | ||
*/ | ||
public function getTimezone(): \DateTimeZone; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getTimezoneName(): string; | ||
|
||
/** | ||
* @return \DateTimeImmutable | ||
*/ | ||
public function getSince(): \DateTimeImmutable; | ||
|
||
/** | ||
* @return \DateTimeImmutable | ||
*/ | ||
public function getTill(): \DateTimeImmutable; | ||
|
||
/** | ||
* @param DateTimeRangeInterface $dateTimeRange | ||
* | ||
* @return bool | ||
*/ | ||
public function isEqual(self $dateTimeRange): bool; | ||
|
||
/** | ||
* @param DateTimeRangeInterface $dateTimeRange | ||
* | ||
* @return bool | ||
*/ | ||
public function intersects(self $dateTimeRange): bool; | ||
} |
Oops, something went wrong.