1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Validator; |
13
|
|
|
|
14
|
|
|
use SimpleValidator\Validator; |
15
|
|
|
use SimpleValidator\Validators; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Subtask Validator. |
19
|
|
|
*/ |
20
|
|
|
class SubtaskValidator extends BaseValidator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Validate creation. |
24
|
|
|
* |
25
|
|
|
* @param array $values Form values |
26
|
|
|
* |
27
|
|
|
* @return array $valid, $errors [0] = Success or not, [1] = List of errors |
28
|
|
|
*/ |
29
|
|
|
public function validateCreation(array $values) |
30
|
|
|
{ |
31
|
|
|
$rules = [ |
32
|
|
|
new Validators\Required('task_id', t('The task id is required')), |
33
|
|
|
new Validators\Required('title', t('The title is required')), |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
$v = new Validator($values, array_merge($rules, $this->commonValidationRules())); |
37
|
|
|
|
38
|
|
|
return [ |
39
|
|
|
$v->execute(), |
40
|
|
|
$v->getErrors(), |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Validate modification. |
46
|
|
|
* |
47
|
|
|
* @param array $values Form values |
48
|
|
|
* |
49
|
|
|
* @return array $valid, $errors [0] = Success or not, [1] = List of errors |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function validateModification(array $values) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$rules = [ |
54
|
|
|
new Validators\Required('id', t('The subtask id is required')), |
55
|
|
|
new Validators\Required('task_id', t('The task id is required')), |
56
|
|
|
new Validators\Required('title', t('The title is required')), |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
$v = new Validator($values, array_merge($rules, $this->commonValidationRules())); |
60
|
|
|
|
61
|
|
|
return [ |
62
|
|
|
$v->execute(), |
63
|
|
|
$v->getErrors(), |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Validate API modification. |
69
|
|
|
* |
70
|
|
|
* @param array $values Form values |
71
|
|
|
* |
72
|
|
|
* @return array $valid, $errors [0] = Success or not, [1] = List of errors |
73
|
|
|
*/ |
74
|
|
|
public function validateApiModification(array $values) |
75
|
|
|
{ |
76
|
|
|
$rules = [ |
77
|
|
|
new Validators\Required('id', t('The subtask id is required')), |
78
|
|
|
new Validators\Required('task_id', t('The task id is required')), |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
$v = new Validator($values, array_merge($rules, $this->commonValidationRules())); |
82
|
|
|
|
83
|
|
|
return [ |
84
|
|
|
$v->execute(), |
85
|
|
|
$v->getErrors(), |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Common validation rules. |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
private function commonValidationRules() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
new Validators\Integer('id', t('The subtask id must be an integer')), |
98
|
|
|
new Validators\Integer('task_id', t('The task id must be an integer')), |
99
|
|
|
new Validators\MaxLength('title', t('The maximum length is %d characters', 255), 255), |
100
|
|
|
new Validators\Integer('user_id', t('The user id must be an integer')), |
101
|
|
|
new Validators\Integer('status', t('The status must be an integer')), |
102
|
|
|
new Validators\Numeric('time_estimated', t('The time must be a numeric value')), |
103
|
|
|
new Validators\Numeric('time_spent', t('The time must be a numeric value')), |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.