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 (['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 ', ['startsWith' => 'r'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'z'])); var_dump(StringFilter::check(' r a ', ['endsWith' => 'a'])); var_dump(StringFilter::check(' r a ', ['endsWith' => 'z'])); var_dump(StringFilter::check(' r a ', ['startsWith' => 'r', 'endsWith' => 'a'])); 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/Q0euV
function name:  (null)
number of ops:  57
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     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                                                 
   59     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                                                 
   60    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                                                 
   61    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                                                 
   63    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                                                 
   64    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                                                 
   65    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                                                 
   66    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                                                 
         56      > RETURN                                                   1

Class Filter:
Function check:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q0euV
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/Q0euV
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 = 47
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 47
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 46
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 46
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 46
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
Branch analysis from position: 27
Branch analysis from position: 15
filename:       /in/Q0euV
function name:  check
number of ops:  50
compiled vars:  !0 = $value, !1 = $options, !2 = $length, !3 = $name, !4 = $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  ~5      !0
          3        TYPE_CHECK                                    4          ~5
          4      > JMPZ                                                     ~6, ->6
   21     5    > > RETURN                                                   <false>
   24     6    >   FETCH_DIM_IS                                     ~7      !1, 'trim'
          7        COALESCE                                         ~8      ~7
          8        QM_ASSIGN                                        ~8      <true>
          9        TYPE_CHECK                                  1018          ~8
         10      > JMPZ                                                     ~9, ->15
   25    11    >   INIT_FCALL                                               'trim'
         12        SEND_VAR                                                 !0
         13        DO_ICALL                                         $10     
         14        ASSIGN                                                   !0, $10
   28    15    >   INIT_FCALL                                               'mb_strlen'
         16        SEND_VAR                                                 !0
         17        SEND_VAL                                                 '8bit'
         18        DO_ICALL                                         $12     
         19        ASSIGN                                                   !2, $12
   30    20        FETCH_DIM_IS                                     ~14     !1, 'empty'
         21        COALESCE                                         ~15     ~14
         22        QM_ASSIGN                                        ~15     <true>
         23        TYPE_CHECK                                    4  ~16     ~15
         24      > JMPZ_EX                                          ~16     ~16, ->27
         25    >   IS_IDENTICAL                                     ~17     !2, 0
         26        BOOL                                             ~16     ~17
         27    > > JMPZ                                                     ~16, ->29
   31    28    > > RETURN                                                   <false>
   34    29    > > FE_RESET_R                                       $18     <array>, ->47
         30    > > FE_FETCH_R                                               $18, !3, ->47
   35    31    >   ISSET_ISEMPTY_DIM_OBJ                         0          !1, !3
         32      > JMPZ                                                     ~19, ->46
   36    33    >   INIT_STATIC_METHOD_CALL                                  'StringFilter', 'check'
         34        FETCH_DIM_R                                      ~20     !1, !3
         35        SEND_VAL                                                 ~20
         36        DO_FCALL                                      0  $21     
         37        ASSIGN                                                   !4, $21
   38    38        INIT_STATIC_METHOD_CALL                                  !3
         39        SEND_VAR_EX                                              !0
         40        SEND_VAR_EX                                              !4
         41        DO_FCALL                                      0  $23     
         42        TYPE_CHECK                                    4          $23
         43      > JMPZ                                                     ~24, ->46
   39    44    >   FE_FREE                                                  $18
         45      > RETURN                                                   <false>
   34    46    > > JMP                                                      ->30
         47    >   FE_FREE                                                  $18
   44    48      > RETURN                                                   !0
   45    49*     > 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/Q0euV
function name:  startsWith
number of ops:  21
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   49     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
   50    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/Q0euV
function name:  endsWith
number of ops:  17
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   54     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
   55    16*     > RETURN                                                   null

End of function endswith

End of class StringFilter.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.47 ms | 1412 KiB | 23 Q