Skip to content

Commit

Permalink
Improve regexp for matching durations; allow P1W week-indicators and …
Browse files Browse the repository at this point in the history
…fractional years
  • Loading branch information
tvdijen committed May 9, 2024
1 parent c1dcd95 commit c03e67b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/CustomAssertionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
trait CustomAssertionTrait
{
/** @var non-falsy-string */
private static string $duration_regex = '/^(-?)P(?=.)((\d+)Y)?((\d+)M)?((\d+)D)?(T(?=.)((\d+)H)?((\d+)M)?(\d*(\.\d+)?S)?)?$/i';
private static string $duration_regex = '/^([-+]?)P(?!$)(?:(?<years>\d+(?:[\.\,]\d+)?)Y)?(?:(?<months>\d+(?:[\.\,]\d+)?)M)?(?:(?<weeks>\d+(?:[\.\,]\d+)?)W)?(?:(?<days>\d+(?:[\.\,]\d+)?)D)?(T(?=\d)(?:(?<hours>\d+(?:[\.\,]\d+)?)H)?(?:(?<minutes>\d+(?:[\.\,]\d+)?)M)?(?:(?<seconds>\d+(?:[\.\,]\d+)?)S)?)?$/';

/** @var non-falsy-string */
private static string $qname_regex = '/^[a-zA-Z_][\w.-]*:[a-zA-Z_][\w.-]*$/';
Expand Down
4 changes: 3 additions & 1 deletion tests/Assert/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ public static function provideDuration(): array
return [
[true, 'P2Y6M5DT12H35M30S'],
[true, 'P1DT2H'],
[true, 'P1W'],
[true, 'P20M'],
[true, 'PT20M'],
[true, 'P0Y20M0D'],
[true, 'P0Y'],
[true, '-P60D'],
[true, 'PT1M30.5S'],
[true, 'P15.5Y'],
[true, 'P15,5Y'],
[false, 'P-20M'],
[false, 'P20MT'],
[false, 'P1YM5D'],
[false, 'P15.5Y'],
[false, 'P1D2H'],
[false, '1Y2M'],
[false, 'P2M1Y'],
Expand Down

0 comments on commit c03e67b

Please sign in to comment.