Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of max word validation #379

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/qtism/runtime/tests/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
class Utils
{
private const MAX_ENTRY_RESTRICTION_PATTERN = '/^\/\^(?P<splitPattern>\([^{]+)\{(?P<min>\d+)(?:(?P<isRange>,)|,(?P<max>\d+))?\}\$\/\w*$/';
private const SOURCE_MAX_WORDS_SPLIT_PATTERN = '(?:(?:[^\s\:\!\?\;\…\€]+)[\s\:\!\?\;\…\€]*)';
private const MAX_WORDS_SPLIT_PATTERN = '/[\s.,:;?!&#%\/*+=]+/';
private const CLOSE_MATCH_GROUP_TOKEN = ')';
private const OPEN_MATCH_GROUP_TOKEN = '(';

Expand Down Expand Up @@ -90,21 +92,23 @@ public static function isResponseValid(QtiDatatype $response = null, ResponseVal
$patternMask = OperatorUtils::prepareXsdPatternForPcre($patternMask);

$isMaxEntryRestriction = preg_match(self::MAX_ENTRY_RESTRICTION_PATTERN, $patternMask, $matches)
&& self::isSingleMatchGroup($patternMask);
&& self::isSingleMatchGroup($patternMask)
&& $matches['splitPattern'] === self::SOURCE_MAX_WORDS_SPLIT_PATTERN;


foreach ($values as $value) {
$result = @preg_match($patternMask, (string)$value);
if ($isMaxEntryRestriction) {
[$min, $max] = self::extractMaxEntryRestrictions($matches);
$entries = count(array_filter(preg_split(self::MAX_WORDS_SPLIT_PATTERN, $value)));
if ($entries > $max || $entries < $min) {
return false;
}
} else {
$result = @preg_match($patternMask, (string)$value);

if ($result === 0) {
return false;
} elseif ($result === false) {
if ($isMaxEntryRestriction) {
[$splitPattern, $min, $max] = self::extractMaxEntryRestrictionsRestrictions($matches);
$entries = count(preg_split("/$splitPattern/", $value)) - 1;
if ($entries > $max || $entries < $min) {
return false;
}
} else {
if ($result === 0) {
return false;
} elseif ($result === false) {
throw new RuntimeException(OperatorUtils::lastPregErrorMessage());
}
}
Expand Down Expand Up @@ -149,12 +153,12 @@ private static function isSingleMatchGroup(string $patternMask): bool
/**
* @return array [(string)$splitPattern, (int)$min, (int)$max]
*/
private static function extractMaxEntryRestrictionsRestrictions(array $matches): array
private static function extractMaxEntryRestrictions(array $matches): array
{
extract($matches);
$isRange = !empty($isRange);
$max ??= $isRange ? PHP_INT_MAX : $min;

return [$splitPattern, (int)$min, (int)$max];
return [(int)$min, (int)$max];
}
}