22
33namespace Loilo \JsonPath ;
44
5- function apply_filter_selector ($ selector , $ root_node , $ json )
5+ function apply_filter_selector ($ selector , $ root_node , Node $ node )
66{
77 // The filter selector works with arrays and objects exclusively.
88 // Its result is a list of(zero, one, multiple, or all)
99 // their array elements or member values, respectively.
1010 // Applied to a primitive value,
1111 // it selects nothing(and therefore does not contribute to the result of the filter selector).
12- if (is_json_primitive ($ json )) {
12+ if (is_json_primitive ($ node -> value )) {
1313 return [];
1414 }
1515
16- return Array\filter (to_array ( $ json ), function ($ item ) use ($ selector , $ root_node ) {
17- return apply_filter_expression ($ selector ->expr , $ root_node , $ item );
16+ return Array\filter (enumerate_node ( $ node ), function (Node $ node ) use ($ selector , $ root_node ) {
17+ return apply_filter_expression ($ selector ->expr , $ root_node , $ node );
1818 });
1919}
2020
21- function apply_filter_expression ($ expr , $ root_node , $ json )
21+ function apply_filter_expression ($ expr , $ root_node , Node $ node )
2222{
2323 $ exp_type = $ expr ->type ;
2424 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 ),
25+ 'ComparisonExpr ' => apply_compare ($ expr , $ root_node , $ node ),
26+ 'TestExpr ' => apply_test ($ expr , $ root_node , $ node ),
27+ 'LogicalBinary ' , 'LogicalUnary ' => apply_logical ($ expr , $ root_node , $ node ),
2828 default => throw new \Exception ("Unexpected expression type: $ exp_type " ),
2929 };
3030}
3131
32- function apply_compare ($ compare , $ root_node , $ json )
32+ function apply_compare ($ compare , $ root_node , Node $ node )
3333{
34- $ left = apply_comparable ($ compare ->left , $ root_node , $ json );
35- $ right = apply_comparable ($ compare ->right , $ root_node , $ json );
34+ $ left = apply_comparable ($ compare ->left , $ root_node , $ node );
35+ $ right = apply_comparable ($ compare ->right , $ root_node , $ node );
3636
3737 return eval_compare ($ left , $ right , $ compare ->operator );
3838}
@@ -86,33 +86,33 @@ function apply_current_node($current_node, $root_node, $node_list)
8686 return apply_segments ($ current_node ->segments , $ root_node , $ node_list );
8787}
8888
89- function apply_comparable ($ comparable , $ root_node , $ json )
89+ function apply_comparable ($ comparable , $ root_node , Node $ node )
9090{
9191 // These can be obtained via literal values; singular queries,
9292 // each of which selects at most one node
9393 return match ($ comparable ->type ) {
9494 'Literal ' => $ comparable ->member ,
9595 'CurrentNode ' => array_key_exists (
9696 0 ,
97- $ result = apply_current_node ($ comparable , $ root_node , [$ json ]),
97+ $ result = apply_current_node ($ comparable , $ root_node , [$ node ]),
9898 )
99- ? $ result [0 ]
99+ ? $ result [0 ]-> value
100100 : nothing (),
101- 'Root ' => apply_root ($ comparable , $ root_node )[0 ] ?? nothing (),
102- 'FunctionExpr ' => apply_function ($ comparable , $ root_node , $ json ),
101+ 'Root ' => apply_root ($ comparable , $ root_node )[0 ]?->value ?? nothing (),
102+ 'FunctionExpr ' => apply_function ($ comparable , $ root_node , $ node ),
103103 };
104104}
105105
106- function apply_test ($ expr , $ root_node , $ json )
106+ function apply_test ($ expr , $ root_node , Node $ node )
107107{
108- return apply_query ($ expr ->query , $ root_node , $ json );
108+ return apply_query ($ expr ->query , $ root_node , $ node );
109109}
110110
111- function apply_query ($ query , $ root_node , $ json )
111+ function apply_query ($ query , $ root_node , Node $ node )
112112{
113113 switch ($ query ->type ) {
114114 case 'FunctionExpr ' :
115- $ function_result = apply_function ($ query , $ root_node , $ json );
115+ $ function_result = apply_function ($ query , $ root_node , $ node );
116116
117117 // LogicalType
118118 if ($ function_result === LogicalType::true ()) {
@@ -129,40 +129,40 @@ function apply_query($query, $root_node, $json)
129129 // ValueType
130130 throw new \Exception ("Function {$ query ->name } result must be compared " );
131131 case 'CurrentNode ' :
132- return sizeof (apply_current_node ($ query , $ root_node , [$ json ])) > 0 ;
132+ return sizeof (apply_current_node ($ query , $ root_node , [$ node ])) > 0 ;
133133 case 'Root ' :
134134 return sizeof (apply_root ($ query , $ root_node )) > 0 ;
135135 }
136136
137137 return false ;
138138}
139139
140- function apply_logical ($ expr , $ root_node , $ json )
140+ function apply_logical ($ expr , $ root_node , Node $ node )
141141{
142142 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 ),
143+ '|| ' => apply_or ($ expr , $ root_node , $ node ),
144+ '&& ' => apply_and ($ expr , $ root_node , $ node ),
145+ '! ' => apply_not ($ expr , $ root_node , $ node ),
146146 };
147147}
148148
149- function apply_or ($ or , $ root_node , $ json )
149+ function apply_or ($ or , $ root_node , Node $ node )
150150{
151151 // TODO: make efficient
152- $ left = apply_filter_expression ($ or ->left , $ root_node , $ json );
153- $ right = apply_filter_expression ($ or ->right , $ root_node , $ json );
152+ $ left = apply_filter_expression ($ or ->left , $ root_node , $ node );
153+ $ right = apply_filter_expression ($ or ->right , $ root_node , $ node );
154154 return $ left || $ right ;
155155}
156156
157- function apply_and ($ and , $ root_node , $ json )
157+ function apply_and ($ and , $ root_node , Node $ node )
158158{
159- $ left = apply_filter_expression ($ and ->left , $ root_node , $ json );
160- $ right = apply_filter_expression ($ and ->right , $ root_node , $ json );
159+ $ left = apply_filter_expression ($ and ->left , $ root_node , $ node );
160+ $ right = apply_filter_expression ($ and ->right , $ root_node , $ node );
161161 return $ left && $ right ;
162162}
163163
164- function apply_not ($ not , $ root_node , $ json )
164+ function apply_not ($ not , $ root_node , Node $ node )
165165{
166- $ result = apply_filter_expression ($ not ->expr , $ root_node , $ json );
166+ $ result = apply_filter_expression ($ not ->expr , $ root_node , $ node );
167167 return !$ result ;
168168}
0 commit comments