3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Filename { /** * @var string */ private $basename; /** * @var string */ private $extension; /** * Filename constructor. * * @param string $filename * @param array $replace */ public function __construct(string $filename, array $replace = []) { $info = pathinfo($filename); $info = array_map('strtolower', $info); if (array_key_exists('extension', $info)) { $this->setExtension($info['extension']); } if (empty($replace)) { $replace = [ ' ' => '_', 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss' ]; } $this->basename = self::clean($info['filename'], $replace); } /** * @param int $length * * @throws \Exception */ public function limitLength(int $length) { assure($length > 0, 'Invalide Länge: %d', $length); if (strlen($this->basename) > $length) { $this->basename = substr($this->basename, 0, $length); } } /** * @param string $str * @param array $replace * * @return string */ public static function clean(string $str, array $replace = []): string { $str = trim($str); $str = trim($str, '_-'); if (!empty($replace)) { $str = str_replace(array_keys($replace), array_values($replace), $str); } $str = preg_replace('#[^\w\-]+#i', '_', $str); //Mehrere aufeinanderfolgende Unterstriche zu einem Unterstrich $str = preg_replace('#_+#', '_', $str); //Bindestrich vor Unterstrich zum Bindestrich $str = preg_replace('#\-_#', '-', $str); //Bindestrich nach Unterstrich zum Bindestrich $str = preg_replace('#_\-#', '-', $str); //Unterstrich am Ende zu nix $str = preg_replace('#_$#', '', $str); return $str; } /** * @return null|string */ public function getExtension() { return $this->extension; } /** * @return bool */ public function hasExtension(): bool { return $this->extension !== null; } /** * @param string $extension */ public function setExtension(string $extension) { if (MimeTypes::isExtensionSupported($extension)) { $this->extension = $extension; } } /** * @return string */ public function getBasename(): string { return $this->basename; } /** * @return bool */ public function isValid(): bool { return strlen($this->getBasename()) !== 0 && $this->hasExtension(); } /** * @return string */ public function assemble(): string { if ($this->hasExtension()) { return sprintf('%s.%s', $this->getBasename(), $this->getExtension()); } return $this->getBasename(); } } $file = 'Versicherungsschein/Nachträge'; var_dump(new Filename($file));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/W0YNM
function name:  (null)
number of ops:  8
compiled vars:  !0 = $file
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  141     0  E >   ASSIGN                                                   !0, 'Versicherungsschein%2FNachtr%C3%A4ge'
  142     1        INIT_FCALL                                               'var_dump'
          2        NEW                                              $2      'Filename'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Class Filename:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
Branch analysis from position: 18
filename:       /in/W0YNM
function name:  __construct
number of ops:  30
compiled vars:  !0 = $filename, !1 = $replace, !2 = $info
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   22     2        INIT_FCALL                                               'pathinfo'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !2, $3
   23     6        INIT_FCALL                                               'array_map'
          7        SEND_VAL                                                 'strtolower'
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                         $5      
         10        ASSIGN                                                   !2, $5
   24    11        ARRAY_KEY_EXISTS                                         'extension', !2
         12      > JMPZ                                                     ~7, ->18
   25    13    >   INIT_METHOD_CALL                                         'setExtension'
         14        CHECK_FUNC_ARG                                           
         15        FETCH_DIM_FUNC_ARG                               $8      !2, 'extension'
         16        SEND_FUNC_ARG                                            $8
         17        DO_FCALL                                      0          
   28    18    >   ISSET_ISEMPTY_CV                                         !1
         19      > JMPZ                                                     ~10, ->21
   29    20    >   ASSIGN                                                   !1, <array>
   41    21    >   INIT_STATIC_METHOD_CALL                                  'clean'
         22        CHECK_FUNC_ARG                                           
         23        FETCH_DIM_FUNC_ARG                               $13     !2, 'filename'
         24        SEND_FUNC_ARG                                            $13
         25        SEND_VAR_EX                                              !1
         26        DO_FCALL                                      0  $14     
         27        ASSIGN_OBJ                                               'basename'
         28        OP_DATA                                                  $14
   42    29      > RETURN                                                   null

End of function __construct

Function limitlength:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 19
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/W0YNM
function name:  limitLength
number of ops:  20
compiled vars:  !0 = $length
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
   51     1        INIT_FCALL_BY_NAME                                       'assure'
          2        IS_SMALLER                                       ~1      0, !0
          3        SEND_VAL_EX                                              ~1
          4        SEND_VAL_EX                                              'Invalide+L%C3%A4nge%3A+%25d'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
   53     7        FETCH_OBJ_R                                      ~3      'basename'
          8        STRLEN                                           ~4      ~3
          9        IS_SMALLER                                               !0, ~4
         10      > JMPZ                                                     ~5, ->19
   54    11    >   INIT_FCALL                                               'substr'
         12        FETCH_OBJ_R                                      ~7      'basename'
         13        SEND_VAL                                                 ~7
         14        SEND_VAL                                                 0
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $8      
         17        ASSIGN_OBJ                                               'basename'
         18        OP_DATA                                                  $8
   56    19    > > RETURN                                                   null

End of function limitlength

Function clean:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 26
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/W0YNM
function name:  clean
number of ops:  60
compiled vars:  !0 = $str, !1 = $replace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   66     2        INIT_FCALL                                               'trim'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $2      
          5        ASSIGN                                                   !0, $2
   67     6        INIT_FCALL                                               'trim'
          7        SEND_VAR                                                 !0
          8        SEND_VAL                                                 '_-'
          9        DO_ICALL                                         $4      
         10        ASSIGN                                                   !0, $4
   69    11        ISSET_ISEMPTY_CV                                 ~6      !1
         12        BOOL_NOT                                         ~7      ~6
         13      > JMPZ                                                     ~7, ->26
   70    14    >   INIT_FCALL                                               'str_replace'
         15        INIT_FCALL                                               'array_keys'
         16        SEND_VAR                                                 !1
         17        DO_ICALL                                         $8      
         18        SEND_VAR                                                 $8
         19        INIT_FCALL                                               'array_values'
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                         $9      
         22        SEND_VAR                                                 $9
         23        SEND_VAR                                                 !0
         24        DO_ICALL                                         $10     
         25        ASSIGN                                                   !0, $10
   73    26    >   INIT_FCALL                                               'preg_replace'
         27        SEND_VAL                                                 '%23%5B%5E%5Cw%5C-%5D%2B%23i'
         28        SEND_VAL                                                 '_'
         29        SEND_VAR                                                 !0
         30        DO_ICALL                                         $12     
         31        ASSIGN                                                   !0, $12
   75    32        INIT_FCALL                                               'preg_replace'
         33        SEND_VAL                                                 '%23_%2B%23'
         34        SEND_VAL                                                 '_'
         35        SEND_VAR                                                 !0
         36        DO_ICALL                                         $14     
         37        ASSIGN                                                   !0, $14
   77    38        INIT_FCALL                                               'preg_replace'
         39        SEND_VAL                                                 '%23%5C-_%23'
         40        SEND_VAL                                                 '-'
         41        SEND_VAR                                                 !0
         42        DO_ICALL                                         $16     
         43        ASSIGN                                                   !0, $16
   79    44        INIT_FCALL                                               'preg_replace'
         45        SEND_VAL                                                 '%23_%5C-%23'
         46        SEND_VAL                                                 '-'
         47        SEND_VAR                                                 !0
         48        DO_ICALL                                         $18     
         49        ASSIGN                                                   !0, $18
   81    50        INIT_FCALL                                               'preg_replace'
         51        SEND_VAL                                                 '%23_%24%23'
         52        SEND_VAL                                                 ''
         53        SEND_VAR                                                 !0
         54        DO_ICALL                                         $20     
         55        ASSIGN                                                   !0, $20
   83    56        VERIFY_RETURN_TYPE                                       !0
         57      > RETURN                                                   !0
   84    58*       VERIFY_RETURN_TYPE                                       
         59*     > RETURN                                                   null

End of function clean

Function getextension:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/W0YNM
function name:  getExtension
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   FETCH_OBJ_R                                      ~0      'extension'
          1      > RETURN                                                   ~0
   92     2*     > RETURN                                                   null

End of function getextension

Function hasextension:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/W0YNM
function name:  hasExtension
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   99     0  E >   FETCH_OBJ_R                                      ~0      'extension'
          1        TYPE_CHECK                                  1020  ~1      ~0
          2        VERIFY_RETURN_TYPE                                       ~1
          3      > RETURN                                                   ~1
  100     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function hasextension

Function setextension:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/W0YNM
function name:  setExtension
number of ops:  8
compiled vars:  !0 = $extension
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  105     0  E >   RECV                                             !0      
  107     1        INIT_STATIC_METHOD_CALL                                  'MimeTypes', 'isExtensionSupported'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4      > JMPZ                                                     $1, ->7
  108     5    >   ASSIGN_OBJ                                               'extension'
          6        OP_DATA                                                  !0
  110     7    > > RETURN                                                   null

End of function setextension

Function getbasename:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/W0YNM
function name:  getBasename
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  117     0  E >   FETCH_OBJ_R                                      ~0      'basename'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
  118     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function getbasename

Function isvalid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/W0YNM
function name:  isValid
number of ops:  12
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  125     0  E >   INIT_METHOD_CALL                                         'getBasename'
          1        DO_FCALL                                      0  $0      
          2        STRLEN                                           ~1      $0
          3        IS_NOT_IDENTICAL                                 ~2      ~1, 0
          4      > JMPZ_EX                                          ~2      ~2, ->8
          5    >   INIT_METHOD_CALL                                         'hasExtension'
          6        DO_FCALL                                      0  $3      
          7        BOOL                                             ~2      $3
          8    >   VERIFY_RETURN_TYPE                                       ~2
          9      > RETURN                                                   ~2
  126    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function isvalid

Function assemble:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 14
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/W0YNM
function name:  assemble
number of ops:  20
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  133     0  E >   INIT_METHOD_CALL                                         'hasExtension'
          1        DO_FCALL                                      0  $0      
          2      > JMPZ                                                     $0, ->14
  134     3    >   INIT_FCALL                                               'sprintf'
          4        SEND_VAL                                                 '%25s.%25s'
          5        INIT_METHOD_CALL                                         'getBasename'
          6        DO_FCALL                                      0  $1      
          7        SEND_VAR                                                 $1
          8        INIT_METHOD_CALL                                         'getExtension'
          9        DO_FCALL                                      0  $2      
         10        SEND_VAR                                                 $2
         11        DO_ICALL                                         $3      
         12        VERIFY_RETURN_TYPE                                       $3
         13      > RETURN                                                   $3
  137    14    >   INIT_METHOD_CALL                                         'getBasename'
         15        DO_FCALL                                      0  $4      
         16        VERIFY_RETURN_TYPE                                       $4
         17      > RETURN                                                   $4
  138    18*       VERIFY_RETURN_TYPE                                       
         19*     > RETURN                                                   null

End of function assemble

End of class Filename.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
174.34 ms | 1412 KiB | 33 Q