55namespace Patchlevel \EventSourcing \DCB ;
66
77use Patchlevel \EventSourcing \Message \Message ;
8- use Patchlevel \EventSourcing \Store \Header \ TagsHeader ;
8+ use Patchlevel \EventSourcing \Store \Query ;
99
10- use function array_diff ;
1110use function array_map ;
12- use function array_merge ;
13- use function array_unique ;
14- use function array_values ;
15- use function in_array ;
16- use function sort ;
1711
1812/**
1913 * @experimental
@@ -27,36 +21,15 @@ public function __construct(
2721 ) {
2822 }
2923
30- /** @return list<string> */
31- public function tagFilter (): array
24+ public function query (): Query
3225 {
33- $ tags = [] ;
26+ $ query = new Query () ;
3427
3528 foreach ($ this ->projections as $ projection ) {
36- $ tags = array_merge ( $ tags , $ projection ->tagFilter ());
29+ $ query = $ query -> add ( $ projection ->queryComponent ());
3730 }
3831
39- return array_values (array_unique ($ tags ));
40- }
41-
42- /** @return list<list<string>> */
43- public function groupedTagFilter (): array
44- {
45- $ result = [];
46-
47- foreach ($ this ->projections as $ projection ) {
48- $ tags = $ projection ->tagFilter ();
49-
50- sort ($ tags );
51-
52- if (in_array ($ tags , $ result , true )) {
53- continue ;
54- }
55-
56- $ result [] = $ tags ;
57- }
58-
59- return $ result ;
32+ return $ query ;
6033 }
6134
6235 /** @return array<string, mixed> */
@@ -69,12 +42,8 @@ public function initialState(): array
6942
7043 public function apply (mixed $ state , Message $ message ): mixed
7144 {
72- $ tags = $ message ->header (TagsHeader::class)->tags ;
73-
7445 foreach ($ this ->projections as $ name => $ projection ) {
75- $ neededTags = $ projection ->tagFilter ();
76-
77- if (!$ this ->isSubset ($ neededTags , $ tags )) {
46+ if (!$ projection ->queryComponent ()->match ($ message )) {
7847 continue ;
7948 }
8049
@@ -83,13 +52,4 @@ public function apply(mixed $state, Message $message): mixed
8352
8453 return $ state ;
8554 }
86-
87- /**
88- * @param list<string> $needle
89- * @param list<string> $haystack
90- */
91- private function isSubset (array $ needle , array $ haystack ): bool
92- {
93- return empty (array_diff ($ needle , $ haystack ));
94- }
9555}
0 commit comments