@@ -63,8 +63,6 @@ public function __construct(
63
63
public function getData (Feed $ feed ): array
64
64
{
65
65
try {
66
- $ results = [];
67
-
68
66
$ configuration = $ feed ->getConfiguration ();
69
67
70
68
$ enabledModifiers = $ configuration ['enabledModifiers ' ] ?? [];
@@ -76,62 +74,28 @@ public function getData(Feed $feed): array
76
74
}
77
75
78
76
$ resources = $ configuration ['resources ' ];
79
- foreach ($ resources as $ resource ) {
80
- $ events = $ this ->getResourceEvents ($ resource );
81
-
82
- /** @var CalendarEvent $event */
83
- foreach ($ events as $ event ) {
84
- $ title = $ event ->title ;
85
-
86
- // Modify title according to event modifiers.
87
- foreach ($ this ->eventModifiers as $ modifier ) {
88
- // Make it configurable in the Feed if the modifiers should be enabled.
89
- if ($ modifier ['activateInFeed ' ] && !in_array ($ modifier ['id ' ], $ enabledModifiers )) {
90
- continue ;
91
- }
92
-
93
- if (self ::EXCLUDE_IF_TITLE_NOT_CONTAINS == $ modifier ['type ' ]) {
94
- $ match = preg_match ('/ ' .$ modifier ['trigger ' ].'/ ' .(!$ modifier ['caseSensitive ' ] ? 'i ' : '' ), $ title );
95
-
96
- if ($ modifier ['removeTrigger ' ]) {
97
- $ title = str_replace ($ modifier ['trigger ' ], '' , $ title );
98
- }
99
-
100
- if (!$ match ) {
101
- continue ;
102
- }
103
- }
104
-
105
- if (self ::REPLACE_TITLE_IF_CONTAINS == $ modifier ['type ' ]) {
106
- $ match = preg_match ('/ ' .$ modifier ['trigger ' ].'/ ' .(!$ modifier ['caseSensitive ' ] ? 'i ' : '' ), $ title );
107
-
108
- if ($ modifier ['removeTrigger ' ]) {
109
- $ title = str_replace ($ modifier ['trigger ' ], '' , $ title );
110
- }
111
-
112
- if ($ match ) {
113
- $ title = $ modifier ['replacement ' ];
114
- }
115
- }
116
- }
117
77
118
- $ title = trim ( $ title ) ;
78
+ $ events = [] ;
119
79
120
- $ results [] = [
121
- 'id ' => Ulid::generate (),
122
- 'title ' => $ title ,
123
- 'startTime ' => $ event ->startTimeTimestamp ,
124
- 'endTime ' => $ event ->endTimeTimestamp ,
125
- 'resourceTitle ' => $ event ->resourceDisplayName ,
126
- 'resourceId ' => $ event ->resourceId ,
127
- ];
128
- }
80
+ foreach ($ resources as $ resource ) {
81
+ $ events += $ this ->getResourceEvents ($ resource );
129
82
}
130
83
84
+ $ modifiedResults = static ::applyModifiersToEvents ($ events , $ this ->eventModifiers , $ enabledModifiers );
85
+
86
+ $ resultsAsArray = array_map (fn (CalendarEvent $ event ) => [
87
+ 'id ' => Ulid::generate (),
88
+ 'title ' => $ event ->title ,
89
+ 'startTime ' => $ event ->startTimeTimestamp ,
90
+ 'endTime ' => $ event ->endTimeTimestamp ,
91
+ 'resourceTitle ' => $ event ->resourceDisplayName ,
92
+ 'resourceId ' => $ event ->resourceId ,
93
+ ], $ modifiedResults );
94
+
131
95
// Sort bookings by start time.
132
- usort ($ results , fn (array $ a , array $ b ) => $ a ['startTime ' ] > $ b ['startTime ' ] ? 1 : -1 );
96
+ usort ($ resultsAsArray , fn (array $ a , array $ b ) => $ a ['startTime ' ] > $ b ['startTime ' ] ? 1 : -1 );
133
97
134
- return $ results ;
98
+ return $ resultsAsArray ;
135
99
} catch (\Throwable $ throwable ) {
136
100
$ this ->logger ->error ('{code}: {message} ' , [
137
101
'code ' => $ throwable ->getCode (),
@@ -142,6 +106,54 @@ public function getData(Feed $feed): array
142
106
return [];
143
107
}
144
108
109
+ public static function applyModifiersToEvents (array $ events , array $ eventModifiers , array $ enabledModifiers ): array
110
+ {
111
+ $ results = [];
112
+
113
+ /** @var CalendarEvent $event */
114
+ foreach ($ events as $ event ) {
115
+ $ title = $ event ->title ;
116
+
117
+ // Modify title according to event modifiers.
118
+ foreach ($ eventModifiers as $ modifier ) {
119
+ // Make it configurable in the Feed if the modifiers should be enabled.
120
+ if ($ modifier ['activateInFeed ' ] && !in_array ($ modifier ['id ' ], $ enabledModifiers )) {
121
+ continue ;
122
+ }
123
+
124
+ $ pattern = $ modifier ['pattern ' ];
125
+
126
+ if (self ::EXCLUDE_IF_TITLE_NOT_CONTAINS == $ modifier ['type ' ]) {
127
+ $ match = preg_match ($ pattern , $ title );
128
+
129
+ if (!$ match ) {
130
+ continue 2 ;
131
+ }
132
+
133
+ if ($ modifier ['removeTrigger ' ]) {
134
+ $ title = preg_replace ($ pattern , '' , $ title );
135
+ }
136
+ }
137
+
138
+ if (self ::REPLACE_TITLE_IF_CONTAINS == $ modifier ['type ' ]) {
139
+ $ match = preg_match ($ pattern , $ title );
140
+
141
+ if ($ match ) {
142
+ $ title = $ modifier ['replacement ' ];
143
+ }
144
+ }
145
+ }
146
+
147
+ $ title = trim ($ title );
148
+
149
+ $ event ->title = $ title ;
150
+
151
+ $ results [] = $ event ;
152
+ }
153
+
154
+ return $ results ;
155
+ }
156
+
145
157
/**
146
158
* {@inheritDoc}
147
159
*/
0 commit comments