[FEATURE] Add date parsing for schema based input conversion

This commit is contained in:
Philipp Dieter
2021-11-21 23:59:12 +01:00
parent c563dcf069
commit 8dbc5791d2
2 changed files with 63 additions and 15 deletions

View File

@@ -52,28 +52,33 @@ trait ValidationTrait
$flatten = new Flatten();
$schemaFlat = $flatten->flattenToArray($schema);
$typesList = [];
$formatsList = [];
foreach ($schemaFlat as $index => $row) {
$dataIndex = preg_replace(
'/(\.)(properties\.|items\.)/',
'$1',
$index
);
$dataIndex = preg_replace(
'/^properties\./',
'',
$dataIndex
);
$dataIndex = preg_replace(
'/\.(type|format)$/',
'',
$dataIndex
);
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;
}
if (substr($index, -7) == '.format') {
$formatsList[$dataIndex] = $row;
}
}
foreach ($input as $index => $row) {
$rowType = $typesList[$index];
$formatType = $formatsList[$index];
if (!$rowType) {
continue;
}
@@ -92,6 +97,16 @@ trait ValidationTrait
if ($testResult === true || $testResult === false) {
$input[$index] = $testResult;
}
case 'string':
switch ($formatType) {
case 'date':
$row = \DateTime::createFromFormat(
'Y-m-d H:i:s',
$row . ' 00:00:00',
);
break;
}
break;
}
}
return $input;