@@ -72,11 +72,23 @@ protected function filterProperty(
72
72
throw new InvalidArgumentException (sprintf ('“%s” filter must be only used with a string value. ' , self ::PARAMETER_ARCHIVE ));
73
73
}
74
74
75
- $ range = $ this ->normalizeFilteringDates ($ value [self ::PARAMETER_ARCHIVE ]);
75
+ $ this ->singleArchiveFilter (
76
+ $ queryBuilder ,
77
+ $ queryNameGenerator ,
78
+ $ alias ,
79
+ $ field ,
80
+ $ value [self ::PARAMETER_ARCHIVE ],
81
+ );
82
+ }
76
83
77
- if (null === $ range || 2 !== count ($ range )) {
78
- return ;
79
- }
84
+ protected function singleArchiveFilter (
85
+ QueryBuilder $ queryBuilder ,
86
+ QueryNameGeneratorInterface $ queryNameGenerator ,
87
+ string $ alias ,
88
+ string $ field ,
89
+ string $ value ,
90
+ ): void {
91
+ [$ startDate , $ endDate ] = $ this ->normalizeFilteringDates ($ value );
80
92
81
93
$ valueParameter = $ queryNameGenerator ->generateParameterName ($ field );
82
94
$ queryBuilder ->andWhere ($ queryBuilder ->expr ()->isNotNull (sprintf ('%s.%s ' , $ alias , $ field )))
@@ -85,43 +97,43 @@ protected function filterProperty(
85
97
': ' .$ valueParameter .'Start ' ,
86
98
': ' .$ valueParameter .'End '
87
99
))
88
- ->setParameter ($ valueParameter .'Start ' , $ range [ 0 ] )
89
- ->setParameter ($ valueParameter .'End ' , $ range [ 1 ] );
100
+ ->setParameter ($ valueParameter .'Start ' , $ startDate )
101
+ ->setParameter ($ valueParameter .'End ' , $ endDate );
90
102
}
91
103
92
104
/**
93
105
* Support archive parameter with year or year-month.
94
106
*
95
- * @return \DateTime[]|null
107
+ * @return array{ \DateTime, \DateTime}
96
108
*
97
109
* @throws \Exception
98
110
*/
99
- protected function normalizeFilteringDates (string $ value ): ? array
111
+ protected function normalizeFilteringDates (string $ value ): array
100
112
{
101
113
/*
102
114
* Support archive parameter with year or year-month
103
115
*/
104
- if (preg_match ('#[0-9]{4}\-[0-9]{2}\-[0-9]{2}# ' , $ value ) > 0 ) {
116
+ if (preg_match ('#^ [0-9]{4}\-[0-9]{2}\-[0-9]{2}$ # ' , $ value ) > 0 ) {
105
117
$ startDate = new \DateTime ($ value .' 00:00:00 ' );
106
118
$ endDate = clone $ startDate ;
107
119
$ endDate ->add (new \DateInterval ('P1D ' ));
108
120
109
121
return [$ startDate , $ this ->limitEndDate ($ endDate )];
110
- } elseif (preg_match ('#[0-9]{4}\-[0-9]{2}# ' , $ value ) > 0 ) {
122
+ } elseif (preg_match ('#^ [0-9]{4}\-[0-9]{2}$ # ' , $ value ) > 0 ) {
111
123
$ startDate = new \DateTime ($ value .'-01 00:00:00 ' );
112
124
$ endDate = clone $ startDate ;
113
125
$ endDate ->add (new \DateInterval ('P1M ' ));
114
126
115
127
return [$ startDate , $ this ->limitEndDate ($ endDate )];
116
- } elseif (preg_match ('#[0-9]{4}# ' , $ value ) > 0 ) {
128
+ } elseif (preg_match ('#^ [0-9]{4}$ # ' , $ value ) > 0 ) {
117
129
$ startDate = new \DateTime ($ value .'-01-01 00:00:00 ' );
118
130
$ endDate = clone $ startDate ;
119
131
$ endDate ->add (new \DateInterval ('P1Y ' ));
120
132
121
133
return [$ startDate , $ this ->limitEndDate ($ endDate )];
122
134
}
123
135
124
- return null ;
136
+ throw new InvalidArgumentException ( sprintf ( ' “%s” filter must be a valid archive specification. ' , self :: PARAMETER_ARCHIVE )) ;
125
137
}
126
138
127
139
protected function limitEndDate (\DateTime $ endDate ): \DateTime
0 commit comments