3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Filter { abstract public static function check($value, array $options = array()); } final class IntFilter { public static function check($value, array $options = array()) { return $value; } } final class StringFilter { public static function check($value, array $options = array()) { if (false === is_string($value)) { return false; } if (false !== ($options['trim'] ?? true)) { $value = trim($value); } $length = mb_strlen($value, '8bit'); if ((false === ($options['empty'] ?? true)) && (0 === $length)) { return false; } foreach (['min', 'max'] as $name) { $optionLength = IntFilter::check($options['min'], ['min' => 1]); if (('min' === $name && $length < $optionLength) || ('max' === $name && $length > $optionLength)) { return false; } } foreach (['startsWith', 'endsWith'] as $name) { if (isset($options[$name])) { $needle = StringFilter::check($options[$name]); if (false === self::$name($value, $needle)) { return false; } } } return $value; } private static function startsWith($haystack, $needle) { return $haystack[0] === $needle[0] ? strncmp($haystack, $needle, mb_strlen($needle, '8bit')) === 0 : false; } private static function endsWith($haystack, $needle) { return self::startsWith(mb_substr($haystack, -mb_strlen($needle, '8bit')), $needle); } } var_dump(StringFilter::check(' r a ', ['min' => 4])); var_dump(StringFilter::check(' r a ', ['max' => 4])); var_dump(StringFilter::check(' r a ', ['min' => 4, 'max' => 4])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r'])); var_dump(StringFilter::check(' r a ', ['endsWith' => 'a'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r', 'endsWith' => 'a'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r at'])); var_dump(StringFilter::check(' r a ', ['endsWith' => 'r at'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r at', 'endsWith' => 'r at'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'z'])); var_dump(StringFilter::check(' r a ', ['endsWith' => 'z'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r', 'endsWith' => 'z'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'z', 'endsWith' => 'a'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'z', 'endsWith' => 'y']));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/L7In7
function name:  (null)
number of ops:  99
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
          2        SEND_VAL                                                 '++r++a+++'
          3        SEND_VAL                                                 <array>
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
   68     7        INIT_FCALL                                               'var_dump'
          8        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
          9        SEND_VAL                                                 '++r++a+++'
         10        SEND_VAL                                                 <array>
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                                 
   69    14        INIT_FCALL                                               'var_dump'
         15        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         16        SEND_VAL                                                 '++r++a+++'
         17        SEND_VAL                                                 <array>
         18        DO_FCALL                                      0  $4      
         19        SEND_VAR                                                 $4
         20        DO_ICALL                                                 
   71    21        INIT_FCALL                                               'var_dump'
         22        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         23        SEND_VAL                                                 '++r++a+++'
         24        SEND_VAL                                                 <array>
         25        DO_FCALL                                      0  $6      
         26        SEND_VAR                                                 $6
         27        DO_ICALL                                                 
   72    28        INIT_FCALL                                               'var_dump'
         29        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         30        SEND_VAL                                                 '++r++a+++'
         31        SEND_VAL                                                 <array>
         32        DO_FCALL                                      0  $8      
         33        SEND_VAR                                                 $8
         34        DO_ICALL                                                 
   73    35        INIT_FCALL                                               'var_dump'
         36        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         37        SEND_VAL                                                 '++r++a+++'
         38        SEND_VAL                                                 <array>
         39        DO_FCALL                                      0  $10     
         40        SEND_VAR                                                 $10
         41        DO_ICALL                                                 
   75    42        INIT_FCALL                                               'var_dump'
         43        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         44        SEND_VAL                                                 '++r++a+++'
         45        SEND_VAL                                                 <array>
         46        DO_FCALL                                      0  $12     
         47        SEND_VAR                                                 $12
         48        DO_ICALL                                                 
   76    49        INIT_FCALL                                               'var_dump'
         50        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         51        SEND_VAL                                                 '++r++a+++'
         52        SEND_VAL                                                 <array>
         53        DO_FCALL                                      0  $14     
         54        SEND_VAR                                                 $14
         55        DO_ICALL                                                 
   77    56        INIT_FCALL                                               'var_dump'
         57        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         58        SEND_VAL                                                 '++r++a+++'
         59        SEND_VAL                                                 <array>
         60        DO_FCALL                                      0  $16     
         61        SEND_VAR                                                 $16
         62        DO_ICALL                                                 
   79    63        INIT_FCALL                                               'var_dump'
         64        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         65        SEND_VAL                                                 '++r++a+++'
         66        SEND_VAL                                                 <array>
         67        DO_FCALL                                      0  $18     
         68        SEND_VAR                                                 $18
         69        DO_ICALL                                                 
   80    70        INIT_FCALL                                               'var_dump'
         71        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         72        SEND_VAL                                                 '++r++a+++'
         73        SEND_VAL                                                 <array>
         74        DO_FCALL                                      0  $20     
         75        SEND_VAR                                                 $20
         76        DO_ICALL                                                 
   81    77        INIT_FCALL                                               'var_dump'
         78        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         79        SEND_VAL                                                 '++r++a+++'
         80        SEND_VAL                                                 <array>
         81        DO_FCALL                                      0  $22     
         82        SEND_VAR                                                 $22
         83        DO_ICALL                                                 
   82    84        INIT_FCALL                                               'var_dump'
         85        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         86        SEND_VAL                                                 '++r++a+++'
         87        SEND_VAL                                                 <array>
         88        DO_FCALL                                      0  $24     
         89        SEND_VAR                                                 $24
         90        DO_ICALL                                                 
   83    91        INIT_FCALL                                               'var_dump'
         92        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         93        SEND_VAL                                                 '++r++a+++'
         94        SEND_VAL                                                 <array>
         95        DO_FCALL                                      0  $26     
         96        SEND_VAR                                                 $26
         97        DO_ICALL                                                 
         98      > RETURN                                                   1

Class Filter:
Function check:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/L7In7
function name:  check
number of ops:  3
compiled vars:  !0 = $value, !1 = $options
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2      > RETURN                                                   null

End of function check

End of class Filter.

Class IntFilter:
Function check:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/L7In7
function name:  check
number of ops:  4
compiled vars:  !0 = $value, !1 = $options
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   12     2      > RETURN                                                   !0
   13     3*     > RETURN                                                   null

End of function check

End of class IntFilter.

Class StringFilter:
Function check:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
2 jumps found. (Code = 46) Position 1 = 25, Position 2 = 27
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 29
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 51
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 51
Branch analysis from position: 31
2 jumps found. (Code = 46) Position 1 = 39, Position 2 = 41
Branch analysis from position: 39
2 jumps found. (Code = 47) Position 1 = 42, Position 2 = 47
Branch analysis from position: 42
2 jumps found. (Code = 46) Position 1 = 44, Position 2 = 46
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 48, Position 2 = 50
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 46
Branch analysis from position: 47
Branch analysis from position: 41
Branch analysis from position: 51
2 jumps found. (Code = 77) Position 1 = 53, Position 2 = 70
Branch analysis from position: 53
2 jumps found. (Code = 78) Position 1 = 54, Position 2 = 70
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 69
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 69
Branch analysis from position: 67
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
Branch analysis from position: 69
Branch analysis from position: 70
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 70
Branch analysis from position: 51
Branch analysis from position: 27
Branch analysis from position: 15
filename:       /in/L7In7
function name:  check
number of ops:  73
compiled vars:  !0 = $value, !1 = $options, !2 = $length, !3 = $name, !4 = $optionLength, !5 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   20     2        TYPE_CHECK                                   64  ~6      !0
          3        TYPE_CHECK                                    4          ~6
          4      > JMPZ                                                     ~7, ->6
   21     5    > > RETURN                                                   <false>
   24     6    >   FETCH_DIM_IS                                     ~8      !1, 'trim'
          7        COALESCE                                         ~9      ~8
          8        QM_ASSIGN                                        ~9      <true>
          9        TYPE_CHECK                                  1018          ~9
         10      > JMPZ                                                     ~10, ->15
   25    11    >   INIT_FCALL                                               'trim'
         12        SEND_VAR                                                 !0
         13        DO_ICALL                                         $11     
         14        ASSIGN                                                   !0, $11
   28    15    >   INIT_FCALL                                               'mb_strlen'
         16        SEND_VAR                                                 !0
         17        SEND_VAL                                                 '8bit'
         18        DO_ICALL                                         $13     
         19        ASSIGN                                                   !2, $13
   30    20        FETCH_DIM_IS                                     ~15     !1, 'empty'
         21        COALESCE                                         ~16     ~15
         22        QM_ASSIGN                                        ~16     <true>
         23        TYPE_CHECK                                    4  ~17     ~16
         24      > JMPZ_EX                                          ~17     ~17, ->27
         25    >   IS_IDENTICAL                                     ~18     !2, 0
         26        BOOL                                             ~17     ~18
         27    > > JMPZ                                                     ~17, ->29
   31    28    > > RETURN                                                   <false>
   34    29    > > FE_RESET_R                                       $19     <array>, ->51
         30    > > FE_FETCH_R                                               $19, !3, ->51
   35    31    >   INIT_STATIC_METHOD_CALL                                  'IntFilter', 'check'
         32        FETCH_DIM_R                                      ~20     !1, 'min'
         33        SEND_VAL                                                 ~20
         34        SEND_VAL                                                 <array>
         35        DO_FCALL                                      0  $21     
         36        ASSIGN                                                   !4, $21
   37    37        IS_IDENTICAL                                     ~23     !3, 'min'
         38      > JMPZ_EX                                          ~23     ~23, ->41
         39    >   IS_SMALLER                                       ~24     !2, !4
         40        BOOL                                             ~23     ~24
         41    > > JMPNZ_EX                                         ~23     ~23, ->47
         42    >   IS_IDENTICAL                                     ~25     !3, 'max'
         43      > JMPZ_EX                                          ~25     ~25, ->46
         44    >   IS_SMALLER                                       ~26     !4, !2
         45        BOOL                                             ~25     ~26
         46    >   BOOL                                             ~23     ~25
         47    > > JMPZ                                                     ~23, ->50
   38    48    >   FE_FREE                                                  $19
         49      > RETURN                                                   <false>
   34    50    > > JMP                                                      ->30
         51    >   FE_FREE                                                  $19
   43    52      > FE_RESET_R                                       $27     <array>, ->70
         53    > > FE_FETCH_R                                               $27, !3, ->70
   44    54    >   ISSET_ISEMPTY_DIM_OBJ                         0          !1, !3
         55      > JMPZ                                                     ~28, ->69
   45    56    >   INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         57        FETCH_DIM_R                                      ~29     !1, !3
         58        SEND_VAL                                                 ~29
         59        DO_FCALL                                      0  $30     
         60        ASSIGN                                                   !5, $30
   47    61        INIT_STATIC_METHOD_CALL                                  !3
         62        SEND_VAR_EX                                              !0
         63        SEND_VAR_EX                                              !5
         64        DO_FCALL                                      0  $32     
         65        TYPE_CHECK                                    4          $32
         66      > JMPZ                                                     ~33, ->69
   48    67    >   FE_FREE                                                  $27
         68      > RETURN                                                   <false>
   43    69    > > JMP                                                      ->53
         70    >   FE_FREE                                                  $27
   53    71      > RETURN                                                   !0
   54    72*     > RETURN                                                   null

End of function check

Function startswith:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 18
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/L7In7
function name:  startsWith
number of ops:  21
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   58     2        FETCH_DIM_R                                      ~2      !0, 0
          3        FETCH_DIM_R                                      ~3      !1, 0
          4        IS_IDENTICAL                                             ~2, ~3
          5      > JMPZ                                                     ~4, ->18
          6    >   INIT_FCALL                                               'strncmp'
          7        SEND_VAR                                                 !0
          8        SEND_VAR                                                 !1
          9        INIT_FCALL                                               'mb_strlen'
         10        SEND_VAR                                                 !1
         11        SEND_VAL                                                 '8bit'
         12        DO_ICALL                                         $5      
         13        SEND_VAR                                                 $5
         14        DO_ICALL                                         $6      
         15        IS_IDENTICAL                                     ~7      $6, 0
         16        QM_ASSIGN                                        ~8      ~7
         17      > JMP                                                      ->19
         18    >   QM_ASSIGN                                        ~8      <false>
         19    > > RETURN                                                   ~8
   59    20*     > RETURN                                                   null

End of function startswith

Function endswith:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/L7In7
function name:  endsWith
number of ops:  17
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   63     2        INIT_STATIC_METHOD_CALL                                  'startsWith'
          3        INIT_FCALL                                               'mb_substr'
          4        SEND_VAR                                                 !0
          5        INIT_FCALL                                               'mb_strlen'
          6        SEND_VAR                                                 !1
          7        SEND_VAL                                                 '8bit'
          8        DO_ICALL                                         $2      
          9        MUL                                              ~3      $2, -1
         10        SEND_VAL                                                 ~3
         11        DO_ICALL                                         $4      
         12        SEND_VAR                                                 $4
         13        SEND_VAR                                                 !1
         14        DO_FCALL                                      0  $5      
         15      > RETURN                                                   $5
   64    16*     > RETURN                                                   null

End of function endswith

End of class StringFilter.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.3 ms | 1416 KiB | 23 Q