-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from fykosak/dev-datetime-period-3
DateTime Period
- Loading branch information
Showing
18 changed files
with
149 additions
and
161 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fykosak\Utils\DateTime; | ||
|
||
class Period | ||
{ | ||
public function __construct( | ||
public readonly \DateTimeImmutable $begin, | ||
public readonly \DateTimeImmutable $end, | ||
) { | ||
if ($this->begin > $this->end) { | ||
throw new \LogicException(); | ||
} | ||
} | ||
|
||
public function isBefore(?\DateTimeInterface $dateTime = null): bool | ||
{ | ||
return $this->begin > ($dateTime ?? new \DateTimeImmutable()); | ||
} | ||
|
||
public function isAfter(?\DateTimeInterface $dateTime = null): bool | ||
{ | ||
return $this->end < ($dateTime ?? new \DateTimeImmutable()); | ||
} | ||
|
||
public function isOnGoing(?\DateTimeInterface $dateTime = null): bool | ||
{ | ||
return $this->begin <= ($dateTime ?? new \DateTimeImmutable()) | ||
&& $this->end >= ($dateTime ?? new \DateTimeImmutable()); | ||
} | ||
|
||
public function is(Phase $period, ?\DateTimeInterface $dateTime = null): bool | ||
{ | ||
return match ($period) { | ||
Phase::before => $this->isBefore($dateTime), | ||
Phase::after => $this->isAfter($dateTime), | ||
Phase::onGoing => $this->isOnGoing($dateTime) | ||
}; | ||
} | ||
|
||
public function getPhase(?\DateTimeInterface $dateTime = null): Phase | ||
{ | ||
if ($this->isBefore($dateTime)) { | ||
return Phase::before; | ||
} | ||
if ($this->isAfter($dateTime)) { | ||
return Phase::after; | ||
} | ||
if ($this->isOnGoing($dateTime)) { | ||
return Phase::onGoing; | ||
} | ||
throw new \LogicException(); | ||
} | ||
|
||
public function duration(): \DateInterval | ||
{ | ||
return $this->end->diff($this->begin); | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fykosak\Utils\DateTime; | ||
|
||
enum Phase | ||
{ | ||
case before; | ||
case after; | ||
case onGoing; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fykosak\Utils\Logging; | ||
|
||
enum MessageLevel: string | ||
{ | ||
case Error = 'danger'; | ||
case Warning = 'warning'; | ||
case Success = 'success'; | ||
case Info = 'info'; | ||
case Primary = 'primary'; | ||
} |
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
Oops, something went wrong.