[TASK] Sepeate validation from ActionController
This commit is contained in:
parent
a3cb4325bf
commit
329fa6d313
@ -12,9 +12,7 @@ namespace Cjel\TemplatesAide\Controller;
|
|||||||
*
|
*
|
||||||
***/
|
***/
|
||||||
|
|
||||||
use \Opis\JsonSchema\{
|
use Cjel\TemplatesAide\Traits\ValidationTrait;
|
||||||
Validator, ValidationResult, ValidationError, Schema
|
|
||||||
};
|
|
||||||
use TYPO3\CMS\Core\Cache\CacheManager;
|
use TYPO3\CMS\Core\Cache\CacheManager;
|
||||||
use TYPO3\CMS\Core\Log\LogManager;
|
use TYPO3\CMS\Core\Log\LogManager;
|
||||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||||
@ -30,6 +28,10 @@ use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
|
|||||||
class ActionController extends BaseController
|
class ActionController extends BaseController
|
||||||
{
|
{
|
||||||
|
|
||||||
|
use ValidationTrait {
|
||||||
|
validateAgainstSchema as traitValidateAgainstSchema;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* page type
|
* page type
|
||||||
*/
|
*/
|
||||||
@ -96,11 +98,6 @@ class ActionController extends BaseController
|
|||||||
*/
|
*/
|
||||||
protected $redirect = null;
|
protected $redirect = null;
|
||||||
|
|
||||||
/**
|
|
||||||
* is valid
|
|
||||||
*/
|
|
||||||
protected $isValid = true;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* errors
|
* errors
|
||||||
*/
|
*/
|
||||||
@ -390,58 +387,56 @@ class ActionController extends BaseController
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function arrayRemoveEmptyStrings($array)
|
|
||||||
{
|
|
||||||
foreach ($array as $key => &$value) {
|
|
||||||
if (is_array($value)) {
|
|
||||||
$value = $this->arrayRemoveEmptyStrings($value);
|
|
||||||
} else {
|
|
||||||
if (is_string($value) && !strlen($value)) {
|
|
||||||
unset($array[$key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unset($value);
|
|
||||||
return $array;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function arrayToObject($array) {
|
|
||||||
if (is_array($array)) {
|
|
||||||
return (object) array_map([__CLASS__, __METHOD__], $array);
|
|
||||||
} else {
|
|
||||||
return $array;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* validate objects
|
* legacy function to prevent beaking old code
|
||||||
*
|
*
|
||||||
* @param $input
|
* @deprecated
|
||||||
* @param schema
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
protected function validateInput($input, $schema)
|
protected function validateInput($input, $schema)
|
||||||
{
|
{
|
||||||
$validator = new Validator();
|
return $this->validateAgainstSchema($input, $schema, true);
|
||||||
$input = $this->arrayRemoveEmptyStrings($input);
|
}
|
||||||
//@todo make optional when usiing rest api
|
|
||||||
//array_walk_recursive(
|
/**
|
||||||
// $input,
|
* validate input and translate error messages
|
||||||
// function (&$value) {
|
*/
|
||||||
// if (filter_var($value, FILTER_VALIDATE_INT)) {
|
protected function validateAgainstSchema(
|
||||||
// $value = (int)$value;
|
$input, $schema, $translate = false
|
||||||
// }
|
) {
|
||||||
// }
|
$validationResult = $this->traitValidateAgainstSchema(
|
||||||
//);
|
|
||||||
$input = $this->arrayToObject($input);
|
|
||||||
$validationResult = $validator->dataValidation(
|
|
||||||
$input,
|
$input,
|
||||||
json_encode($schema),
|
$schema
|
||||||
-1
|
|
||||||
);
|
);
|
||||||
if (!$validationResult->isValid()) {
|
if (!$validationResult->isValid()) {
|
||||||
$this->isValid = false;
|
//foreach ($validationResult->getErrors() as $error){
|
||||||
$this->responseStatus = [400 => 'validationError'];
|
// $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;
|
||||||
|
// }
|
||||||
|
// $this->errors[$field] = [
|
||||||
|
// 'keyword' => $error->keyword(),
|
||||||
|
// 'details' => $error->keywordArgs()
|
||||||
|
// ];
|
||||||
|
//}
|
||||||
|
if ($translate) {
|
||||||
|
$this->translateErrorMessages($validationResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $validationResult->isValid();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* translate error messages to user readable strings
|
||||||
|
*/
|
||||||
|
protected function translateErrorMessages($validationResult)
|
||||||
|
{
|
||||||
foreach ($validationResult->getErrors() as $error){
|
foreach ($validationResult->getErrors() as $error){
|
||||||
$errorLabel = null;
|
$errorLabel = null;
|
||||||
$field = implode('.', $error->dataPointer());
|
$field = implode('.', $error->dataPointer());
|
||||||
@ -453,19 +448,10 @@ class ActionController extends BaseController
|
|||||||
if ($error->keyword() == 'additionalProperties') {
|
if ($error->keyword() == 'additionalProperties') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$this->errors[$field] = [
|
|
||||||
'keyword' => $error->keyword(),
|
|
||||||
'details' => $error->keywordArgs()
|
|
||||||
];
|
|
||||||
if ($error->keyword() != 'required') {
|
if ($error->keyword() != 'required') {
|
||||||
$errorLabel = $this->getTranslation(
|
$errorLabel = $this->getTranslation(
|
||||||
'error.' . $field . '.' . $error->keyword()
|
'error.' . $field . '.' . $error->keyword()
|
||||||
);
|
);
|
||||||
//if ($errorLabel == null) {
|
|
||||||
// $errorLabel = $this->getTranslation(
|
|
||||||
// 'error.' . $field . '.required'
|
|
||||||
// );
|
|
||||||
//}
|
|
||||||
if ($errorLabel == null) {
|
if ($errorLabel == null) {
|
||||||
$errorLabel = 'error.'
|
$errorLabel = 'error.'
|
||||||
. $field
|
. $field
|
||||||
@ -487,8 +473,6 @@ class ActionController extends BaseController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $validationResult->isValid();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns plugin namespace to build js post request
|
* returns plugin namespace to build js post request
|
||||||
|
122
Classes/Traits/ValidationTrait.php
Normal file
122
Classes/Traits/ValidationTrait.php
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
<?php
|
||||||
|
namespace Cjel\TemplatesAide\Traits;
|
||||||
|
|
||||||
|
/***
|
||||||
|
*
|
||||||
|
* This file is part of the "Templates Aide" Extension for TYPO3 CMS.
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please read the
|
||||||
|
* LICENSE.txt file that was distributed with this source code.
|
||||||
|
*
|
||||||
|
* (c) 2021 Philipp Dieter <philippdieter@attic-media.net>
|
||||||
|
*
|
||||||
|
***/
|
||||||
|
|
||||||
|
use \Opis\JsonSchema\{
|
||||||
|
Validator, ValidationResult, ValidationError, Schema
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ValidationTrait
|
||||||
|
*/
|
||||||
|
trait ValidationTrait
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* is valid
|
||||||
|
*/
|
||||||
|
protected $isValid = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* errors
|
||||||
|
*/
|
||||||
|
protected $errors = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* errors labels
|
||||||
|
*/
|
||||||
|
protected $errorLabels = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* validate objects
|
||||||
|
*
|
||||||
|
* @param $input
|
||||||
|
* @param schema
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function validateAgainstSchema($input, $schema)
|
||||||
|
{
|
||||||
|
$validator = new Validator();
|
||||||
|
$input = $this->arrayRemoveEmptyStrings($input);
|
||||||
|
//@TODO make optional when usiing rest api
|
||||||
|
//array_walk_recursive(
|
||||||
|
// $input,
|
||||||
|
// function (&$value) {
|
||||||
|
// if (filter_var($value, FILTER_VALIDATE_INT)) {
|
||||||
|
// $value = (int)$value;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//);
|
||||||
|
$input = $this->arrayToObject($input);
|
||||||
|
$validationResult = $validator->dataValidation(
|
||||||
|
$input,
|
||||||
|
json_encode($schema),
|
||||||
|
-1
|
||||||
|
);
|
||||||
|
if (!$validationResult->isValid()) {
|
||||||
|
$this->isValid = false;
|
||||||
|
$this->responseStatus = [400 => 'validationError'];
|
||||||
|
foreach ($validationResult->getErrors() as $error){
|
||||||
|
$field = implode('.', $error->dataPointer());
|
||||||
|
if ($error->keyword() == 'required') {
|
||||||
|
$tmp = $error->dataPointer();
|
||||||
|
array_push($tmp, $error->keywordArgs()['missing']);
|
||||||
|
$field = implode('.', $tmp);
|
||||||
|
}
|
||||||
|
if ($error->keyword() == 'additionalProperties') {
|
||||||
|
foreach ($error->subErrors() as $subError) {
|
||||||
|
$this->errors[$subError->dataPointer()[0]] = [
|
||||||
|
'keyword' => 'superfluos',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->errors[$field] = [
|
||||||
|
'keyword' => $error->keyword(),
|
||||||
|
'details' => $error->keywordArgs()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $validationResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* remove empty strings
|
||||||
|
*/
|
||||||
|
public function arrayRemoveEmptyStrings($array)
|
||||||
|
{
|
||||||
|
foreach ($array as $key => &$value) {
|
||||||
|
if (is_array($value)) {
|
||||||
|
$value = $this->arrayRemoveEmptyStrings($value);
|
||||||
|
} else {
|
||||||
|
if (is_string($value) && !strlen($value)) {
|
||||||
|
unset($array[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unset($value);
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* function arrayTObject
|
||||||
|
*/
|
||||||
|
public static function arrayToObject($array) {
|
||||||
|
if (is_array($array)) {
|
||||||
|
return (object) array_map([__CLASS__, __METHOD__], $array);
|
||||||
|
} else {
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user