3v4l.org

run code in 300+ PHP versions simultaneously
<?php class UriLanguageResolver { private $languages; private $defaultLanguage; private $uriBase; private $regex; public function __construct(array $languages, string $defaultLanguage, string $uriBase = '') { if (!\in_array($defaultLanguage, $languages)) { throw new \InvalidArgumentException('Default language not present in supported languages'); } $this->languages = $languages; $this->defaultLanguage = $defaultLanguage; $this->uriBase = $uriBase; $this->regex = '(^(' . \preg_quote($this->uriBase) . ')/(' . \implode('|', $languages) . ')(?=/|$)(.*))'; } public function resolve(string $uri): UriWithLanguage { $language = $this->defaultLanguage; if (\preg_match($this->regex, $uri, $match)) { $language = $match[2]; $uri = $match[1] . $match[3]; } return new UriWithLanguage($language, $uri); } } class UriWithLanguage { public $language; public $uri; public function __construct($language, $uri) { $this->language = $language; $this->uri = $uri; } } $resolver = new UriLanguageResolver(['en', 'fr', 'de', 'nl'], 'en', '/cms'); $tests = [ '/cms/articles/foo', '/cms/en/articles/foo', '/cms/fr/articles/foo', ]; foreach ($tests as $test) { $parts = $resolver->resolve($test); echo " Input URI: {$test} Language: {$parts->language} Routed URI: {$parts->uri} "; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 24
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 24
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/BTrj0
function name:  (null)
number of ops:  26
compiled vars:  !0 = $resolver, !1 = $tests, !2 = $test, !3 = $parts
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   NEW                                              $4      'UriLanguageResolver'
          1        SEND_VAL_EX                                              <array>
          2        SEND_VAL_EX                                              'en'
          3        SEND_VAL_EX                                              '%2Fcms'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $4
   49     6        ASSIGN                                                   !1, <array>
   55     7      > FE_RESET_R                                       $8      !1, ->24
          8    > > FE_FETCH_R                                               $8, !2, ->24
   56     9    >   INIT_METHOD_CALL                                         !0, 'resolve'
         10        SEND_VAR_EX                                              !2
         11        DO_FCALL                                      0  $9      
         12        ASSIGN                                                   !3, $9
   58    13        ROPE_INIT                                     7  ~14     '%0AInput+URI%3A+'
   59    14        ROPE_ADD                                      1  ~14     ~14, !2
         15        ROPE_ADD                                      2  ~14     ~14, '%0A++Language%3A+++'
   60    16        FETCH_OBJ_R                                      ~11     !3, 'language'
         17        ROPE_ADD                                      3  ~14     ~14, ~11
         18        ROPE_ADD                                      4  ~14     ~14, '%0A++Routed+URI%3A+'
   61    19        FETCH_OBJ_R                                      ~12     !3, 'uri'
         20        ROPE_ADD                                      5  ~14     ~14, ~12
         21        ROPE_END                                      6  ~13     ~14, '%0A'
         22        ECHO                                                     ~13
   55    23      > JMP                                                      ->8
         24    >   FE_FREE                                                  $8
   63    25      > RETURN                                                   1

Class UriLanguageResolver:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BTrj0
function name:  __construct
number of ops:  34
compiled vars:  !0 = $languages, !1 = $defaultLanguage, !2 = $uriBase
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      ''
   12     3        INIT_FCALL                                               'in_array'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $3      
          7        BOOL_NOT                                         ~4      $3
          8      > JMPZ                                                     ~4, ->13
   13     9    >   NEW                                              $5      'InvalidArgumentException'
         10        SEND_VAL_EX                                              'Default+language+not+present+in+supported+languages'
         11        DO_FCALL                                      0          
         12      > THROW                                         0          $5
   16    13    >   ASSIGN_OBJ                                               'languages'
         14        OP_DATA                                                  !0
   17    15        ASSIGN_OBJ                                               'defaultLanguage'
         16        OP_DATA                                                  !1
   18    17        ASSIGN_OBJ                                               'uriBase'
         18        OP_DATA                                                  !2
   19    19        INIT_FCALL                                               'preg_quote'
         20        FETCH_OBJ_R                                      ~11     'uriBase'
         21        SEND_VAL                                                 ~11
         22        DO_ICALL                                         $12     
         23        CONCAT                                           ~13     '%28%5E%28', $12
         24        CONCAT                                           ~14     ~13, '%29%2F%28'
         25        INIT_FCALL                                               'implode'
         26        SEND_VAL                                                 '%7C'
         27        SEND_VAR                                                 !0
         28        DO_ICALL                                         $15     
         29        CONCAT                                           ~16     ~14, $15
         30        CONCAT                                           ~17     ~16, '%29%28%3F%3D%2F%7C%24%29%28.%2A%29%29'
         31        ASSIGN_OBJ                                               'regex'
         32        OP_DATA                                                  ~17
   20    33      > RETURN                                                   null

End of function __construct

Function resolve:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/BTrj0
function name:  resolve
number of ops:  24
compiled vars:  !0 = $uri, !1 = $language, !2 = $match
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   24     1        FETCH_OBJ_R                                      ~3      'defaultLanguage'
          2        ASSIGN                                                   !1, ~3
   26     3        INIT_FCALL                                               'preg_match'
          4        FETCH_OBJ_R                                      ~5      'regex'
          5        SEND_VAL                                                 ~5
          6        SEND_VAR                                                 !0
          7        SEND_REF                                                 !2
          8        DO_ICALL                                         $6      
          9      > JMPZ                                                     $6, ->16
   27    10    >   FETCH_DIM_R                                      ~7      !2, 2
         11        ASSIGN                                                   !1, ~7
   28    12        FETCH_DIM_R                                      ~9      !2, 1
         13        FETCH_DIM_R                                      ~10     !2, 3
         14        CONCAT                                           ~11     ~9, ~10
         15        ASSIGN                                                   !0, ~11
   31    16    >   NEW                                              $13     'UriWithLanguage'
         17        SEND_VAR_EX                                              !1
         18        SEND_VAR_EX                                              !0
         19        DO_FCALL                                      0          
         20        VERIFY_RETURN_TYPE                                       $13
         21      > RETURN                                                   $13
   32    22*       VERIFY_RETURN_TYPE                                       
         23*     > RETURN                                                   null

End of function resolve

End of class UriLanguageResolver.

Class UriWithLanguage:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BTrj0
function name:  __construct
number of ops:  7
compiled vars:  !0 = $language, !1 = $uri
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   42     2        ASSIGN_OBJ                                               'language'
          3        OP_DATA                                                  !0
   43     4        ASSIGN_OBJ                                               'uri'
          5        OP_DATA                                                  !1
   44     6      > RETURN                                                   null

End of function __construct

End of class UriWithLanguage.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.55 ms | 1014 KiB | 17 Q