[FEATURE] Backport function to convert form data input by schema
This commit is contained in:
parent
cf387b1af7
commit
9858f663f6
@ -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
|
||||
*
|
||||
|
@ -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": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user