3v4l.org

run code in 300+ PHP versions simultaneously
<?php class KeysReplacer { public $default = [ 'id' => 'id', 'full_address' => 'adress', 'floorall' => 'floorall', 'build_year' => 'build_year', ]; public $services = [ 'service_1' => [ 'id' => 'internal_id', 'full_address' => 'locality', 'floorall' => 'floorall', 'build_year' => 'build_date' ], 'service_2' => [ 'id' => 'ид', 'full_address' => 'полный_адрес', 'floorall' => 'этажность', 'build_year' => 'год_постройки' ], ]; public function __invoke($item, $from = null) { $result = []; foreach($item as $key => $value){ $result[$from && isset($this->services[$from][$key]) ? $this->services[$from][$key] : $this->default[$key]] = $value; } return json_encode($result, JSON_UNESCAPED_UNICODE); } } function getItem(){ return [ 'id' => 1, 'full_address' => 'г. Москва', 'floorall' => 18, 'build_year' => 1990 ]; } $item = getItem(); $responseNormalizer = new KeysReplacer; $response_0 = $responseNormalizer($item); $response_1 = $responseNormalizer($item, 'service_1'); $response_2 = $responseNormalizer($item, 'service_2'); var_dump($response_0); // string(68) "{"id":1,"adress":"г. Москва","floorall":18,"build_year":1990}" var_dump($response_1); // string(79) "{"internal_id":1,"locality":"г. Москва","floorall":18,"build_date":1990}" var_dump($response_2); // string(112) "{"ид":1,"полный_адрес":"г. Москва","этажность":18,"год_постройки":1990}"
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sMBFN
function name:  (null)
number of ops:  30
compiled vars:  !0 = $item, !1 = $responseNormalizer, !2 = $response_0, !3 = $response_1, !4 = $response_2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   INIT_FCALL                                               'getitem'
          1        DO_FCALL                                      0  $5      
          2        ASSIGN                                                   !0, $5
   55     3        NEW                                              $7      'KeysReplacer'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $7
   57     6        INIT_DYNAMIC_CALL                                        !1
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $10     
          9        ASSIGN                                                   !2, $10
   58    10        INIT_DYNAMIC_CALL                                        !1
         11        SEND_VAR_EX                                              !0
         12        SEND_VAL_EX                                              'service_1'
         13        DO_FCALL                                      0  $12     
         14        ASSIGN                                                   !3, $12
   59    15        INIT_DYNAMIC_CALL                                        !1
         16        SEND_VAR_EX                                              !0
         17        SEND_VAL_EX                                              'service_2'
         18        DO_FCALL                                      0  $14     
         19        ASSIGN                                                   !4, $14
   61    20        INIT_FCALL                                               'var_dump'
         21        SEND_VAR                                                 !2
         22        DO_ICALL                                                 
   62    23        INIT_FCALL                                               'var_dump'
         24        SEND_VAR                                                 !3
         25        DO_ICALL                                                 
   63    26        INIT_FCALL                                               'var_dump'
         27        SEND_VAR                                                 !4
         28        DO_ICALL                                                 
         29      > RETURN                                                   1

Function getitem:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/sMBFN
function name:  getItem
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E > > RETURN                                                   <array>
   49     1*     > RETURN                                                   null

End of function getitem

Class KeysReplacer:
Function __invoke:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 23
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 23
Branch analysis from position: 5
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 17
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 11
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/sMBFN
function name:  __invoke
number of ops:  30
compiled vars:  !0 = $item, !1 = $from, !2 = $result, !3 = $value, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   29     2        ASSIGN                                                   !2, <array>
   30     3      > FE_RESET_R                                       $6      !0, ->23
          4    > > FE_FETCH_R                                       ~7      $6, !3, ->23
          5    >   ASSIGN                                                   !4, ~7
   31     6      > JMPZ_EX                                          ~9      !1, ->11
          7    >   FETCH_OBJ_IS                                     ~10     'services'
          8        FETCH_DIM_IS                                     ~11     ~10, !1
          9        ISSET_ISEMPTY_DIM_OBJ                         0  ~12     ~11, !4
         10        BOOL                                             ~9      ~12
         11    > > JMPZ                                                     ~9, ->17
   32    12    >   FETCH_OBJ_R                                      ~13     'services'
         13        FETCH_DIM_R                                      ~14     ~13, !1
         14        FETCH_DIM_R                                      ~15     ~14, !4
         15        QM_ASSIGN                                        ~16     ~15
         16      > JMP                                                      ->20
   33    17    >   FETCH_OBJ_R                                      ~17     'default'
         18        FETCH_DIM_R                                      ~18     ~17, !4
         19        QM_ASSIGN                                        ~16     ~18
         20    >   ASSIGN_DIM                                               !2, ~16
         21        OP_DATA                                                  !3
   30    22      > JMP                                                      ->4
         23    >   FE_FREE                                                  $6
   35    24        INIT_FCALL                                               'json_encode'
         25        SEND_VAR                                                 !2
         26        SEND_VAL                                                 256
         27        DO_ICALL                                         $20     
         28      > RETURN                                                   $20
   36    29*     > RETURN                                                   null

End of function __invoke

End of class KeysReplacer.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.41 ms | 1403 KiB | 18 Q