Commit 76358dd4 authored by Tino Goratsch's avatar Tino Goratsch

simplified the usage and creation of the validation rules

parent 106ffbe6
......@@ -9,21 +9,11 @@ use ACP3\Core\Validation\Validator;
*/
abstract class AbstractValidationRule implements ValidationRuleInterface
{
const NAME = '';
/**
* @var string
*/
protected $message = '';
/**
* @inheritdoc
*/
public function getName()
{
return static::NAME;
}
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class BirthdayValidationRule extends AbstractValidationRule
{
const NAME = 'birthday';
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class ChangePasswordValidationRule extends PasswordValidationRule
{
const NAME = 'change_password';
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class DateValidationRule extends AbstractValidationRule
{
const NAME = 'date';
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class EmailValidationRule extends AbstractValidationRule
{
const NAME = 'email';
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class FileUploadValidationRule extends AbstractValidationRule
{
const NAME = 'file_upload';
/**
* @inheritdoc
*/
......
......@@ -12,8 +12,6 @@ use ACP3\Core\Validation\Validator;
*/
class FormTokenValidationRule extends AbstractValidationRule
{
const NAME = 'form_token';
/**
* @var \ACP3\Core\Http\RequestInterface
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class InArrayValidationRule extends AbstractValidationRule
{
const NAME = 'in_array';
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class IntegerValidationRule extends AbstractValidationRule
{
const NAME = 'integer';
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class InternalUriValidationRule extends AbstractValidationRule
{
const NAME = 'internal_uri';
/**
* @inheritdoc
*/
......
......@@ -9,8 +9,6 @@ use ACP3\Core\I18n\Translator;
*/
class LanguagePackExistsValidationRule extends AbstractValidationRule
{
const NAME = 'language_pack_exists';
/**
* @var \ACP3\Core\I18n\Translator
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class MinLengthValidationRule extends AbstractValidationRule
{
const NAME = 'min_length';
/**
* @inheritdoc
*/
......
......@@ -8,8 +8,6 @@ use ACP3\Core\Modules;
*/
class ModuleIsInstalledValidationRule extends AbstractValidationRule
{
const NAME = 'module_is_installed';
/**
* @var \ACP3\Core\Modules
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class NotEmptyValidationRule extends AbstractValidationRule
{
const NAME = 'not_empty';
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class NumberGreaterThanValidationRule extends AbstractValidationRule
{
const NAME = 'number_greater_than';
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class PasswordValidationRule extends AbstractValidationRule
{
const NAME = 'password';
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class PictureValidationRule extends AbstractValidationRule
{
const NAME = 'picture';
/**
* @var \ACP3\Core\Validation\ValidationRules\FileUploadValidationRule
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class TimeZoneExistsValidationRule extends AbstractValidationRule
{
const NAME = 'time_zone_exists';
/**
* @inheritdoc
*/
......
......@@ -7,8 +7,6 @@ namespace ACP3\Core\Validation\ValidationRules;
*/
class UriSafeValidationRule extends AbstractValidationRule
{
const NAME = 'uri_safe';
/**
* @inheritdoc
*/
......
......@@ -9,11 +9,6 @@ use ACP3\Core\Validation\Validator;
*/
interface ValidationRuleInterface
{
/**
* @return string
*/
public function getName();
/**
* @return string
*/
......
......@@ -31,7 +31,7 @@ class Validator
*/
public function registerValidationRule(ValidationRuleInterface $validationRule)
{
$this->validationRules[$validationRule->getName()] = $validationRule;
$this->validationRules[get_class($validationRule)] = $validationRule;
return $this;
}
......@@ -50,15 +50,15 @@ class Validator
}
/**
* @param string $validationRuleName
* @param string $validationRule
* @param array $params
*
* @return $this
*/
public function addConstraint($validationRuleName, array $params = [])
public function addConstraint($validationRule, array $params = [])
{
$this->constraints[] = [
'rule' => $validationRuleName,
'rule' => $validationRule,
'params' => array_merge($this->getDefaultConstraintParams(), $params)
];
......@@ -127,19 +127,19 @@ class Validator
}
/**
* @param string $validationRuleName
* @param string $validationRule
* @param mixed $field
*
* @return bool
* @throws \ACP3\Core\Validation\Exceptions\ValidationRuleNotFoundException
*/
public function is($validationRuleName, $field)
public function is($validationRule, $field)
{
if (isset($this->validationRules[$validationRuleName])) {
return $this->validationRules[$validationRuleName]->isValid($field);
if (isset($this->validationRules[$validationRule])) {
return $this->validationRules[$validationRule]->isValid($field);
}
throw new ValidationRuleNotFoundException('Can not find the validation rule with the name ' . $validationRuleName . '.');
throw new ValidationRuleNotFoundException('Can not find the validation rule with the name ' . $validationRule . '.');
}
/**
......
......@@ -55,16 +55,16 @@ class AdminFormValidation extends Core\Validation\AbstractFormValidation
public function validate(array $formData)
{
$this->validator
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::class)
->addConstraint(
Core\Validation\ValidationRules\DateValidationRule::NAME,
Core\Validation\ValidationRules\DateValidationRule::class,
[
'data' => $formData,
'field' => ['start', 'end'],
'message' => $this->translator->t('system', 'select_date')
])
->addConstraint(
Core\Validation\ValidationRules\MinLengthValidationRule::NAME,
Core\Validation\ValidationRules\MinLengthValidationRule::class,
[
'data' => $formData,
'field' => 'title',
......@@ -74,7 +74,7 @@ class AdminFormValidation extends Core\Validation\AbstractFormValidation
]
])
->addConstraint(
Core\Validation\ValidationRules\MinLengthValidationRule::NAME,
Core\Validation\ValidationRules\MinLengthValidationRule::class,
[
'data' => $formData,
'field' => 'text',
......@@ -84,7 +84,7 @@ class AdminFormValidation extends Core\Validation\AbstractFormValidation
]
])
->addConstraint(
UriAliasValidationRule::NAME,
UriAliasValidationRule::class,
[
'data' => $formData,
'field' => 'alias',
......@@ -107,21 +107,21 @@ class AdminFormValidation extends Core\Validation\AbstractFormValidation
{
$this->validator
->addConstraint(
Core\Validation\ValidationRules\IntegerValidationRule::NAME,
Core\Validation\ValidationRules\IntegerValidationRule::class,
[
'data' => $formData,
'field' => 'block_id',
'message' => $this->translator->t('menus', 'select_menu_bar')
])
->addConstraint(
Menus\Validation\ValidationRules\ParentIdValidationRule::NAME,
Menus\Validation\ValidationRules\ParentIdValidationRule::class,
[
'data' => $formData,
'field' => 'parent_id',
'message' => $this->translator->t('menus', 'select_superior_page')
])
->addConstraint(
Core\Validation\ValidationRules\InArrayValidationRule::NAME,
Core\Validation\ValidationRules\InArrayValidationRule::class,
[
'data' => $formData,
'field' => 'display',
......@@ -131,7 +131,7 @@ class AdminFormValidation extends Core\Validation\AbstractFormValidation
]
])
->addConstraint(
Menus\Validation\ValidationRules\AllowedMenuValidationRule::NAME,
Menus\Validation\ValidationRules\AllowedMenuValidationRule::class,
[
'data' => $formData,
'field' => ['parent_id', 'block_id'],
......
......@@ -10,8 +10,6 @@ use ACP3\Modules\ACP3\Articles\Model\ArticleRepository;
*/
class ArticleExistsValidationRule extends AbstractValidationRule
{
const NAME = 'articles_article_exists';
/**
* @var \ACP3\Modules\ACP3\Articles\Model\ArticleRepository
*/
......
......@@ -14,8 +14,6 @@ use ACP3\Core\Validation\ValidationRules\AbstractValidationRule;
*/
class CaptchaValidationRule extends AbstractValidationRule
{
const NAME = 'captcha';
/**
* @var \ACP3\Core\ACL
*/
......
......@@ -84,23 +84,23 @@ class AdminFormValidation extends Core\Validation\AbstractFormValidation
public function validate(array $formData)
{
$this->validator
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::class)
->addConstraint(
Core\Validation\ValidationRules\NotEmptyValidationRule::NAME,
Core\Validation\ValidationRules\NotEmptyValidationRule::class,
[
'data' => $formData,
'field' => 'title',
'message' => $this->translator->t('categories', 'title_to_short')
])
->addConstraint(
Core\Validation\ValidationRules\NotEmptyValidationRule::NAME,
Core\Validation\ValidationRules\NotEmptyValidationRule::class,
[
'data' => $formData,
'field' => 'description',
'message' => $this->translator->t('categories', 'description_to_short')
])
->addConstraint(
Core\Validation\ValidationRules\PictureValidationRule::NAME,
Core\Validation\ValidationRules\PictureValidationRule::class,
[
'data' => $this->file,
'field' => 'picture',
......@@ -113,7 +113,7 @@ class AdminFormValidation extends Core\Validation\AbstractFormValidation
]
])
->addConstraint(
DuplicateCategoryValidationRule::NAME,
DuplicateCategoryValidationRule::class,
[
'data' => $formData,
'field' => 'title',
......@@ -126,7 +126,7 @@ class AdminFormValidation extends Core\Validation\AbstractFormValidation
if (empty($categoryId)) {
$this->validator->addConstraint(
Core\Validation\ValidationRules\NotEmptyValidationRule::NAME,
Core\Validation\ValidationRules\NotEmptyValidationRule::class,
[
'data' => $formData,
'field' => 'module',
......
......@@ -17,23 +17,23 @@ class AdminSettingsFormValidation extends AbstractFormValidation
public function validate(array $formData)
{
$this->validator
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::class)
->addConstraint(
Core\Validation\ValidationRules\IntegerValidationRule::NAME,
Core\Validation\ValidationRules\IntegerValidationRule::class,
[
'data' => $formData,
'field' => 'width',
'message' => $this->translator->t('categories', 'invalid_image_width_entered')
])
->addConstraint(
Core\Validation\ValidationRules\IntegerValidationRule::NAME,
Core\Validation\ValidationRules\IntegerValidationRule::class,
[
'data' => $formData,
'field' => 'height',
'message' => $this->translator->t('categories', 'invalid_image_height_entered')
])
->addConstraint(
Core\Validation\ValidationRules\IntegerValidationRule::NAME,
Core\Validation\ValidationRules\IntegerValidationRule::class,
[
'data' => $formData,
'field' => 'filesize',
......
......@@ -10,8 +10,6 @@ use ACP3\Modules\ACP3\Categories\Helpers;
*/
class CategoryExistsValidationRule extends AbstractValidationRule
{
const NAME = 'categories_category_exists';
/**
* @var \ACP3\Modules\ACP3\Categories\Helpers
*/
......
......@@ -10,8 +10,6 @@ use ACP3\Modules\ACP3\Categories\Helpers;
*/
class DuplicateCategoryValidationRule extends AbstractValidationRule
{
const NAME = 'categories_duplicate_category';
/**
* @var \ACP3\Modules\ACP3\Categories\Helpers
*/
......
......@@ -225,7 +225,7 @@ class Details extends Core\Modules\AdminController
$updateValues = [];
$updateValues['message'] = Core\Functions::strEncode($formData['message']);
if ((empty($comment['user_id']) || $this->validator->is(Core\Validation\ValidationRules\IntegerValidationRule::NAME, $comment['user_id']) === false) &&
if ((empty($comment['user_id']) || $this->validator->is(Core\Validation\ValidationRules\IntegerValidationRule::class, $comment['user_id']) === false) &&
!empty($formData['name'])
) {
$updateValues['name'] = Core\Functions::strEncode($formData['name']);
......
......@@ -17,16 +17,16 @@ class AdminFormValidation extends AbstractFormValidation
public function validate(array $formData)
{
$this->validator
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::class)
->addConstraint(
UserNameValidationRule::NAME,
UserNameValidationRule::class,
[
'data' => $formData,
'field' => ['name', 'user_id'],
'message' => $this->translator->t('system', 'name_to_short')
])
->addConstraint(
Core\Validation\ValidationRules\NotEmptyValidationRule::NAME,
Core\Validation\ValidationRules\NotEmptyValidationRule::class,
[
'data' => $formData,
'field' => 'message',
......
......@@ -38,9 +38,9 @@ class AdminSettingsFormValidation extends AbstractFormValidation
public function validate(array $formData)
{
$this->validator
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::class)
->addConstraint(
Core\Validation\ValidationRules\InArrayValidationRule::NAME,
Core\Validation\ValidationRules\InArrayValidationRule::class,
[
'data' => $formData,
'field' => 'dateformat',
......@@ -53,7 +53,7 @@ class AdminSettingsFormValidation extends AbstractFormValidation
if ($this->modules->isActive('emoticons')) {
$this->validator
->addConstraint(
Core\Validation\ValidationRules\InArrayValidationRule::NAME,
Core\Validation\ValidationRules\InArrayValidationRule::class,
[
'data' => $formData,
'field' => 'emoticons',
......
......@@ -31,9 +31,9 @@ class FormValidation extends Core\Validation\AbstractFormValidation
public function validate(array $formData)
{
$this->validator
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::class)
->addConstraint(
FloodBarrierValidationRule::NAME,
FloodBarrierValidationRule::class,
[
'message' => $this->translator->t('system', 'flood_no_entry_possible'),
'extra' => [
......@@ -41,21 +41,21 @@ class FormValidation extends Core\Validation\AbstractFormValidation
]
])
->addConstraint(
Core\Validation\ValidationRules\NotEmptyValidationRule::NAME,
Core\Validation\ValidationRules\NotEmptyValidationRule::class,
[
'data' => $formData,
'field' => 'name',
'message' => $this->translator->t('system', 'name_to_short')
])
->addConstraint(
Core\Validation\ValidationRules\NotEmptyValidationRule::NAME,
Core\Validation\ValidationRules\NotEmptyValidationRule::class,
[
'data' => $formData,
'field' => 'message',
'message' => $this->translator->t('system', 'message_to_short')
])
->addConstraint(
CaptchaValidationRule::NAME,
CaptchaValidationRule::class,
[
'data' => $formData,
'field' => 'captcha',
......
......@@ -10,5 +10,4 @@ use ACP3\Core\Validation\ValidationRules\AbstractFloodBarrierValidationRule;
*/
class FloodBarrierValidationRule extends AbstractFloodBarrierValidationRule
{
const NAME = 'comments_flood_barrier';
}
\ No newline at end of file
......@@ -10,8 +10,6 @@ use ACP3\Core\Validation\ValidationRules\IntegerValidationRule;
*/
class UserNameValidationRule extends AbstractValidationRule
{
const NAME = 'comments_user_name';
/**
* @var \ACP3\Core\Validation\ValidationRules\IntegerValidationRule
*/
......
......@@ -16,9 +16,9 @@ class AdminSettingsFormValidation extends AbstractFormValidation
public function validate(array $formData)
{
$this->validator
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::class)
->addConstraint(
Core\Validation\ValidationRules\EmailValidationRule::NAME,
Core\Validation\ValidationRules\EmailValidationRule::class,
[
'data' => $formData,
'field' => 'mail',
......
......@@ -17,30 +17,30 @@ class FormValidation extends Core\Validation\AbstractFormValidation
public function validate(array $formData)
{
$this->validator
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::class)
->addConstraint(
Core\Validation\ValidationRules\NotEmptyValidationRule::NAME,
Core\Validation\ValidationRules\NotEmptyValidationRule::class,
[
'data' => $formData,
'field' => 'name',
'message' => $this->translator->t('system', 'name_to_short')
])
->addConstraint(
Core\Validation\ValidationRules\EmailValidationRule::NAME,
Core\Validation\ValidationRules\EmailValidationRule::class,
[
'data' => $formData,
'field' => 'mail',
'message' => $this->translator->t('system', 'wrong_email_format')
])
->addConstraint(
Core\Validation\ValidationRules\NotEmptyValidationRule::NAME,
Core\Validation\ValidationRules\NotEmptyValidationRule::class,
[
'data' => $formData,
'field' => 'message',
'message' => $this->translator->t('system', 'message_to_short')
])
->addConstraint(
CaptchaValidationRule::NAME,
CaptchaValidationRule::class,
[
'data' => $formData,
'field' => 'captcha',
......
......@@ -64,23 +64,23 @@ class AdminFormValidation extends Core\Validation\AbstractFormValidation
public function validate(array $formData)
{
$this->validator
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::class)
->addConstraint(
Core\Validation\ValidationRules\NotEmptyValidationRule::NAME,
Core\Validation\ValidationRules\NotEmptyValidationRule::class,
[
'data' => $formData,
'field' => 'code',
'message' => $this->translator->t('emoticons', 'type_in_code')
])
->addConstraint(
Core\Validation\ValidationRules\NotEmptyValidationRule::NAME,
Core\Validation\ValidationRules\NotEmptyValidationRule::class,
[
'data' => $formData,
'field' => 'description',
'message' => $this->translator->t('emoticons', 'type_in_description')
])
->addConstraint(
Core\Validation\ValidationRules\PictureValidationRule::NAME,
Core\Validation\ValidationRules\PictureValidationRule::class,
[
'data' => $this->file,
'field' => 'picture',
......
......@@ -16,23 +16,23 @@ class AdminSettingsFormValidation extends AbstractFormValidation
public function validate(array $formData)
{
$this->validator
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(Core\Validation\ValidationRules\FormTokenValidationRule::class)
->addConstraint(
Core\Validation\ValidationRules\IntegerValidationRule::NAME,
Core\Validation\ValidationRules\IntegerValidationRule::class,
[
'data' => $formData,
'field' => 'width',
'message' => $this->translator->t('emoticons', 'invalid_image_width_entered')
])
->addConstraint(
Core\Validation\ValidationRules\IntegerValidationRule::NAME,
Core\Validation\ValidationRules\IntegerValidationRule::class,
[
'data' => $formData,
'field' => 'height',
'message' => $this->translator->t('emoticons', 'invalid_image_height_entered')
])
->addConstraint(
Core\Validation\ValidationRules\IntegerValidationRule::NAME,
Core\Validation\ValidationRules\IntegerValidationRule::class,
[
'data' => $formData,
'field' => 'filesize',
......