|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Platine\App\Validator; |
|
6
|
|
|
|
|
7
|
|
|
use Platine\App\Param\ProductParam; |
|
8
|
|
|
use Platine\Framework\Form\Validator\AbstractValidator; |
|
9
|
|
|
use Platine\Lang\Lang; |
|
10
|
|
|
use Platine\Validator\Rule\Integer; |
|
11
|
|
|
use Platine\Validator\Rule\MaxLength; |
|
12
|
|
|
use Platine\Validator\Rule\Min; |
|
13
|
|
|
use Platine\Validator\Rule\MinLength; |
|
14
|
|
|
use Platine\Validator\Rule\NotEmpty; |
|
15
|
|
|
use Platine\Validator\Rule\Number; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @class ProductValidator |
|
19
|
|
|
* @package Platine\App\Validator |
|
20
|
|
|
* @template TEntity as \Platine\Orm\Entity |
|
21
|
|
|
*/ |
|
22
|
|
|
class ProductValidator extends AbstractValidator |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* The parameter instance |
|
26
|
|
|
* @var ProductParam<TEntity> |
|
27
|
|
|
*/ |
|
28
|
|
|
protected ProductParam $param; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create new instance |
|
32
|
|
|
* @param ProductParam<TEntity> $param |
|
33
|
|
|
* @param Lang $lang |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(ProductParam $param, Lang $lang) |
|
36
|
|
|
{ |
|
37
|
|
|
parent::__construct($lang); |
|
38
|
|
|
$this->param = $param; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setValidationData(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->addData('name', $this->param->getName()); |
|
47
|
|
|
$this->addData('description', $this->param->getDescription()); |
|
48
|
|
|
$this->addData('price', $this->param->getPrice()); |
|
49
|
|
|
$this->addData('quantity', $this->param->getQuantity()); |
|
50
|
|
|
$this->addData('category', $this->param->getCategory()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setValidationRules(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->addRules('name', [ |
|
59
|
|
|
new NotEmpty(), |
|
60
|
|
|
new MinLength(2), |
|
61
|
|
|
new MaxLength(50), |
|
62
|
|
|
]); |
|
63
|
|
|
|
|
64
|
|
|
$this->addRules('description', [ |
|
65
|
|
|
new MinLength(2), |
|
66
|
|
|
new MaxLength(150), |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
$this->addRules('price', [ |
|
70
|
|
|
new NotEmpty(), |
|
71
|
|
|
new Number(), |
|
72
|
|
|
new Min(0), |
|
73
|
|
|
]); |
|
74
|
|
|
|
|
75
|
|
|
$this->addRules('quantity', [ |
|
76
|
|
|
new NotEmpty(), |
|
77
|
|
|
new Number(), |
|
78
|
|
|
new Min(0), |
|
79
|
|
|
]); |
|
80
|
|
|
|
|
81
|
|
|
$this->addRules('category', [ |
|
82
|
|
|
new NotEmpty(), |
|
83
|
|
|
new Integer(), |
|
84
|
|
|
]); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|