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 = []) { $ext = pathinfo($filename, PATHINFO_EXTENSION); $this->setExtension($ext); if (empty($replace)) { $replace = [ ' ' => '_', 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss' ]; } $this->basename = self::clean(substr($filename, -strlen($ext)), $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/LpL2t
function name:  (null)
number of ops:  8
compiled vars:  !0 = $file
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  138     0  E >   ASSIGN                                                   !0, 'Versicherungsschein%2FNachtr%C3%A4ge'
  139     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 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/LpL2t
function name:  __construct
number of ops:  26
compiled vars:  !0 = $filename, !1 = $replace, !2 = $ext
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        SEND_VAL                                                 4
          5        DO_ICALL                                         $3      
          6        ASSIGN                                                   !2, $3
   23     7        INIT_METHOD_CALL                                         'setExtension'
          8        SEND_VAR_EX                                              !2
          9        DO_FCALL                                      0          
   25    10        ISSET_ISEMPTY_CV                                         !1
         11      > JMPZ                                                     ~6, ->13
   26    12    >   ASSIGN                                                   !1, <array>
   38    13    >   INIT_STATIC_METHOD_CALL                                  'clean'
         14        INIT_FCALL                                               'substr'
         15        SEND_VAR                                                 !0
         16        STRLEN                                           ~9      !2
         17        MUL                                              ~10     ~9, -1
         18        SEND_VAL                                                 ~10
         19        DO_ICALL                                         $11     
         20        SEND_VAR_NO_REF_EX                                       $11
         21        SEND_VAR_EX                                              !1
         22        DO_FCALL                                      0  $12     
         23        ASSIGN_OBJ                                               'basename'
         24        OP_DATA                                                  $12
   39    25      > 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/LpL2t
function name:  limitLength
number of ops:  20
compiled vars:  !0 = $length
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
   48     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          
   50     7        FETCH_OBJ_R                                      ~3      'basename'
          8        STRLEN                                           ~4      ~3
          9        IS_SMALLER                                               !0, ~4
         10      > JMPZ                                                     ~5, ->19
   51    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
   53    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/LpL2t
function name:  clean
number of ops:  60
compiled vars:  !0 = $str, !1 = $replace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   63     2        INIT_FCALL                                               'trim'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $2      
          5        ASSIGN                                                   !0, $2
   64     6        INIT_FCALL                                               'trim'
          7        SEND_VAR                                                 !0
          8        SEND_VAL                                                 '_-'
          9        DO_ICALL                                         $4      
         10        ASSIGN                                                   !0, $4
   66    11        ISSET_ISEMPTY_CV                                 ~6      !1
         12        BOOL_NOT                                         ~7      ~6
         13      > JMPZ                                                     ~7, ->26
   67    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
   70    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
   72    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
   74    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
   76    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
   78    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
   80    56        VERIFY_RETURN_TYPE                                       !0
         57      > RETURN                                                   !0
   81    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/LpL2t
function name:  getExtension
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   FETCH_OBJ_R                                      ~0      'extension'
          1      > RETURN                                                   ~0
   89     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/LpL2t
function name:  hasExtension
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   96     0  E >   FETCH_OBJ_R                                      ~0      'extension'
          1        TYPE_CHECK                                  1020  ~1      ~0
          2        VERIFY_RETURN_TYPE                                       ~1
          3      > RETURN                                                   ~1
   97     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/LpL2t
function name:  setExtension
number of ops:  8
compiled vars:  !0 = $extension
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   RECV                                             !0      
  104     1        INIT_STATIC_METHOD_CALL                                  'MimeTypes', 'isExtensionSupported'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4      > JMPZ                                                     $1, ->7
  105     5    >   ASSIGN_OBJ                                               'extension'
          6        OP_DATA                                                  !0
  107     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/LpL2t
function name:  getBasename
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  114     0  E >   FETCH_OBJ_R                                      ~0      'basename'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
  115     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/LpL2t
function name:  isValid
number of ops:  12
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  122     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
  123    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/LpL2t
function name:  assemble
number of ops:  20
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  130     0  E >   INIT_METHOD_CALL                                         'hasExtension'
          1        DO_FCALL                                      0  $0      
          2      > JMPZ                                                     $0, ->14
  131     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
  134    14    >   INIT_METHOD_CALL                                         'getBasename'
         15        DO_FCALL                                      0  $4      
         16        VERIFY_RETURN_TYPE                                       $4
         17      > RETURN                                                   $4
  135    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:
279.13 ms | 1412 KiB | 32 Q