diff --git a/config/regex.txt b/config/regex.txt index 35f5cc9..c4117f1 100644 --- a/config/regex.txt +++ b/config/regex.txt @@ -2,7 +2,7 @@ # Year start ^(? - (?[~?%]{0,2}) + (?[~?%]{0,1}) Y? (? [+-]? # optional sign @@ -14,7 +14,7 @@ (?>S # Literal S letter. It is for the significant digit indicator (?\d+) )? - (?\)?[~%?]{0,2}) + (?\)?[~%?]{0,1}) ) # Year end @@ -22,24 +22,24 @@ # Month start (? - (?[~?%]{0,2}) + (?[~?%]{0,1}) (?\(+)? (?(?>1[0-9UX]|[0UX][0-9UX]|[0-9][0-9])) (?>\^(?[\P{L}\P{N}\P{M}:.-]+))? - (?[~?%]{0,2}) + (?[~?%]{0,1}) ) # Month end (?>- # Begin Day Literal - (hyphen) # Day start (? - (?[~?%]{0,2}) + (?[~?%]{0,1}) (?\(+)? (? (?>[012UX][0-9UX]|3[01UX]) ) ) - (?[~?%]{0,2}) + (?[~?%]{0,1}) (?[)~%?]*) # Day end diff --git a/src/PackagePrivate/Parser/Parser.php b/src/PackagePrivate/Parser/Parser.php index f6de06f..64ca248 100644 --- a/src/PackagePrivate/Parser/Parser.php +++ b/src/PackagePrivate/Parser/Parser.php @@ -209,7 +209,10 @@ private function buildQualification(): Qualification { // TODO private static function genQualificationValue( ?string $flag = null ): int { - assert( is_string( $flag ) ); + if ( !is_string( $flag ) || !array_key_exists( $flag, self::$map ) ) { + throw new InvalidArgumentException( 'Qualifier is invalid' ); + } + return (int)self::$map[$flag]; } diff --git a/tests/Unit/PackagePrivate/Parser/ParserTest.php b/tests/Unit/PackagePrivate/Parser/ParserTest.php index 922959d..a530d03 100644 --- a/tests/Unit/PackagePrivate/Parser/ParserTest.php +++ b/tests/Unit/PackagePrivate/Parser/ParserTest.php @@ -292,4 +292,27 @@ public function setOpenMiddleNonExtDateValues(): array { [ "{2000%..2000-02}", "Dates in set ranges cannot be uncertain" ], ]; } + + /** + * @dataProvider provideCombinedUncertainAndApproximateQualifiers + */ + public function testThrowExceptionWhenUsedCombinedUncertainAndApproximateQualifiers( string $combinedUncertainAndApproximate ): void { + $parser = new Parser(); + $this->expectException( InvalidArgumentException::class ); + $this->expectExceptionMessage( 'Invalid edtf format ' . $combinedUncertainAndApproximate ); + $parser->createEdtf( $combinedUncertainAndApproximate ); + } + + public function provideCombinedUncertainAndApproximateQualifiers(): array { + return [ + [ '1990?~' ], + [ '1990~?' ], + [ '?~1990}' ], + [ '~?1990' ], + [ '1990-02~?' ], + [ '1990-?~02' ], + [ '1990-?~02~?-03' ], + [ '1990-02-~?03' ], + ]; + } } \ No newline at end of file