Skip to content

Commit bd6c1e1

Browse files
committed
Fix Time\Duration polyfill public API implementation
1 parent 06b467b commit bd6c1e1

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/Time/Resources/stubs/Time/Duration.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public static function fromHours(int $hours): self
176176
* Parse a ISO-8601 period. ISO-8601 periods with a date component will be rejected.
177177
* The biggest allowed component is H.
178178
*/
179-
public static function fromIso8601String(string $specification): self
179+
public static function fromIso8601DurationString(string $specification): self
180180
{
181181
if (preg_match(self::REGEXP_ISO8601, $specification, $parts) !== 1) {
182182
throw new TimeException("The submitted duration `$specification` is invalid or contains unsupported ISO 8601 duration components.");
@@ -210,6 +210,15 @@ public function negate(): self
210210
return new self($this->seconds, $this->nanoseconds, !$this->negative);
211211
}
212212

213+
public function absolute(): self
214+
{
215+
if ($this->negative === true) {
216+
return $this->negate();
217+
}
218+
219+
return $this;
220+
}
221+
213222
/**
214223
* Add the given duration to the duration.
215224
*

tests/Time/DurationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function testParseIso8601(
132132
int $nanoseconds,
133133
bool $negative
134134
) {
135-
$duration = Duration::fromIso8601String($spec);
135+
$duration = Duration::fromIso8601DurationString($spec);
136136

137137
$this->assertSame($seconds, $duration->seconds);
138138
$this->assertSame($nanoseconds, $duration->nanoseconds);
@@ -162,7 +162,7 @@ public function testRejectInvalidIso8601(string $spec)
162162
{
163163
$this->expectException(TimeException::class);
164164

165-
Duration::fromIso8601String($spec);
165+
Duration::fromIso8601DurationString($spec);
166166
}
167167

168168
/**
@@ -349,7 +349,7 @@ public function testItRejectsInvalidIso8601Durations(string $specification)
349349
{
350350
$this->expectException(TimeException::class);
351351

352-
Duration::fromIso8601String($specification);
352+
Duration::fromIso8601DurationString($specification);
353353
}
354354

355355
/**

0 commit comments

Comments
 (0)