[FEATURE] Backport function to convert form data input by schema

This commit is contained in:
Philipp Dieter 2021-11-21 23:09:16 +01:00
parent cf387b1af7
commit 9858f663f6
2 changed files with 60 additions and 1 deletions

View File

@ -16,6 +16,7 @@ use \Opis\JsonSchema\{
Validator, ValidationResult, ValidationError, Schema
};
use Cjel\TemplatesAide\Utility\ArrayUtility;
use Sarhan\Flatten\Flatten;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
@ -39,6 +40,63 @@ trait ValidationTrait
*/
protected $errorLabels = [];
/**
* validate objects
*
* @param $input
* @param schema
* @return void
*/
protected function convertInputBySchema($input, $schema)
{
$flatten = new Flatten();
$schemaFlat = $flatten->flattenToArray($schema);
$typesList = [];
foreach ($schemaFlat as $index => $row) {
if (substr($index, -5) == '.type') {
$dataIndex = preg_replace(
'/(\.)(properties\.|items\.)/',
'$1',
$index
);
$dataIndex = preg_replace(
'/^properties\./',
'',
$dataIndex
);
$dataIndex = preg_replace(
'/\.type$/',
'',
$dataIndex
);
$typesList[$dataIndex] = $row;
}
}
foreach ($input as $index => $row) {
$rowType = $typesList[$index];
if (!$rowType) {
continue;
}
switch ($rowType) {
case 'integer':
if (is_numeric($row)) {
settype($input[$index], $rowType);
}
break;
case 'boolean':
$testResult = filter_var(
$row,
FILTER_VALIDATE_BOOLEAN,
[FILTER_NULL_ON_FAILURE]
);
if ($testResult === true || $testResult === false) {
$input[$index] = $testResult;
}
}
}
return $input;
}
/**
* validate objects
*

View File

@ -9,7 +9,8 @@
}
],
"require": {
"typo3/cms-core": "8.7.0 - 9.5.99"
"typo3/cms-core": "8.7.0 - 9.5.99",
"sarhan/php-flatten": "^4.0"
},
"autoload": {
"psr-4": {