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) { if (isset($options[$name])) { $optionLength = IntFilter::check($options[$name], ['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 ', ['min' => 5])); var_dump(StringFilter::check(' r a ', ['max' => 3])); var_dump(StringFilter::check(' r a ', ['min' => 5, 'max' => 4])); var_dump(StringFilter::check(' r a ', ['min' => 4, 'max' => 3])); var_dump(StringFilter::check(' r a ', ['min' => 5, 'max' => 3])); 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/3XP1W
function name:  (null)
number of ops:  134
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     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                                                 
   69     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                                                 
   70    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                                                 
   72    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                                                 
   73    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                                                 
   74    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                                                 
   78    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                                                 
   82    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                                                 
   83    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                                                 
   84    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                                                 
   86    98        INIT_FCALL                                               'var_dump'
         99        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
        100        SEND_VAL                                                 '++r++a+++'
        101        SEND_VAL                                                 <array>
        102        DO_FCALL                                      0  $28     
        103        SEND_VAR                                                 $28
        104        DO_ICALL                                                 
   87   105        INIT_FCALL                                               'var_dump'
        106        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
        107        SEND_VAL                                                 '++r++a+++'
        108        SEND_VAL                                                 <array>
        109        DO_FCALL                                      0  $30     
        110        SEND_VAR                                                 $30
        111        DO_ICALL                                                 
   88   112        INIT_FCALL                                               'var_dump'
        113        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
        114        SEND_VAL                                                 '++r++a+++'
        115        SEND_VAL                                                 <array>
        116        DO_FCALL                                      0  $32     
        117        SEND_VAR                                                 $32
        118        DO_ICALL                                                 
   89   119        INIT_FCALL                                               'var_dump'
        120        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
        121        SEND_VAL                                                 '++r++a+++'
        122        SEND_VAL                                                 <array>
        123        DO_FCALL                                      0  $34     
        124        SEND_VAR                                                 $34
        125        DO_ICALL                                                 
   90   126        INIT_FCALL                                               'var_dump'
        127        INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
        128        SEND_VAL                                                 '++r++a+++'
        129        SEND_VAL                                                 <array>
        130        DO_FCALL                                      0  $36     
        131        SEND_VAR                                                 $36
        132        DO_ICALL                                                 
        133      > RETURN                                                   1

Class Filter:
Function check:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3XP1W
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/3XP1W
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 = 53
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 53
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 52
Branch analysis from position: 33
2 jumps found. (Code = 46) Position 1 = 41, Position 2 = 43
Branch analysis from position: 41
2 jumps found. (Code = 47) Position 1 = 44, Position 2 = 49
Branch analysis from position: 44
2 jumps found. (Code = 46) Position 1 = 46, Position 2 = 48
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 52
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 48
Branch analysis from position: 49
Branch analysis from position: 43
Branch analysis from position: 52
Branch analysis from position: 53
2 jumps found. (Code = 77) Position 1 = 55, Position 2 = 72
Branch analysis from position: 55
2 jumps found. (Code = 78) Position 1 = 56, Position 2 = 72
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 71
Branch analysis from position: 58
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 71
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 71
1 jumps found. (Code = 42) Position 1 = 55
Branch analysis from position: 55
Branch analysis from position: 71
Branch analysis from position: 72
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 72
Branch analysis from position: 53
Branch analysis from position: 27
Branch analysis from position: 15
filename:       /in/3XP1W
function name:  check
number of ops:  75
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>, ->53
         30    > > FE_FETCH_R                                               $19, !3, ->53
   35    31    >   ISSET_ISEMPTY_DIM_OBJ                         0          !1, !3
         32      > JMPZ                                                     ~20, ->52
   36    33    >   INIT_STATIC_METHOD_CALL                                  'IntFilter', 'check'
         34        FETCH_DIM_R                                      ~21     !1, !3
         35        SEND_VAL                                                 ~21
         36        SEND_VAL                                                 <array>
         37        DO_FCALL                                      0  $22     
         38        ASSIGN                                                   !4, $22
   38    39        IS_IDENTICAL                                     ~24     !3, 'min'
         40      > JMPZ_EX                                          ~24     ~24, ->43
         41    >   IS_SMALLER                                       ~25     !2, !4
         42        BOOL                                             ~24     ~25
         43    > > JMPNZ_EX                                         ~24     ~24, ->49
         44    >   IS_IDENTICAL                                     ~26     !3, 'max'
         45      > JMPZ_EX                                          ~26     ~26, ->48
         46    >   IS_SMALLER                                       ~27     !4, !2
         47        BOOL                                             ~26     ~27
         48    >   BOOL                                             ~24     ~26
         49    > > JMPZ                                                     ~24, ->52
   39    50    >   FE_FREE                                                  $19
         51      > RETURN                                                   <false>
   34    52    > > JMP                                                      ->30
         53    >   FE_FREE                                                  $19
   44    54      > FE_RESET_R                                       $28     <array>, ->72
         55    > > FE_FETCH_R                                               $28, !3, ->72
   45    56    >   ISSET_ISEMPTY_DIM_OBJ                         0          !1, !3
         57      > JMPZ                                                     ~29, ->71
   46    58    >   INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         59        FETCH_DIM_R                                      ~30     !1, !3
         60        SEND_VAL                                                 ~30
         61        DO_FCALL                                      0  $31     
         62        ASSIGN                                                   !5, $31
   48    63        INIT_STATIC_METHOD_CALL                                  !3
         64        SEND_VAR_EX                                              !0
         65        SEND_VAR_EX                                              !5
         66        DO_FCALL                                      0  $33     
         67        TYPE_CHECK                                    4          $33
         68      > JMPZ                                                     ~34, ->71
   49    69    >   FE_FREE                                                  $28
         70      > RETURN                                                   <false>
   44    71    > > JMP                                                      ->55
         72    >   FE_FREE                                                  $28
   54    73      > RETURN                                                   !0
   55    74*     > 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/3XP1W
function name:  startsWith
number of ops:  21
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   59     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
   60    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/3XP1W
function name:  endsWith
number of ops:  17
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   64     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
   65    16*     > RETURN                                                   null

End of function endswith

End of class StringFilter.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
196.27 ms | 1420 KiB | 23 Q