Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CWD
Common
Commits
6401425e
Commit
6401425e
authored
Oct 10, 2018
by
Niklas de Vries
Browse files
Fixed and optimized LocalTime
parent
c94e849f
Changes
1
Hide whitespace changes
Inline
Side-by-side
LocalTime.php
View file @
6401425e
...
...
@@ -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
(
str
in
g
$hour
,
str
in
g
$minute
,
str
in
g
$second
)
public
function
__construct
(
in
t
$hour
,
in
t
$minute
,
in
t
$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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment