Skip to content

Commit

Permalink
Dont force leading zeros and allow for integer|string type arguments …
Browse files Browse the repository at this point in the history
…in Time class
  • Loading branch information
ceeram committed Feb 12, 2016
1 parent ee219e6 commit 321c368
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ final class Time
/**
* Creates a new time.
*
* @param string $hours
* @param string $minutes
* @param string $seconds Optional seconds with leading zero
* @param string|int $hours
* @param string|int $minutes
* @param string|int $seconds Optional seconds
*/
public function __construct($hours, $minutes, $seconds = '00')
public function __construct($hours, $minutes, $seconds = 0)
{
$this->hours = $hours;
$this->minutes = $minutes;
$this->seconds = $seconds;
$this->hours = (int) $hours;
$this->minutes = (int) $minutes;
$this->seconds = (int) $seconds;
}

/**
Expand Down Expand Up @@ -99,7 +99,7 @@ public function isAfterOrEqual(Time $other)
*/
public function getHours()
{
return (int) $this->hours;
return $this->hours;
}

/**
Expand All @@ -109,7 +109,7 @@ public function getHours()
*/
public function getMinutes()
{
return (int) $this->minutes;
return $this->minutes;
}

/**
Expand All @@ -119,7 +119,7 @@ public function getMinutes()
*/
public function getSeconds()
{
return (int) $this->seconds;
return $this->seconds;
}

/**
Expand All @@ -129,7 +129,7 @@ public function getSeconds()
*/
public function toInteger()
{
return (int) $this->hours.$this->minutes.$this->seconds;
return (int) sprintf('%d%02d%02d', $this->hours, $this->minutes, $this->seconds);
}

/**
Expand All @@ -139,6 +139,6 @@ public function toInteger()
*/
public function toString()
{
return sprintf('%s:%s:%s', $this->hours, $this->minutes, $this->seconds);
return sprintf('%02d:%02d:%02d', $this->hours, $this->minutes, $this->seconds);
}
}
4 changes: 4 additions & 0 deletions tests/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,9 @@ public function testToString()
{
$time = new Time('20', '30');
$this->assertEquals('20:30:00', $time->toString());
$time = new Time('9', '8', '7');
$this->assertEquals('09:08:07', $time->toString());
$time = new Time(9, 8, 7);
$this->assertEquals('09:08:07', $time->toString());
}
}

0 comments on commit 321c368

Please sign in to comment.