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' => ['outputField3' => ['double']], 'dbField3' => ['outputField3' => ['double', 'dollars']] ]; public function __construct(array $data) { $this->rawData = $data; $this->mapData(); } /** * @param $value * @return mixed */ private function double($value) { return $value * 2; } /** * @param $value * @return string */ private function dollars($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' => 1, 'dbField2' => 10, 'dbField3' => 100]; // 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($dm->getParsedData());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G0VgE
function name:  (null)
number of ops:  12
compiled vars:  !0 = $data, !1 = $dm
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   ASSIGN                                                   !0, <array>
   79     1        NEW                                              $3      'DataMapper'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
   81     5        INIT_FCALL                                               'json_encode'
          6        INIT_METHOD_CALL                                         !1, '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/G0VgE
function name:  __construct
number of ops:  6
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
   23     1        ASSIGN_OBJ                                               'rawData'
          2        OP_DATA                                                  !0
   24     3        INIT_METHOD_CALL                                         'mapData'
          4        DO_FCALL                                      0          
   25     5      > RETURN                                                   null

End of function __construct

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

End of function double

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

End of function dollars

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/G0VgE
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
-------------------------------------------------------------------------------------
   46     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
   48     4        FETCH_OBJ_IS                                     ~10     'rawData'
          5        ISSET_ISEMPTY_DIM_OBJ                         0          ~10, !1
          6      > JMPZ                                                     ~11, ->39
   50     7    >   TYPE_CHECK                                   64          !0
          8      > JMPZ                                                     ~12, ->15
   51     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
   53    15    >   TYPE_CHECK                                  128          !0
         16      > JMPZ                                                     ~17, ->39
   54    17    >   INIT_FCALL                                               'key'
         18        SEND_VAR                                                 !0
         19        DO_ICALL                                         $18     
         20        ASSIGN                                                   !2, $18
   55    21        INIT_FCALL                                               'reset'
         22        SEND_REF                                                 !0
         23        DO_ICALL                                         $20     
         24        ASSIGN                                                   !3, $20
   56    25        FETCH_OBJ_R                                      ~22     'rawData'
         26        FETCH_DIM_R                                      ~23     ~22, !1
         27        ASSIGN                                                   !4, ~23
   58    28      > FE_RESET_R                                       $25     !3, ->35
         29    > > FE_FETCH_R                                               $25, !5, ->35
   59    30    >   INIT_METHOD_CALL                                         !5
         31        SEND_VAR_EX                                              !4
         32        DO_FCALL                                      0  $26     
         33        ASSIGN                                                   !4, $26
   58    34      > JMP                                                      ->29
         35    >   FE_FREE                                                  $25
   61    36        FETCH_OBJ_W                                      $28     'parsedData'
         37        ASSIGN_DIM                                               $28, !2
         38        OP_DATA                                                  !4
   46    39    > > JMP                                                      ->2
         40    >   FE_FREE                                                  $7
   65    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/G0VgE
function name:  getParsedData
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   FETCH_OBJ_R                                      ~0      'parsedData'
          1      > RETURN                                                   ~0
   73     2*     > RETURN                                                   null

End of function getparseddata

End of class DataMapper.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
165.48 ms | 1404 KiB | 19 Q