UserValidator::setValidationData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Platine\App\Validator;
6
7
use Platine\App\Param\UserParam;
8
use Platine\Framework\Auth\Enum\UserStatus;
9
use Platine\Framework\Form\Validator\AbstractValidator;
10
use Platine\Lang\Lang;
11
use Platine\Validator\Rule\AlphaNumericDash;
12
use Platine\Validator\Rule\Email;
13
use Platine\Validator\Rule\Enum;
14
use Platine\Validator\Rule\Matches;
15
use Platine\Validator\Rule\MaxLength;
16
use Platine\Validator\Rule\MinLength;
17
use Platine\Validator\Rule\NotEmpty;
18
19
/**
20
* @class UserValidator
21
* @package Platine\App\Validator
22
* @template TEntity as \Platine\Orm\Entity
23
*/
24
class UserValidator extends AbstractValidator
25
{
26
    /**
27
    * Create new instance
28
    * @param UserParam<TEntity> $param
29
    * @param Lang $lang
30
    * @param bool $ignorePassword
31
    */
32
    public function __construct(
33
        protected UserParam $param,
34
        Lang $lang,
35
        protected bool $ignorePassword = false
36
    ) {
37
        parent::__construct($lang);
38
    }
39
40
    /**
41
    * {@inheritdoc}
42
    */
43
    public function setValidationData(): void
44
    {
45
        $this->addData('username', $this->param->getUsername());
46
        $this->addData('lastname', $this->param->getLastname());
47
        $this->addData('firstname', $this->param->getFirstname());
48
        $this->addData('email', $this->param->getEmail());
49
        $this->addData('password', $this->param->getPassword());
50
        $this->addData('password_confirm', $this->param->getPasswordConfirm());
51
        $this->addData('status', $this->param->getStatus());
52
        $this->addData('role', $this->param->getRole());
53
    }
54
55
    /**
56
    * {@inheritdoc}
57
    */
58
    public function setValidationRules(): void
59
    {
60
        $this->addRules('username', [
61
            new NotEmpty(),
62
            new MinLength(3),
63
            new MaxLength(20),
64
            new AlphaNumericDash(),
65
        ]);
66
67
        $this->addRules('lastname', [
68
            new NotEmpty(),
69
            new MinLength(2),
70
            new MaxLength(30),
71
        ]);
72
73
        $this->addRules('firstname', [
74
            new NotEmpty(),
75
            new MinLength(2),
76
            new MaxLength(30),
77
        ]);
78
79
        $this->addRules('email', [
80
            new NotEmpty(),
81
            new Email(),
82
        ]);
83
84
        $this->addRules('password', [
85
            new MinLength(5),
86
            new MaxLength(100),
87
        ]);
88
89
        $this->addRules('password_confirm', [
90
            new MinLength(5),
91
            new MaxLength(100),
92
            new Matches('password'),
93
        ]);
94
95
        if ($this->ignorePassword === false) {
96
            $this->addRule('password', new NotEmpty());
97
            $this->addRule('password_confirm', new NotEmpty());
98
        }
99
100
        $this->addRules('status', [
101
            new NotEmpty(),
102
            new Enum(UserStatus::class),
103
        ]);
104
105
        $this->addRules('role', [
106
            new MinLength(2),
107
            new MaxLength(50),
108
        ]);
109
    }
110
}
111