Skip to content

Commit

Permalink
Additional check for QualificationValue (#100)
Browse files Browse the repository at this point in the history
* Add QualificationValue validation

* Add support for all approximate classifiers

* Reduce the number of selectable special characters

* Add tests

* Add review fixes
  • Loading branch information
apetushok authored Apr 25, 2024
1 parent a95ccfb commit 41d9be0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
12 changes: 6 additions & 6 deletions config/regex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Year start
^(?<year>
(?<yearOpenFlag>[~?%]{0,2})
(?<yearOpenFlag>[~?%]{0,1})
Y?
(?<yearNum>
[+-]? # optional sign
Expand All @@ -14,32 +14,32 @@
(?>S # Literal S letter. It is for the significant digit indicator
(?<yearSignificantDigit>\d+)
)?
(?<yearCloseFlag>\)?[~%?]{0,2})
(?<yearCloseFlag>\)?[~%?]{0,1})
)
# Year end

(?>- # Literal - (hyphen)

# Month start
(?<month>
(?<monthOpenFlag>[~?%]{0,2})
(?<monthOpenFlag>[~?%]{0,1})
(?<monthOpenParents>\(+)?
(?<monthNum>(?>1[0-9UX]|[0UX][0-9UX]|[0-9][0-9]))
(?>\^(?<seasonQualifier>[\P{L}\P{N}\P{M}:.-]+))?
(?<monthCloseFlag>[~?%]{0,2})
(?<monthCloseFlag>[~?%]{0,1})
)
# Month end

(?>- # Begin Day Literal - (hyphen)
# Day start
(?<day>
(?<dayOpenFlag>[~?%]{0,2})
(?<dayOpenFlag>[~?%]{0,1})
(?<dayOpenParents>\(+)?
(?<dayNum>
(?>[012UX][0-9UX]|3[01UX])
)
)
(?<dayCloseFlag>[~?%]{0,2})
(?<dayCloseFlag>[~?%]{0,1})
(?<dayEnd>[)~%?]*)
# Day end

Expand Down
5 changes: 4 additions & 1 deletion src/PackagePrivate/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/PackagePrivate/Parser/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ],
];
}
}

0 comments on commit 41d9be0

Please sign in to comment.