Merge branch 'master' of gitea:museum-digital/MDNodaHelpers
This commit is contained in:
@ -11,7 +11,59 @@ declare(strict_types = 1);
|
||||
*/
|
||||
final class NodaValidationHelper {
|
||||
|
||||
const ACTOR_DESCRIPTION_REQUIRED_DISTINCT_CHARS = 2;
|
||||
const ACTOR_DESCRIPTION_REQUIRED_DISTINCT_CHARS = 3;
|
||||
|
||||
/**
|
||||
* Validates an actor description for completeness. Of course, only an informed
|
||||
* guess based on the length and character composition of the description can be
|
||||
* made.
|
||||
*
|
||||
* @param string $description Input descrition.
|
||||
* @param string $name Names of the actor. Optional. Setting this enables
|
||||
* checks e.g. to prevent duplicating the actor name
|
||||
* as a description.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function validateTagDescription(string $description, string $name = ""):void {
|
||||
|
||||
// For nodac, empty tag descriptions are to be allowed. Thus, a
|
||||
// dedicated check for fully empty ones with a dedicated exception
|
||||
// is needed.
|
||||
if (empty($description)) {
|
||||
throw new MDInvalidEmptyInputException("No tag name is provided");
|
||||
}
|
||||
|
||||
// Throw error on descriptions that are too short
|
||||
if (\mb_strlen($description) < 10) {
|
||||
throw new MDgenericInvalidInputsException("Tag description is too short");
|
||||
}
|
||||
|
||||
// Validate tag description based on character composition.
|
||||
|
||||
// Ensure more than 3 distinct characters are used.
|
||||
$chars = \str_split($description);
|
||||
|
||||
$uniqueChars = array_unique($chars);
|
||||
if (count($uniqueChars) <= self::ACTOR_DESCRIPTION_REQUIRED_DISTINCT_CHARS) {
|
||||
throw new MDgenericInvalidInputsException("There need to be more than " . self::ACTOR_DESCRIPTION_REQUIRED_DISTINCT_CHARS . " distinct characters.");
|
||||
}
|
||||
|
||||
// Ensure more than the actor name is used.
|
||||
$clearedChars = [' ' => ' ', ',' => ' ', ';' => ' ', '.' => ' '];
|
||||
|
||||
$uniqueNames = array_unique(array_diff(explode(' ', strtr($name, $clearedChars)), ['']));
|
||||
sort($uniqueNames);
|
||||
|
||||
$descCleared = strtr($description, $clearedChars);
|
||||
$descWords = array_unique(array_diff(explode(' ', $descCleared), ['']));
|
||||
sort($descWords);
|
||||
|
||||
if ($uniqueNames === $descWords) {
|
||||
throw new MDgenericInvalidInputsException("The tag name was simply repeated in the description. This is not enough.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates an actor description for completeness. Of course, only an informed
|
||||
|
Reference in New Issue
Block a user