@@ -21,19 +21,12 @@ function apply_filter_selector($selector, $root_node, $json)
2121function apply_filter_expression ($ expr , $ root_node , $ json )
2222{
2323 $ exp_type = $ expr ->type ;
24- switch ($ exp_type ) {
25- case 'ComparisonExpr ' :
26- return apply_compare ($ expr , $ root_node , $ json );
27- case 'TestExpr ' :
28- return apply_test ($ expr , $ root_node , $ json );
29- case 'LogicalBinary ' :
30- case 'LogicalUnary ' :
31- return apply_logical ($ expr , $ root_node , $ json );
32- default :
33- throw new \Exception ("Unexpected expression type: $ exp_type " );
34- }
35-
36- return false ;
24+ return match ($ exp_type ) {
25+ 'ComparisonExpr ' => apply_compare ($ expr , $ root_node , $ json ),
26+ 'TestExpr ' => apply_test ($ expr , $ root_node , $ json ),
27+ 'LogicalBinary ' , 'LogicalUnary ' => apply_logical ($ expr , $ root_node , $ json ),
28+ default => throw new \Exception ("Unexpected expression type: $ exp_type " ),
29+ };
3730}
3831
3932function apply_compare ($ compare , $ root_node , $ json )
@@ -97,17 +90,17 @@ function apply_comparable($comparable, $root_node, $json)
9790{
9891 // These can be obtained via literal values; singular queries,
9992 // each of which selects at most one node
100- switch ($ comparable ->type ) {
101- case 'Literal ' :
102- return $ comparable -> member ;
103- case ' CurrentNode ' :
104- $ result = apply_current_node ($ comparable , $ root_node , [$ json ]);
105- return array_key_exists ( 0 , $ result ) ? $ result [ 0 ] : nothing ();
106- case ' Root ' :
107- return apply_root ( $ comparable , $ root_node )[ 0 ] ?? nothing ();
108- case ' FunctionExpr ' :
109- return apply_function ($ comparable , $ root_node , $ json );
110- }
93+ return match ($ comparable ->type ) {
94+ 'Literal ' => $ comparable -> member ,
95+ ' CurrentNode ' => array_key_exists (
96+ 0 ,
97+ $ result = apply_current_node ($ comparable , $ root_node , [$ json ]),
98+ )
99+ ? $ result [ 0 ]
100+ : nothing (),
101+ ' Root ' => apply_root ( $ comparable , $ root_node )[ 0 ] ?? nothing (),
102+ ' FunctionExpr ' => apply_function ($ comparable , $ root_node , $ json ),
103+ };
111104}
112105
113106function apply_test ($ expr , $ root_node , $ json )
@@ -146,14 +139,11 @@ function apply_query($query, $root_node, $json)
146139
147140function apply_logical ($ expr , $ root_node , $ json )
148141{
149- switch ($ expr ->operator ) {
150- case '|| ' :
151- return apply_or ($ expr , $ root_node , $ json );
152- case '&& ' :
153- return apply_and ($ expr , $ root_node , $ json );
154- case '! ' :
155- return apply_not ($ expr , $ root_node , $ json );
156- }
142+ return match ($ expr ->operator ) {
143+ '|| ' => apply_or ($ expr , $ root_node , $ json ),
144+ '&& ' => apply_and ($ expr , $ root_node , $ json ),
145+ '! ' => apply_not ($ expr , $ root_node , $ json ),
146+ };
157147}
158148
159149function apply_or ($ or , $ root_node , $ json )
0 commit comments