Skip to content

Commit e02ca3d

Browse files
authored
Merge pull request #363 from os2display/feature/358-more-filter-options
358: Add area and facility filters
2 parents 7cfc9cc + f2e103c commit e02ca3d

2 files changed

Lines changed: 63 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7-
### Added
8-
7+
- [#363](https://github.com/os2display/display-api-service/pull/363)
8+
- Added optional 'area' and 'facility' configuration fields
99
- [#362](https://github.com/os2display/display-api-service/pull/362)
1010
- Added new API version field and variable to handle new feature from issue #352.
11-
1211
- [#385](https://github.com/os2display/display-api-service/pull/385)
1312
- Replaced PSR-6 caching with Symfony `CacheInterface::get()` in
1413
`FeedService` and `EventDatabaseApiV2FeedType` for stampede prevention.

src/Feed/BrndFeedType.php

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ public function getAdminFormOptions(FeedSource $feedSource): array
4949
'label' => 'Sportcenter ID',
5050
'formGroupClasses' => 'mb-3',
5151
],
52+
[
53+
'key' => 'brnd-area',
54+
'input' => 'input',
55+
'type' => 'text',
56+
'name' => 'area',
57+
'label' => 'Område',
58+
'formGroupClasses' => 'mb-3',
59+
],
60+
[
61+
'key' => 'brnd-facility',
62+
'input' => 'input',
63+
'type' => 'text',
64+
'name' => 'facility',
65+
'label' => 'Facilitet',
66+
'formGroupClasses' => 'mb-3',
67+
],
5268
];
5369
}
5470

@@ -71,28 +87,68 @@ public function getData(Feed $feed): array
7187

7288
$baseUri = $secrets->apiBaseUri;
7389
$sportCenterId = $configuration['sport_center_id'] ?? null;
90+
$areaFilter = $configuration['area'] ?? '';
91+
$facilityFilter = $configuration['facility'] ?? '';
7492

75-
if ('' === $baseUri || null === $sportCenterId || '' === $sportCenterId) {
93+
if ('' === $baseUri || !is_string($sportCenterId) || '' === trim($sportCenterId)) {
7694
return $result;
7795
}
7896

97+
$areaFilterNormalized = self::normalizeFilterValue($areaFilter);
98+
$facilityFilterNormalized = self::normalizeFilterValue($facilityFilter);
99+
79100
$bookings = $this->apiClient->getInfomonitorBookingsDetails($feedSource, $sportCenterId);
80101

81-
return array_reduce($bookings, function (array $carry, array $booking): array {
102+
$result['bookings'] = array_reduce($bookings, function (array $carry, mixed $booking) use ($areaFilterNormalized, $facilityFilterNormalized): array {
103+
if (!is_array($booking)) {
104+
return $carry;
105+
}
106+
82107
$parsedBooking = $this->parseBrndBooking($booking);
83108

84-
// Validate that booking has required fields
85-
if (!empty($parsedBooking['bookingcode']) && !empty($parsedBooking['bookingBy'])) {
86-
$carry[] = $parsedBooking;
109+
// Bail out if required fields are missing.
110+
if (empty($parsedBooking['bookingcode']) || empty($parsedBooking['bookingBy'])) {
111+
return $carry;
87112
}
88113

114+
// Bail out if area filter applies and booking area does not match.
115+
if ('' !== $areaFilterNormalized) {
116+
$bookingArea = self::normalizeFilterValue($parsedBooking['area'] ?? '');
117+
if ($bookingArea !== $areaFilterNormalized) {
118+
return $carry;
119+
}
120+
}
121+
122+
// Bail out if facility filter applies and booking facility does not match.
123+
if ('' !== $facilityFilterNormalized) {
124+
$bookingFacility = self::normalizeFilterValue($parsedBooking['facility'] ?? '');
125+
if ($bookingFacility !== $facilityFilterNormalized) {
126+
return $carry;
127+
}
128+
}
129+
130+
$carry[] = $parsedBooking;
131+
89132
return $carry;
90133
}, []);
91134
} catch (\Throwable $throwable) {
92135
$this->logger->error($throwable->getMessage());
93136

94137
throw $throwable;
95138
}
139+
140+
return $result;
141+
}
142+
143+
private static function normalizeFilterValue(mixed $value): string
144+
{
145+
if (!is_string($value)) {
146+
return '';
147+
}
148+
149+
$value = trim($value);
150+
151+
return strtolower($value);
96152
}
97153

98154
private function parseBrndBooking(array $booking): array

0 commit comments

Comments
 (0)