3v4l.org

run code in 300+ PHP versions simultaneously
<?php class DataMapper { /** * @var array */ private $rawData; /** * @var array */ private $parsedData; /** * @var array */ private $mapping = [ 'dbField1' => 'outputField1', 'dbField2' => ['outputField2' => ['parseFn1', 'parseFn2']] ]; public function __construct(array $data) { $this->rawData = $data; $this->mapData(); } /** * @param $value * @return mixed */ private function parseFn1($value) { return $value * 2; } /** * @param $value * @return string */ private function parseFn2($value) { return $value . "$"; } // Yes, I know the methods shouldn't be here but it rather be decoupled in another class and // injected with a DI it but I wrote it like this with example purposes only private function mapData() { foreach($this->mapping as $dbFieldName => $content) { if(isset($this->rawData[$dbFieldName])) { if(is_string($content)) { $this->parsedData[$content] = $this->rawData[$dbFieldName]; } else if (is_array($content)) { $frontFieldName = key($content); $functionsArray = reset($content); $value = $this->rawData[$dbFieldName]; foreach($functionsArray as $functionName) { $value = $this->{$functionName}($value); } $this->parsedData[$frontFieldName] = $value; } } } } /** * Simple getter * @return array */ public function getParsedData() { return $this->parsedData; } } // Usually the data comes from the DB so it would be in some kind of Row Set structure $data = ['dbField1' => 5, 'dbField2' => 13]; // The data is handed to the Holder which parses it $dm = new DataMapper($data); // The data is parsed so it can be echo json_encode($data->getParsedData());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tU8hL
function name:  (null)
number of ops:  12
compiled vars:  !0 = $data, !1 = $dm
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   ASSIGN                                                   !0, <array>
   78     1        NEW                                              $3      'DataMapper'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
   80     5        INIT_FCALL                                               'json_encode'
          6        INIT_METHOD_CALL                                         !0, 'getParsedData'
          7        DO_FCALL                                      0  $6      
          8        SEND_VAR                                                 $6
          9        DO_ICALL                                         $7      
         10        ECHO                                                     $7
         11      > RETURN                                                   1

Class DataMapper:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tU8hL
function name:  __construct
number of ops:  6
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   22     1        ASSIGN_OBJ                                               'rawData'
          2        OP_DATA                                                  !0
   23     3        INIT_METHOD_CALL                                         'mapData'
          4        DO_FCALL                                      0          
   24     5      > RETURN                                                   null

End of function __construct

Function parsefn1:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tU8hL
function name:  parseFn1
number of ops:  4
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   31     1        MUL                                              ~1      !0, 2
          2      > RETURN                                                   ~1
   32     3*     > RETURN                                                   null

End of function parsefn1

Function parsefn2:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tU8hL
function name:  parseFn2
number of ops:  4
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
   39     1        CONCAT                                           ~1      !0, '%24'
          2      > RETURN                                                   ~1
   40     3*     > RETURN                                                   null

End of function parsefn2

Function mapdata:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 40
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 40
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 39
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 39
Branch analysis from position: 17
2 jumps found. (Code = 77) Position 1 = 29, Position 2 = 35
Branch analysis from position: 29
2 jumps found. (Code = 78) Position 1 = 30, Position 2 = 35
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 35
Branch analysis from position: 39
Branch analysis from position: 39
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
filename:       /in/tU8hL
function name:  mapData
number of ops:  42
compiled vars:  !0 = $content, !1 = $dbFieldName, !2 = $frontFieldName, !3 = $functionsArray, !4 = $value, !5 = $functionName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   FETCH_OBJ_R                                      ~6      'mapping'
          1      > FE_RESET_R                                       $7      ~6, ->40
          2    > > FE_FETCH_R                                       ~8      $7, !0, ->40
          3    >   ASSIGN                                                   !1, ~8
   47     4        FETCH_OBJ_IS                                     ~10     'rawData'
          5        ISSET_ISEMPTY_DIM_OBJ                         0          ~10, !1
          6      > JMPZ                                                     ~11, ->39
   49     7    >   TYPE_CHECK                                   64          !0
          8      > JMPZ                                                     ~12, ->15
   50     9    >   FETCH_OBJ_R                                      ~15     'rawData'
         10        FETCH_DIM_R                                      ~16     ~15, !1
         11        FETCH_OBJ_W                                      $13     'parsedData'
         12        ASSIGN_DIM                                               $13, !0
         13        OP_DATA                                                  ~16
         14      > JMP                                                      ->39
   52    15    >   TYPE_CHECK                                  128          !0
         16      > JMPZ                                                     ~17, ->39
   53    17    >   INIT_FCALL                                               'key'
         18        SEND_VAR                                                 !0
         19        DO_ICALL                                         $18     
         20        ASSIGN                                                   !2, $18
   54    21        INIT_FCALL                                               'reset'
         22        SEND_REF                                                 !0
         23        DO_ICALL                                         $20     
         24        ASSIGN                                                   !3, $20
   55    25        FETCH_OBJ_R                                      ~22     'rawData'
         26        FETCH_DIM_R                                      ~23     ~22, !1
         27        ASSIGN                                                   !4, ~23
   57    28      > FE_RESET_R                                       $25     !3, ->35
         29    > > FE_FETCH_R                                               $25, !5, ->35
   58    30    >   INIT_METHOD_CALL                                         !5
         31        SEND_VAR_EX                                              !4
         32        DO_FCALL                                      0  $26     
         33        ASSIGN                                                   !4, $26
   57    34      > JMP                                                      ->29
         35    >   FE_FREE                                                  $25
   60    36        FETCH_OBJ_W                                      $28     'parsedData'
         37        ASSIGN_DIM                                               $28, !2
         38        OP_DATA                                                  !4
   45    39    > > JMP                                                      ->2
         40    >   FE_FREE                                                  $7
   64    41      > RETURN                                                   null

End of function mapdata

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

End of function getparseddata

End of class DataMapper.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.43 ms | 1404 KiB | 19 Q