10
10
class MonthYearRange
11
11
{
12
12
#[Assert \NotBlank]
13
- #[Assert \Range(
14
- min: 1 ,
15
- max: 12 ,
16
- )]
13
+ #[Assert \Range(min: 1 , max: 12 )]
17
14
#[ORM \Column(type: 'smallint ' , nullable: true )]
18
15
private ?int $ beginMonth = null ;
19
16
20
17
#[Assert \NotBlank]
21
18
#[ORM \Column(type: 'smallint ' , nullable: true )]
22
19
private ?int $ beginYear = null ;
23
20
24
- #[Assert \NotBlank]
25
- #[Assert \Range(
26
- min: 1 ,
27
- max: 12 ,
28
- )]
21
+ #[Assert \Range(min: 1 , max: 12 )]
29
22
#[ORM \Column(type: 'smallint ' , nullable: true )]
30
23
private ?int $ endMonth = null ;
31
24
32
- #[Assert \NotBlank]
33
25
#[ORM \Column(type: 'smallint ' , nullable: true )]
34
26
private ?int $ endYear = null ;
35
27
@@ -38,46 +30,71 @@ public function getBeginMonth(): ?int
38
30
return $ this ->beginMonth ;
39
31
}
40
32
41
- public function setBeginMonth (?int $ beginMonth ): void
33
+ public function setBeginMonth (?int $ beginMonth ): self
42
34
{
43
35
$ this ->beginMonth = $ beginMonth ;
36
+
37
+ return $ this ;
44
38
}
45
39
46
40
public function getBeginYear (): ?int
47
41
{
48
42
return $ this ->beginYear ;
49
43
}
50
44
51
- public function setBeginYear (?int $ beginYear ): void
45
+ public function setBeginYear (?int $ beginYear ): self
52
46
{
53
47
$ this ->beginYear = $ beginYear ;
48
+
49
+ return $ this ;
54
50
}
55
51
56
52
public function getEndMonth (): ?int
57
53
{
58
54
return $ this ->endMonth ;
59
55
}
60
56
61
- public function setEndMonth (?int $ endMonth ): void
57
+ public function setEndMonth (?int $ endMonth ): self
62
58
{
63
59
$ this ->endMonth = $ endMonth ;
60
+
61
+ return $ this ;
64
62
}
65
63
66
64
public function getEndYear (): ?int
67
65
{
68
66
return $ this ->endYear ;
69
67
}
70
68
71
- public function setEndYear (?int $ endYear ): void
69
+ public function setEndYear (?int $ endYear ): self
72
70
{
73
71
$ this ->endYear = $ endYear ;
72
+
73
+ return $ this ;
74
74
}
75
75
76
76
#[Assert \Callback]
77
- public function validate (ExecutionContextInterface $ context , $ payload )
77
+ public function validate (ExecutionContextInterface $ context , $ payload ): void
78
78
{
79
+ $ endYear = $ this ->getEndYear ();
80
+ $ endMonth = $ this ->getEndMonth ();
81
+
82
+ if ($ endYear === null && $ endMonth === null ) {
83
+ return ;
84
+ }
85
+
86
+ if ($ endYear === null || $ endMonth === null ) {
87
+ $ context ->buildViolation ('trexima_european_cv.range_not_valid ' )
88
+ ->atPath ('endMonth ' )
89
+ ->addViolation ();
90
+ $ context ->buildViolation ('trexima_european_cv.range_not_valid ' )
91
+ ->atPath ('endYear ' )
92
+ ->addViolation ();
93
+ return ;
94
+ }
95
+
79
96
$ from = $ this ->getBeginYear () . sprintf ('%02d ' , $ this ->getBeginMonth ());
80
- $ to = $ this -> getEndYear () . sprintf ('%02d ' , $ this -> getEndMonth () );
97
+ $ to = $ endYear . sprintf ('%02d ' , $ endMonth );
81
98
82
99
if ($ from > $ to ) {
83
100
$ context ->buildViolation ('trexima_european_cv.range_not_valid ' )
@@ -87,11 +104,11 @@ public function validate(ExecutionContextInterface $context, $payload)
87
104
->atPath ('beginYear ' )
88
105
->addViolation ();
89
106
$ context ->buildViolation ('trexima_european_cv.range_not_valid ' )
90
- ->atPath ('endYear ' )
107
+ ->atPath ('endMonth ' )
91
108
->addViolation ();
92
109
$ context ->buildViolation ('trexima_european_cv.range_not_valid ' )
93
- ->atPath ('endMonth ' )
110
+ ->atPath ('endYear ' )
94
111
->addViolation ();
95
112
}
96
113
}
97
- }
114
+ }
0 commit comments