3v4l.org

run code in 300+ PHP versions simultaneously
<?php class test{ protected $id; protected $name; public function getId(){ return $this->id; } public function setId($id){ $this->id = $id; return $this; } public function getName(){ return $this->name; } public function setName($name){ $this->name = $name; return $this; } } class AbstractDbToMapper { /** * @var array */ protected $fields = [ 'new_spec_name' => 'name', 'object_id' => 'id' ]; /** * @param $data * * @return mixed */ public function mapFromDbData($data, $object) { $arrayKeys = array_keys($data); if(empty($arrayKeys)){ return $object; } foreach ($arrayKeys as $key){ if(empty($key)){ continue; } if(!array_key_exists($key,$this->fields)){ continue; } $field = $this->fields[$key]; $method = sprintf('set%s', ucfirst($field)); if(!method_exists($object,$method)){ continue; } call_user_func([$object, $method,$data[$field]]); } return $object; } /** * @param $object * * @return mixed */ public function mapToDbData($object) { $objectFields = array_map( function ($field) { return lcfirst(substr($field, 3)); }, preg_grep('/^get/', get_class_methods($object)) ); $resultArray = []; foreach ($objectFields as $field) { if (!in_array($field, $this->fields)) { continue; } $key = array_search($field,$this->fields); if (empty($key)) { continue; } $method = sprintf('get%s', ucfirst($field)); $resultArray[$key] = call_user_func([$object, $method]); } return $resultArray; } } $array = [ 'object_id' => 4, 'new_spec_name' => 'Супер пупер имя', ]; $test = new test(); $mapper = new AbstractDbToMapper(); var_dump($mapper->mapFromDbData($array,$test)); var_dump($mapper->mapToDbData($test));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Z7hKd
function name:  (null)
number of ops:  21
compiled vars:  !0 = $array, !1 = $test, !2 = $mapper
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  105     0  E >   ASSIGN                                                   !0, <array>
  110     1        NEW                                              $4      'test'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $4
  112     4        NEW                                              $7      'AbstractDbToMapper'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !2, $7
  114     7        INIT_FCALL                                               'var_dump'
          8        INIT_METHOD_CALL                                         !2, 'mapFromDbData'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0  $10     
         12        SEND_VAR                                                 $10
         13        DO_ICALL                                                 
  115    14        INIT_FCALL                                               'var_dump'
         15        INIT_METHOD_CALL                                         !2, 'mapToDbData'
         16        SEND_VAR_EX                                              !1
         17        DO_FCALL                                      0  $12     
         18        SEND_VAR                                                 $12
         19        DO_ICALL                                                 
         20      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FZ7hKd%3A81%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Z7hKd
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $field
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV                                             !0      
   82     1        INIT_FCALL                                               'lcfirst'
          2        INIT_FCALL                                               'substr'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 3
          5        DO_ICALL                                         $1      
          6        SEND_VAR                                                 $1
          7        DO_ICALL                                         $2      
          8      > RETURN                                                   $2
   83     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FZ7hKd%3A81%240

Class test:
Function getid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Z7hKd
function name:  getId
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   FETCH_OBJ_R                                      ~0      'id'
          1      > RETURN                                                   ~0
   10     2*     > RETURN                                                   null

End of function getid

Function setid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Z7hKd
function name:  setId
number of ops:  6
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   13     1        ASSIGN_OBJ                                               'id'
          2        OP_DATA                                                  !0
   15     3        FETCH_THIS                                       ~2      
          4      > RETURN                                                   ~2
   16     5*     > RETURN                                                   null

End of function setid

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

End of function getname

Function setname:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Z7hKd
function name:  setName
number of ops:  6
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   23     1        ASSIGN_OBJ                                               'name'
          2        OP_DATA                                                  !0
   25     3        FETCH_THIS                                       ~2      
          4      > RETURN                                                   ~2
   26     5*     > RETURN                                                   null

End of function setname

End of class test.

