[TASK] Move array functions to utility class
This commit is contained in:
parent
329fa6d313
commit
221eda9f44
@ -15,6 +15,7 @@ namespace Cjel\TemplatesAide\Traits;
|
|||||||
use \Opis\JsonSchema\{
|
use \Opis\JsonSchema\{
|
||||||
Validator, ValidationResult, ValidationError, Schema
|
Validator, ValidationResult, ValidationError, Schema
|
||||||
};
|
};
|
||||||
|
use Cjel\TemplatesAide\Utility\ArrayUtility;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ValidationTrait
|
* ValidationTrait
|
||||||
@ -47,7 +48,7 @@ trait ValidationTrait
|
|||||||
protected function validateAgainstSchema($input, $schema)
|
protected function validateAgainstSchema($input, $schema)
|
||||||
{
|
{
|
||||||
$validator = new Validator();
|
$validator = new Validator();
|
||||||
$input = $this->arrayRemoveEmptyStrings($input);
|
$input = ArrayUtility::removeEmptyStrings($input);
|
||||||
//@TODO make optional when usiing rest api
|
//@TODO make optional when usiing rest api
|
||||||
//array_walk_recursive(
|
//array_walk_recursive(
|
||||||
// $input,
|
// $input,
|
||||||
@ -57,7 +58,7 @@ trait ValidationTrait
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
//);
|
//);
|
||||||
$input = $this->arrayToObject($input);
|
$input = ArrayUtility::toObject($input);
|
||||||
$validationResult = $validator->dataValidation(
|
$validationResult = $validator->dataValidation(
|
||||||
$input,
|
$input,
|
||||||
json_encode($schema),
|
json_encode($schema),
|
||||||
@ -90,33 +91,4 @@ trait ValidationTrait
|
|||||||
return $validationResult;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
49
Classes/Utility/ArrayUtility.php
Normal file
49
Classes/Utility/ArrayUtility.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
namespace Cjel\TemplatesAide\Utility;
|
||||||
|
|
||||||
|
/***
|
||||||
|
*
|
||||||
|
* This file is part of the "" 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
|
||||||
|
*
|
||||||
|
***/
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class ArrayUtility
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* function arrayTobject
|
||||||
|
*/
|
||||||
|
public static function toObject($array) {
|
||||||
|
if (is_array($array)) {
|
||||||
|
return (object) array_map([__CLASS__, __METHOD__], $array);
|
||||||
|
} else {
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* remove empty strings
|
||||||
|
*/
|
||||||
|
public static function removeEmptyStrings($array)
|
||||||
|
{
|
||||||
|
foreach ($array as $key => &$value) {
|
||||||
|
if (is_array($value)) {
|
||||||
|
$value = [__CLASS__, __METHOD__]($value);
|
||||||
|
} else {
|
||||||
|
if (is_string($value) && !strlen($value)) {
|
||||||
|
unset($array[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unset($value);
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user