Skip to content

Commit

Permalink
add TimeInterval property for object comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Apr 23, 2019
1 parent 38117aa commit d4f88c7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/ValueObject/TimeInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
*/
class TimeInterval extends DateInterval implements JsonSerializable
{
/**
* interval seconds
*
* for comparison object
*
* @var int
*/
public $seconds;

/**
* Short time string parse as HH:MM
*
Expand All @@ -36,6 +45,16 @@ class TimeInterval extends DateInterval implements JsonSerializable
*/
protected static $toJsonFormat = '%r%H:%I:%S';

/**
* {@inheritDoc}
*/
public function __construct($interval_spec)
{
parent::__construct($interval_spec);

$this->seconds = $this->toSeconds();
}

/**
* {@inheritDoc}
*
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/ValueObject/TimeIntervalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,17 @@ public function dataCreateFromSeconds()
[-86401, '-24:00:01'],
];
}

public function testCanCompare()
{
$a = TimeInterval::createFromString('00:00:00');
$b = TimeInterval::createFromSeconds(0);
$c = TimeInterval::createFromString('00:00:01');
$d = TimeInterval::createFromSeconds(1);

$this->assertTrue($a == $b);
$this->assertTrue($c == $d);
$this->assertFalse($a == $c);
$this->assertFalse($b == $d);
}
}

0 comments on commit d4f88c7

Please sign in to comment.