3v4l.org

run code in 300+ PHP versions simultaneously
<?php $obj = new stdClass(); $obj->a = 5; $obj->b = 2; $obj->c = 9; $filter = array( 'logic' => 'AND', 'filters' => array( array('field' => 'a', 'operator' => '===', 'value' => 5), array( 'logic' => 'OR', 'filters' => array( array('field' => 'b', 'operator' => '===', 'value' => 6), array('field' => 'c', 'operator' => '<', 'value' => 10), ), ), ), ); function process(string $logic, array $filters, object $obj): bool { // For an AND, all things must be true, so if we're given 5 things, all 5 must be true. // For an OR, only one needs to be true. $targetCount = 'AND' === $logic ? count($filters) : 1; // How many items were true $currentCount = 0; foreach ($filters as $filter) { // The sub-array is a grouping, grab parts, process, and if true, increment our local counter if (array_key_exists('logic', $filter)) { if (process($filter['logic'], $filter['filters'], $obj)) { $currentCount++; } // For a sub-array, don't process anything else continue; } // Grab array items as variables just for ease of use $field = $filter['field']; $operator = $filter['operator']; $value = $filter['value']; // Match on the operator. For lower versions of PHP a switch(true) or just an if could be used, too. $result = match ($operator) { '===' => $obj->$field === $value, '==' => $obj->$field == $value, '>=' => $obj->$field >= $value, '>' => $obj->$field > $value, '<=' => $obj->$field <= $value, '<' => $obj->$field < $value, }; // If success, increment our counter if ($result) { $currentCount++; // If we've reach our target, return success // This could also just be "if process = OR" if ($currentCount === $targetCount) { return true; } } } // See if we met the final condition outside of the loop return $currentCount === $targetCount; } echo process($filter['logic'], $filter['filters'], $obj) ? 'yes' : 'no';
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 20
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Hu0CY
function name:  (null)
number of ops:  23
compiled vars:  !0 = $obj, !1 = $filter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   NEW                                              $2      'stdClass'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
    4     3        ASSIGN_OBJ                                               !0, 'a'
          4        OP_DATA                                                  5
    5     5        ASSIGN_OBJ                                               !0, 'b'
          6        OP_DATA                                                  2
    6     7        ASSIGN_OBJ                                               !0, 'c'
          8        OP_DATA                                                  9
    8     9        ASSIGN                                                   !1, <array>
   73    10        INIT_FCALL                                               'process'
         11        FETCH_DIM_R                                      ~9      !1, 'logic'
         12        SEND_VAL                                                 ~9
         13        FETCH_DIM_R                                      ~10     !1, 'filters'
         14        SEND_VAL                                                 ~10
         15        SEND_VAR                                                 !0
         16        DO_FCALL                                      0  $11     
         17      > JMPZ                                                     $11, ->20
         18    >   QM_ASSIGN                                        ~12     'yes'
         19      > JMP                                                      ->21
         20    >   QM_ASSIGN                                        ~12     'no'
         21    >   ECHO                                                     ~12
         22      > RETURN                                                   1

Function process:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 67
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 67
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 27
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 26
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 26
Branch analysis from position: 27
7 jumps found. (Code = 195) Position 1 = 35, Position 2 = 39, Position 3 = 43, Position 4 = 47, Position 5 = 51, Position 6 = 55, Position 7 = 34
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 61, Position 2 = 66
Branch analysis from position: 61
2 jumps found. (Code = 43) Position 1 = 64, Position 2 = 66
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 66
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 34
1 jumps found. (Code = 197) Position 1 = -2
Branch analysis from position: 67
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 67
Branch analysis from position: 8
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 67
Branch analysis from position: 12
Branch analysis from position: 67
filename:       /in/Hu0CY
function name:  process
number of ops:  73
compiled vars:  !0 = $logic, !1 = $filters, !2 = $obj, !3 = $targetCount, !4 = $currentCount, !5 = $filter, !6 = $field, !7 = $operator, !8 = $value, !9 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   26     3        IS_IDENTICAL                                             !0, 'AND'
          4      > JMPZ                                                     ~10, ->8
          5    >   COUNT                                            ~11     !1
          6        QM_ASSIGN                                        ~12     ~11
          7      > JMP                                                      ->9
          8    >   QM_ASSIGN                                        ~12     1
          9    >   ASSIGN                                                   !3, ~12
   29    10        ASSIGN                                                   !4, 0
   30    11      > FE_RESET_R                                       $15     !1, ->67
         12    > > FE_FETCH_R                                               $15, !5, ->67
   33    13    >   ARRAY_KEY_EXISTS                                         'logic', !5
         14      > JMPZ                                                     ~16, ->27
   34    15    >   INIT_FCALL_BY_NAME                                       'process'
         16        CHECK_FUNC_ARG                                           
         17        FETCH_DIM_FUNC_ARG                               $17     !5, 'logic'
         18        SEND_FUNC_ARG                                            $17
         19        CHECK_FUNC_ARG                                           
         20        FETCH_DIM_FUNC_ARG                               $18     !5, 'filters'
         21        SEND_FUNC_ARG                                            $18
         22        SEND_VAR_EX                                              !2
         23        DO_FCALL                                      0  $19     
         24      > JMPZ                                                     $19, ->26
   35    25    >   PRE_INC                                                  !4
   39    26    > > JMP                                                      ->12
   43    27    >   FETCH_DIM_R                                      ~21     !5, 'field'
         28        ASSIGN                                                   !6, ~21
   44    29        FETCH_DIM_R                                      ~23     !5, 'operator'
         30        ASSIGN                                                   !7, ~23
   45    31        FETCH_DIM_R                                      ~25     !5, 'value'
         32        ASSIGN                                                   !8, ~25
   48    33      > MATCH                                                    !7, [ '%3D%3D%3D':->35, '%3D%3D':->39, '%3E%3D':->43, '%3E':->47, '%3C%3D':->51, '%3C':->55, ]
         34    > > MATCH_ERROR                                              !7
   49    35    >   FETCH_OBJ_R                                      ~28     !2, !6
         36        IS_IDENTICAL                                     ~29     !8, ~28
         37        QM_ASSIGN                                        ~30     ~29
         38      > JMP                                                      ->59
   50    39    >   FETCH_OBJ_R                                      ~31     !2, !6
         40        IS_EQUAL                                         ~32     !8, ~31
         41        QM_ASSIGN                                        ~30     ~32
         42      > JMP                                                      ->59
   51    43    >   FETCH_OBJ_R                                      ~33     !2, !6
         44        IS_SMALLER_OR_EQUAL                              ~34     !8, ~33
         45        QM_ASSIGN                                        ~30     ~34
         46      > JMP                                                      ->59
   52    47    >   FETCH_OBJ_R                                      ~35     !2, !6
         48        IS_SMALLER                                       ~36     !8, ~35
         49        QM_ASSIGN                                        ~30     ~36
         50      > JMP                                                      ->59
   53    51    >   FETCH_OBJ_R                                      ~37     !2, !6
         52        IS_SMALLER_OR_EQUAL                              ~38     ~37, !8
         53        QM_ASSIGN                                        ~30     ~38
         54      > JMP                                                      ->59
   54    55    >   FETCH_OBJ_R                                      ~39     !2, !6
         56        IS_SMALLER                                       ~40     ~39, !8
         57        QM_ASSIGN                                        ~30     ~40
         58      > JMP                                                      ->59
   48    59    >   ASSIGN                                                   !9, ~30
   58    60      > JMPZ                                                     !9, ->66
   59    61    >   PRE_INC                                                  !4
   63    62        IS_IDENTICAL                                             !4, !3
         63      > JMPZ                                                     ~43, ->66
   64    64    >   FE_FREE                                                  $15
         65      > RETURN                                                   <true>
   30    66    > > JMP                                                      ->12
         67    >   FE_FREE                                                  $15
   70    68        IS_IDENTICAL                                     ~44     !4, !3
         69        VERIFY_RETURN_TYPE                                       ~44
         70      > RETURN                                                   ~44
   71    71*       VERIFY_RETURN_TYPE                                       
         72*     > RETURN                                                   null

End of function process

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.75 ms | 1411 KiB | 14 Q