[TASK] EIDUtility: Improve function resolving and error translation handling

This commit is contained in:
Philipp Dieter 2022-11-09 19:18:34 +01:00
parent 252d398174
commit ec7bb84f5e
2 changed files with 151 additions and 11 deletions

View File

@ -19,6 +19,7 @@ use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
@ -88,6 +89,16 @@ class AbstractEIDController
*/
protected $importLogger = null;
/**
* uriMapping
*/
protected $uriMapping = null;
/**
* uriMappingValues
*/
protected $uriMappingValues = [];
/*
* returns the extensionkey set in the exended calss
@ -189,6 +200,8 @@ class AbstractEIDController
0,
true
);
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageService::class);
$GLOBALS['LANG']->init('default');
$GLOBALS['TSFE'] = $frontendController;
$frontendController->connectToDB();
$frontendController->fe_user = EidUtility::initFeUser();
@ -219,6 +232,34 @@ class AbstractEIDController
return $response->withStatus(404);
}
$httpMethod = strtolower($request->getMethod());
if ($this->uriMapping) {
$uriParts = explode('/', $request->getUri()->getPath());
$uriParts = array_slice($uriParts, 3);
foreach ($this->uriMapping[$httpMethod] as $mapping => $function) {
$mappingParts = explode('/', $mapping);
$mappingParts = array_slice($mappingParts, 1);
$max = max(count($mappingParts), count($uriParts));
$mappingMatching = true;
for ($i = 0; $i < $max; $i++) {
if ($uriParts[$i] == $mappingParts[$i]) {
continue;
}
if (
$uriParts[$i]
&& substr($mappingParts[$i], 0, 1) == '{'
&& substr($mappingParts[$i], -1, 1) == '}'
) {
$mappingKey = substr($mappingParts[$i], 1, -1);
$this->uriMappingValues[$mappingKey] = $uriParts[$i];
continue;
}
$mappingMatching = false;
}
if ($mappingMatching == true) {
$requestMethod = $function . 'Request';
}
}
} else {
if ($apiObjectId) {
$requestMethod = $httpMethod
. ucfirst($apiObject)
@ -229,6 +270,7 @@ class AbstractEIDController
. ucfirst($apiObject)
. 'Request';
}
}
if (method_exists($this, $requestMethod)) {
$responseData = $this->$requestMethod($request, $response);
$response = $response->withHeader(

View File

@ -119,8 +119,9 @@ trait ValidationTrait
* @param schema
* @return void
*/
protected function validateAgainstSchema($input, $schema)
{
protected function validateAgainstSchema(
$input, $schema, $translate = false
) {
$validator = new Validator();
$input = ArrayUtility::removeEmptyStrings($input);
if (is_array($input) && array_key_exists('eID', $input)) {
@ -141,6 +142,7 @@ trait ValidationTrait
json_encode($schema),
-1
);
if (!$validationResult->isValid()) {
$this->isValid = false;
$this->responseStatus = [400 => 'validationError'];
@ -166,6 +168,9 @@ trait ValidationTrait
];
}
}
if ($translate) {
$this->translateErrorMessages($validationResult);
}
}
return $validationResult;
}
@ -213,7 +218,8 @@ trait ValidationTrait
$translation = LocalizationUtility::translate(
$key,
$this->getExtensionKey(),
$arguments
$arguments,
'de'
);
if ($translation) {
return $translation;
@ -229,5 +235,97 @@ trait ValidationTrait
return null;
}
/**
* translate error messages to user readable strings
*/
protected function translateErrorMessages($validationResult)
{
foreach ($validationResult->getErrors() as $error){
$errorLabel = null;
$field = implode('.', $error->dataPointer());
if ($error->keyword() == 'required') {
$tmp = $error->dataPointer();
array_push($tmp, $error->keywordArgs()['missing']);
$field = implode('.', $tmp);
}
if ($error->keyword() == 'additionalProperties') {
continue;
}
switch ($error->keyword()) {
case 'required':
$errorLabel = $this->getTranslation(
'error.' . $field . '.required'
);
if ($errorLabel == null) {
$fieldLabel = $this->getTranslation(
'field.' . $field
);
$errorLabel = $this->getTranslation(
'error.required', [$fieldLabel]
);
}
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
case 'pattern':
$errorLabel = $this->getTranslation(
'error.' . $field . '.pattern'
);
if ($errorLabel == null) {
$fieldLabel = $this->getTranslation(
'field.' . $field
);
$errorLabel = $this->getTranslation(
'error.pattern', [$fieldLabel]
);
}
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
case 'format':
$errorLabel = $this->getTranslation(
'error.' . $field . '.format'
);
if ($errorLabel == null) {
$fieldLabel = $this->getTranslation(
'field.' . $field
);
$errorLabel = $this->getTranslation(
'error.format', [$fieldLabel]
);
}
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
default:
$errorLabel = $this->getTranslation(
'error.' . $field . '.' . $error->keyword()
);
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
break;
}
}
}
}