Add class NodaValidationHelper, for now for validating actor
descriptions
This commit is contained in:
@ -7,7 +7,8 @@
|
||||
declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* Contains static functions for getting IDs for noda entries by various means.
|
||||
* Contains static functions for identifying uncertainty or blocking
|
||||
* completely uncertain inputs for actors, times, and places.
|
||||
*/
|
||||
final class NodaUncertaintyHelper {
|
||||
|
||||
|
64
src/NodaValidationHelper.php
Normal file
64
src/NodaValidationHelper.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?PHP
|
||||
/**
|
||||
* Contains class NodaValidationHelper.
|
||||
*
|
||||
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
||||
*/
|
||||
declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* A class of static functions for validating single fields of noda entities.
|
||||
*/
|
||||
final class NodaValidationHelper {
|
||||
|
||||
const ACTOR_DESCRIPTION_REQUIRED_DISTINCT_CHARS = 2;
|
||||
|
||||
/**
|
||||
* 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[] $names 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 validateActorDescription(string $description, array $names = []):void {
|
||||
|
||||
// Throw error on descriptions that are too short
|
||||
if (\mb_strlen($description) < 10) {
|
||||
throw new MDgenericInvalidInputsException("Author description is too short");
|
||||
}
|
||||
|
||||
// Validate actor description based on character composition.
|
||||
|
||||
$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.");
|
||||
}
|
||||
|
||||
if (!empty($names)) {
|
||||
|
||||
$clearedChars = [' ' => ' ', ',' => ' ', ';' => ' ', '.' => ' '];
|
||||
|
||||
$namesMerged = implode(' ', $names);
|
||||
$namesMerged = strtr($namesMerged, $clearedChars);
|
||||
$uniqueNames = array_unique(array_diff(explode(' ', $namesMerged), ['']));
|
||||
sort($uniqueNames);
|
||||
|
||||
$descCleared = strtr($description, $clearedChars);
|
||||
$descWords = array_unique(array_diff(explode(' ', $descCleared), ['']));
|
||||
sort($descWords);
|
||||
|
||||
if ($uniqueNames === $descWords) {
|
||||
throw new MDgenericInvalidInputsException("The actor name was simply repeated in the description. This is not enough.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user