Skip to content

Commit

Permalink
Handle more input values
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Sep 3, 2019
1 parent fde3c2b commit dec2601
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## Unreleased

* PHP 7.3 is now the minimum required version
* PHP 7.3 is now the minimum required version.
* `0`, `null`, `false`, `true` are now supported values as in that they result in `Duration::none()` (`PT0S`).
* An `InvalidDuration` error will be thrown if a value is given without a unit or if the given value cannot be parsed.
* `toIntervalSpec()` did more than it needed to do. Instead of formatting the current value the spec itself is now returned.

## 3.0.1 - 2019-01-28
Expand Down
21 changes: 19 additions & 2 deletions src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateInterval;
use DateTimeImmutable;
use Gamez\Duration\Exception\InvalidDuration;
use Throwable;

final class Duration implements \JsonSerializable
{
Expand All @@ -28,7 +29,19 @@ public static function make($value): self
return new self($value);
}

$stringValue = (string) $value;
if (in_array($value, [0, null, false, true], true)) {
return self::none();
}

$stringValue = trim((string) $value);

if ($value === '') {
return self::none();
}

if (ctype_digit($stringValue)) {
throw InvalidDuration::because('A duration needs a unit');
}

if (preg_match('/^(\d+):(\d+)$/', $stringValue)) {
[$minutes, $seconds] = array_map('intval', explode(':', $stringValue));
Expand All @@ -46,7 +59,11 @@ public static function make($value): self
return new self(new DateInterval($stringValue));
}

return new self(DateInterval::createFromDateString($stringValue));
try {
return new self(DateInterval::createFromDateString($stringValue));
} catch (Throwable $e) {
throw InvalidDuration::because('Unable to parse value: '.$e->getMessage());
}
}

public static function none(): self
Expand Down
17 changes: 17 additions & 0 deletions tests/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public function it_parses_a_value($value, $expectedSpec)
public function validValues()
{
return [
'nothing (null)' => [null, 'PT0S'],
'nothing (false)' => [false, 'PT0S'],
'nothing (true)' => [true, 'PT0S'],
'nothing ("")' => ['', 'PT0S'],
'textual ("13 minutes 37 seconds")' => ['13 minutes 37 seconds', 'PT13M37S'],
'minutes:seconds ("01:23")' => ['01:23', 'PT1M23S'],
Expand All @@ -43,6 +46,20 @@ public function validValues()
];
}

/** @test */
public function it_needs_a_unit()
{
$this->expectException(InvalidDuration::class);
Duration::make(60);
}

/** @test */
public function it_needs_a_parseable_value()
{
$this->expectException(InvalidDuration::class);
Duration::make('xxx');
}

/** @test */
public function it_optimizes_the_date_interval_spec()
{
Expand Down

0 comments on commit dec2601

Please sign in to comment.