Commit 6401425e authored by Niklas de Vries's avatar Niklas de Vries
Browse files

Fixed and optimized LocalTime

parent c94e849f
......@@ -44,9 +44,9 @@ final class LocalTime
public static function fromDateTime(DateTimeInterface $dateTime): self
{
return new self(
$dateTime->format('H'),
$dateTime->format('i'),
$dateTime->format('s')
(int) $dateTime->format('H'),
(int) $dateTime->format('i'),
(int) $dateTime->format('s')
);
}
......@@ -55,9 +55,9 @@ final class LocalTime
* @param string $minute [0:59]
* @param string $second [0:59]
*/
public function __construct(string $hour, string $minute, string $second)
public function __construct(int $hour, int $minute, int $second)
{
if (!$this->isValidTime((int) $hour, (int) $minute, (int) $second)) {
if (!$this->isValidTime($hour, $minute, $second)) {
throw new InvalidArgumentException(sprintf(
'Invalid time: %d-%d-%d',
$hour,
......@@ -76,16 +76,67 @@ final class LocalTime
return $this->hour;
}
public function withHour(int $hour): self
{
if ($hour < 0 || $hour > 23) {
throw new InvalidArgumentException(sprintf(
'Invalid time: %d-%d-%d',
$hour,
$this->minute,
$this->second
));
}
$clone = clone $this;
$clone->hour = $hour;
return $clone;
}
public function getMinute(): string
{
return $this->minute;
}
public function withMinute(int $minute): self
{
if ($minute < 0 || $minute > 59) {
throw new InvalidArgumentException(sprintf(
'Invalid time: %d-%d-%d',
$this->hour,
$minute,
$this->second
));
}
$clone = clone $this;
$clone->minute = $minute;
return $clone;
}
public function getSecond(): string
{
return $this->second;
}
public function withSecond(int $second): self
{
if ($second < 0 || $second > 59) {
throw new InvalidArgumentException(sprintf(
'Invalid time: %d-%d-%d',
$this->hour,
$this->minute,
$second
));
}
$clone = clone $this;
$clone->second = $second;
return $clone;
}
public function toDateTime($timezone = null): DateTimeImmutable
{
if (null === $timezone) {
......@@ -104,7 +155,7 @@ final class LocalTime
return DateTimeImmutable::createFromFormat(
'H:i:s',
sprintf(
'%s:%s:%s',
'%02d:%02d:%02d',
$this->hour,
$this->minute,
$this->second
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment