Skip to content

Commit 7f7b33c

Browse files
author
roadiz-ci
committed
Merge branch hotfix/v2.5.1
1 parent a24e9db commit 7f7b33c

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

config/services.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
parameters:
3-
roadiz_core.cms_version: '2.5.0'
3+
roadiz_core.cms_version: '2.5.1'
44
roadiz_core.cms_version_prefix: 'main'
55
env(APP_NAMESPACE): "roadiz"
66
env(APP_VERSION): "0.1.0"

src/Api/Filter/ArchiveFilter.php

+24-12
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,23 @@ protected function filterProperty(
7272
throw new InvalidArgumentException(sprintf('“%s” filter must be only used with a string value.', self::PARAMETER_ARCHIVE));
7373
}
7474

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+
}
7683

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);
8092

8193
$valueParameter = $queryNameGenerator->generateParameterName($field);
8294
$queryBuilder->andWhere($queryBuilder->expr()->isNotNull(sprintf('%s.%s', $alias, $field)))
@@ -85,43 +97,43 @@ protected function filterProperty(
8597
':'.$valueParameter.'Start',
8698
':'.$valueParameter.'End'
8799
))
88-
->setParameter($valueParameter.'Start', $range[0])
89-
->setParameter($valueParameter.'End', $range[1]);
100+
->setParameter($valueParameter.'Start', $startDate)
101+
->setParameter($valueParameter.'End', $endDate);
90102
}
91103

92104
/**
93105
* Support archive parameter with year or year-month.
94106
*
95-
* @return \DateTime[]|null
107+
* @return array{\DateTime, \DateTime}
96108
*
97109
* @throws \Exception
98110
*/
99-
protected function normalizeFilteringDates(string $value): ?array
111+
protected function normalizeFilteringDates(string $value): array
100112
{
101113
/*
102114
* Support archive parameter with year or year-month
103115
*/
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) {
105117
$startDate = new \DateTime($value.' 00:00:00');
106118
$endDate = clone $startDate;
107119
$endDate->add(new \DateInterval('P1D'));
108120

109121
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) {
111123
$startDate = new \DateTime($value.'-01 00:00:00');
112124
$endDate = clone $startDate;
113125
$endDate->add(new \DateInterval('P1M'));
114126

115127
return [$startDate, $this->limitEndDate($endDate)];
116-
} elseif (preg_match('#[0-9]{4}#', $value) > 0) {
128+
} elseif (preg_match('#^[0-9]{4}$#', $value) > 0) {
117129
$startDate = new \DateTime($value.'-01-01 00:00:00');
118130
$endDate = clone $startDate;
119131
$endDate->add(new \DateInterval('P1Y'));
120132

121133
return [$startDate, $this->limitEndDate($endDate)];
122134
}
123135

124-
return null;
136+
throw new InvalidArgumentException(sprintf('“%s” filter must be a valid archive specification.', self::PARAMETER_ARCHIVE));
125137
}
126138

127139
protected function limitEndDate(\DateTime $endDate): \DateTime

0 commit comments

Comments
 (0)