Class AbstractDbToMapper:
Function mapfromdbdata:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 44
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 44
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 14
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 19
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 37
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
filename:       /in/Z7hKd
function name:  mapFromDbData
number of ops:  47
compiled vars:  !0 = $data, !1 = $object, !2 = $arrayKeys, !3 = $key, !4 = $field, !5 = $method
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   46     2        INIT_FCALL                                               'array_keys'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !2, $6
   47     6        ISSET_ISEMPTY_CV                                         !2
          7      > JMPZ                                                     ~8, ->9
   48     8    > > RETURN                                                   !1
   51     9    > > FE_RESET_R                                       $9      !2, ->44
         10    > > FE_FETCH_R                                               $9, !3, ->44
   52    11    >   ISSET_ISEMPTY_CV                                         !3
         12      > JMPZ                                                     ~10, ->14
   53    13    > > JMP                                                      ->10
   56    14    >   FETCH_OBJ_R                                      ~11     'fields'
         15        ARRAY_KEY_EXISTS                                 ~12     !3, ~11
         16        BOOL_NOT                                         ~13     ~12
         17      > JMPZ                                                     ~13, ->19
   57    18    > > JMP                                                      ->10
   60    19    >   FETCH_OBJ_R                                      ~14     'fields'
         20        FETCH_DIM_R                                      ~15     ~14, !3
         21        ASSIGN                                                   !4, ~15
   61    22        INIT_FCALL                                               'sprintf'
         23        SEND_VAL                                                 'set%25s'
         24        INIT_FCALL                                               'ucfirst'
         25        SEND_VAR                                                 !4
         26        DO_ICALL                                         $17     
         27        SEND_VAR                                                 $17
         28        DO_ICALL                                         $18     
         29        ASSIGN                                                   !5, $18
   63    30        INIT_FCALL                                               'method_exists'
         31        SEND_VAR                                                 !1
         32        SEND_VAR                                                 !5
         33        DO_ICALL                                         $20     
         34        BOOL_NOT                                         ~21     $20
         35      > JMPZ                                                     ~21, ->37
   64    36    > > JMP                                                      ->10
   67    37    >   INIT_ARRAY                                       ~22     !1
         38        ADD_ARRAY_ELEMENT                                ~22     !5
         39        FETCH_DIM_R                                      ~23     !0, !4
         40        ADD_ARRAY_ELEMENT                                ~22     ~23
         41        INIT_USER_CALL                                0          'call_user_func', ~22
         42        DO_FCALL                                      0          
   51    43      > JMP                                                      ->10
         44    >   FE_FREE                                                  $9
   70    45      > RETURN                                                   !1
   71    46*     > RETURN                                                   null

End of function mapfromdbdata

Function maptodbdata:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 49
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 49
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 25
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 34
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 49
filename:       /in/Z7hKd
function name:  mapToDbData
number of ops:  52
compiled vars:  !0 = $object, !1 = $objectFields, !2 = $resultArray, !3 = $field, !4 = $key, !5 = $method
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   RECV                                             !0      
   80     1        INIT_FCALL                                               'array_map'
   81     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FZ7hKd%3A81%240'
   83     3        SEND_VAL                                                 ~6
          4        INIT_FCALL                                               'preg_grep'
          5        SEND_VAL                                                 '%2F%5Eget%2F'
          6        INIT_FCALL                                               'get_class_methods'
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $7      
          9        SEND_VAR                                                 $7
         10        DO_ICALL                                         $8      
         11        SEND_VAR                                                 $8
         12        DO_ICALL                                         $9      
   80    13        ASSIGN                                                   !1, $9
   86    14        ASSIGN                                                   !2, <array>
   87    15      > FE_RESET_R                                       $12     !1, ->49
         16    > > FE_FETCH_R                                               $12, !3, ->49
   88    17    >   INIT_FCALL                                               'in_array'
         18        SEND_VAR                                                 !3
         19        FETCH_OBJ_R                                      ~13     'fields'
         20        SEND_VAL                                                 ~13
         21        DO_ICALL                                         $14     
         22        BOOL_NOT                                         ~15     $14
         23      > JMPZ                                                     ~15, ->25
   89    24    > > JMP                                                      ->16
   92    25    >   INIT_FCALL                                               'array_search'
         26        SEND_VAR                                                 !3
         27        FETCH_OBJ_R                                      ~16     'fields'
         28        SEND_VAL                                                 ~16
         29        DO_ICALL                                         $17     
         30        ASSIGN                                                   !4, $17
   93    31        ISSET_ISEMPTY_CV                                         !4
         32      > JMPZ                                                     ~19, ->34
   94    33    > > JMP                                                      ->16
   97    34    >   INIT_FCALL                                               'sprintf'
         35        SEND_VAL                                                 'get%25s'
         36        INIT_FCALL                                               'ucfirst'
         37        SEND_VAR                                                 !3
         38        DO_ICALL                                         $20     
         39        SEND_VAR                                                 $20
         40        DO_ICALL                                         $21     
         41        ASSIGN                                                   !5, $21
   98    42        INIT_ARRAY                                       ~24     !0
         43        ADD_ARRAY_ELEMENT                                ~24     !5
         44        INIT_USER_CALL                                0          'call_user_func', ~24
         45        DO_FCALL                                      0  $25     
         46        ASSIGN_DIM                                               !2, !4
         47        OP_DATA                                                  $25
   87    48      > JMP                                                      ->16
         49    >   FE_FREE                                                  $12
  101    50      > RETURN                                                   !2
  102    51*     > RETURN                                                   null

End of function maptodbdata

End of class AbstractDbToMapper.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.78 ms | 1412 KiB | 37 Q