Initial xentral_oss_20.3.c9ffacf

This commit is contained in:
Alex
2021-05-21 08:49:41 +02:00
parent 5406e4a551
commit 34e5ac43d9
18884 changed files with 2109867 additions and 0 deletions
@@ -0,0 +1,12 @@
<?php
namespace Xentral\Modules\SystemTemplates\Validator\Exception;
use RuntimeException;
class SystemTemplateValidatorException extends RuntimeException implements SystemTemplateValidatorExceptionInterface
{
}
@@ -0,0 +1,8 @@
<?php
namespace Xentral\Modules\SystemTemplates\Validator\Exception;
interface SystemTemplateValidatorExceptionInterface
{
}
@@ -0,0 +1,188 @@
<?php
namespace Xentral\Modules\SystemTemplates\Validator;
use Xentral\Modules\SystemTemplates\Validator\Exception\SystemTemplateValidatorException;
final class MetaDataValidation implements SystemTemplateValidatorInterface
{
/** @var array $data */
private $data = [];
/** @var array $errors */
private $errors = [];
/** @var string $templePath */
private $templePath;
/** @var array $mandatoryFields */
private $mandatoryFields = [];
/** @var bool $autoRulesCheck */
private $autoRulesCheck;
/** @var Ruleset $ruleSet */
private $ruleSet;
/**
* @return array
*/
public function validateDefault()
{
return [
'title' => [
'rule' => static function ($data) {
return !empty($data['title']) && is_string($data['title']);
},
'required' => true,
'message' => sprintf('%s should be a non empty String', 'Title'),
],
'description' => [
'rule' => static function ($data) {
return !empty($data['description']) && is_string($data['description']);
},
'required' => true,
'message' => sprintf('%s should be a non empty String', 'Description'),
],
'category' => [
'rule' => static function ($data) {
return !empty($data['category']) && is_string($data['category']);
},
'required' => true,
'message' => sprintf('%s should be a non empty String', 'Category'),
],
'filename' => [
'rule' => ['isFileName'],
'required' => true,
'message' => 'File name cannot be blank or non string',
],
];
}
/**
* MetaDataValidation constructor.
*
* @param Ruleset $ruleset
* @param $templatePath
* @param bool $autoRulesCheck
*/
public function __construct(Ruleset $ruleset, $templatePath, $autoRulesCheck = true)
{
$this->templePath = $templatePath;
$this->ruleSet = $ruleset;
$this->autoRulesCheck = $autoRulesCheck;
}
/**
* @param string $error
*/
public function addError($error)
{
$this->errors[] = $error;
}
/**
* @param string $fieldName
*
* @return void
*/
public function setMandatoryFieldName($fieldName)
{
if (!empty($fieldName)) {
$this->mandatoryFields = array_merge($this->mandatoryFields, [$fieldName]);
}
}
/**
* @param string|null $validateConfig
*
* @return bool
*/
public function isValid($validateConfig = null)
{
try {
$this->checkMandatory();
} catch (SystemTemplateValidatorException $exception) {
$this->addError($exception->getMessage());
return false;
}
if ($this->autoRulesCheck === false) {
$this->applyRules($validateConfig);
}
return empty($this->errors);
}
/**
* @param string $configMethod
*
* @return void
*/
public function applyRules($configMethod = null)
{
$this->ruleSet->setRules($this, $configMethod);
}
/**
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* @return void
*/
private function checkMandatory()
{
$missing = array_diff($this->mandatoryFields, array_keys($this->data));
if (!empty($missing)) {
throw new SystemTemplateValidatorException(
sprintf('Missing mandatory parameter "%s', json_encode(array_unique($missing)))
);
}
}
/**
* @param array $data
*/
public function setData($data = [])
{
$this->data = $data;
}
public function getData()
{
return $this->data;
}
/**
* @param string|null $name
*
* @return bool
*/
public function isFileName($name = null)
{
$fileName = null === $name && !empty($this->data['filename']) ? $this->data['filename'] : $name;
if (empty($fileName) || !is_string($fileName)) {
$this->addError('File name cannot be blank or non string');
return false;
}
if (!file_exists($this->templePath . $fileName)) {
$this->addError(sprintf('File "%s" cannot be found', $fileName));
return false;
}
return true;
}
}
@@ -0,0 +1,98 @@
<?php
namespace Xentral\Modules\SystemTemplates\Validator;
use Closure;
use Xentral\Modules\SystemTemplates\Validator\Exception\SystemTemplateValidatorException;
final class Ruleset
{
/** @var SystemTemplateValidatorInterface $validator */
private $validator;
/** @var string $configMethod */
private $configMethod;
/** @var string */
const DEFAULT_RULES_CONFIGURATOR = 'validateDefault';
/**
* @param string|null $configMethod
*/
public function __construct($configMethod = null)
{
if (null === $configMethod) {
$configMethod = static::DEFAULT_RULES_CONFIGURATOR;
}
$this->configMethod = $configMethod;
}
/**
* @param SystemTemplateValidatorInterface $validator
* @param string $configMethod
*/
public function setRules(SystemTemplateValidatorInterface $validator, $configMethod = null)
{
$this->validator = $validator;
$configMethod = $configMethod === null ? $this->configMethod : $configMethod;
if (!method_exists($this->validator, $configMethod)) {
throw new SystemTemplateValidatorException(
sprintf('Validate Config method %s is missing', $configMethod)
);
}
$ruleConfigs = $this->validator->{$configMethod}();
if (!empty($ruleConfigs)) {
foreach ($ruleConfigs as $fieldName => $config) {
if (!is_string($fieldName) || empty($fieldName)) {
throw new SystemTemplateValidatorException(
sprintf('Field name is missing in Configuration at index %s', $fieldName)
);
}
if (!array_key_exists('rule', $config) || empty($config['rule'])) {
throw new SystemTemplateValidatorException(
sprintf('Rule is missing in Configuration at index "%s', $fieldName)
);
}
$this->addRule($fieldName, $config);
}
}
}
/**
* @param string $fieldName
* @param array $config
*
* @return void
*/
private function addRule($fieldName, $config)
{
$callbackResponse = false;
$rule = $config['rule'];
$message = !empty($config['message']) ? $config['message'] : sprintf('%s is Invalid', $fieldName);
if (is_array($rule) && !method_exists($this->validator, $rule[0])) {
throw new SystemTemplateValidatorException(sprintf('Custom check method %s is missing !', $rule[0]));
}
if (array_key_exists('required', $config) && in_array($config['required'], [true, 1], true)) {
$this->validator->setMandatoryFieldName($fieldName);
}
if (is_array($rule)) {
$arg = empty($rule[1]) ? [] : [$rule[1]];
$callbackResponse = call_user_func_array([$this->validator, $rule[0]], $arg);
} elseif ($rule instanceof Closure) {
$callbackResponse = $rule($this->validator->getData());
}
if ($callbackResponse !== true) {
$this->validator->addError($message);
}
}
}
@@ -0,0 +1,58 @@
<?php
namespace Xentral\Modules\SystemTemplates\Validator;
use Xentral\Modules\SystemTemplates\Validator\Exception\SystemTemplateValidatorException;
final class SystemTemplateValidator
{
/** @var SystemTemplateValidatorInterface $validator */
private $validator;
/**
* SystemTemplateValidator constructor.
*
* @param SystemTemplateValidatorInterface $validator
*/
public function __construct(SystemTemplateValidatorInterface $validator)
{
$this->validator = $validator;
}
/**
* @param array $data
*
* @return SystemTemplateValidator
*/
public function fromMeta($data = [])
{
$this->validator->setData($data);
return $this;
}
/**
* @param string|null $validateConfig
*
* @throws SystemTemplateValidatorException
* @return bool
*/
public function isValid($validateConfig = null)
{
$this->validator->applyRules($validateConfig);
return $this->validator->isValid($validateConfig);
}
/**
* @return array
*/
public function getErrors()
{
return $this->validator->getErrors();
}
}
@@ -0,0 +1,24 @@
<?php
namespace Xentral\Modules\SystemTemplates\Validator;
interface SystemTemplateValidatorInterface
{
/** @param string|null $methodName */
public function isValid($methodName = null);
public function getErrors();
public function setData($data);
public function validateDefault();
public function setMandatoryFieldName($fieldName);
public function addError($error);
public function getData();
public function applyRules($ruleMethod = null);
}