3v4l.org

run code in 300+ PHP versions simultaneously
<?php try { $user = new User_DTO(new Validator, [ 'id' => 1, 'name' => 'Foo', 'username' => 'foo', 'email' => '', 'address' => [ 'street' => '', 'suite' => '', 'city' => '', 'zipcode' => '', 'geo' => [ 'lat' => '', 'lng' => '', ], ], 'phone' => '', 'website' => '', 'company' => [ 'name' => '', 'catchPhrase' => '', 'bs' => '', ], ]); $user_list = new User_List_DTO(new Validator, [ 'id' => 2, 'name' => 'Bar', 'username' => 'bar', ]); echo $user->get_id() . PHP_EOL; echo $user->get_name() . PHP_EOL; echo $user->get_username() . PHP_EOL; echo $user_list->get_id() . PHP_EOL; echo $user_list->get_name() . PHP_EOL; echo $user_list->get_username() . PHP_EOL; } catch(Exception $e) { echo $e->getMessage(); } class Validator { public function validate_data_against_schema( array $data, array $schema ) { foreach ( $schema as $key => $requiredType ) { if ( ! array_key_exists( $key, $data ) ) { throw new \UnexpectedValueException( sprintf( 'Required key "%s" does not exist.', $key ) ); } if ( is_array($requiredType) ) { if ( ! is_array( $data[ $key ] ) ) { throw new \UnexpectedValueException( sprintf( 'Key "%s" must be an array, %s provided.', $key, gettype( $data[ $key ] ) ) ); } // Traverse down the children, recursively. $this->validate_data_against_schema( $data[ $key ], $requiredType ); } else { if ( gettype( $data[ $key ] ) !== $requiredType ) { throw new \UnexpectedValueException( sprintf( 'Key "%s" must be of type %s, %s provided.', $key, $requiredType, gettype( $data[ $key ] ) ) ); } } } } } abstract class DTO { abstract protected function getSchema(): array; abstract protected function initaliseFromArray($array): void; final public function __construct( Validator $validator, array $data ) { $validator->validate_data_against_schema( $data, $this->getSchema() ); } } class User_DTO extends DTO { private int $id; private string $name; private string $username; protected function getSchema(): array { return [ 'id' => 'integer', 'name' => 'string', 'username' => 'string', 'email' => 'string', 'address' => [ 'street' => 'string', 'suite' => 'string', 'city' => 'string', 'zipcode' => 'string', 'geo' => [ 'lat' => 'float', 'lng' => 'float', ], ], 'phone' => 'string', 'website' => 'string', 'company' => [ 'name' => 'string', 'catchPhrase' => 'string', 'bs' => 'string', ], ]; } protected function initaliseFromArray($array): void { $this->id = (int) $this->data['id']; $this->name = (string) $this->data['name']; $this->username = (string) $this->data['username']; } public function get_id(): int { return $this->id; } public function get_name(): string { return $this->name; } public function get_username(): string { return $this->username; } // etc } class User_List_DTO extends DTO { protected function getSchema(): array { return [ 'id' => 'integer', 'name' => 'string', 'username' => 'string', ]; } protected function initaliseFromArray($array): void { $this->id = (int) $this->data['id']; $this->name = (string) $this->data['name']; $this->username = (string) $this->data['username']; } public function get_id(): int { return $this->id; } public function get_name(): string { return $this->name; } public function get_username(): string { return $this->username; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 39
Branch analysis from position: 39
2 jumps found. (Code = 107) Position 1 = 40, Position 2 = -2
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  (null)
number of ops:  44
compiled vars:  !0 = $user, !1 = $user_list, !2 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   NEW                                              $3      'User_DTO'
          1        NEW                                              $4      'Validator'
          2        DO_FCALL                                      0          
          3        SEND_VAR_NO_REF_EX                                       $4
    5     4        SEND_VAL_EX                                              <array>
          5        DO_FCALL                                      0          
    4     6        ASSIGN                                                   !0, $3
   28     7        NEW                                              $8      'User_List_DTO'
          8        NEW                                              $9      'Validator'
          9        DO_FCALL                                      0          
         10        SEND_VAR_NO_REF_EX                                       $9
   29    11        SEND_VAL_EX                                              <array>
         12        DO_FCALL                                      0          
   28    13        ASSIGN                                                   !1, $8
   34    14        INIT_METHOD_CALL                                         !0, 'get_id'
         15        DO_FCALL                                      0  $13     
         16        CONCAT                                           ~14     $13, '%0A'
         17        ECHO                                                     ~14
   35    18        INIT_METHOD_CALL                                         !0, 'get_name'
         19        DO_FCALL                                      0  $15     
         20        CONCAT                                           ~16     $15, '%0A'
         21        ECHO                                                     ~16
   36    22        INIT_METHOD_CALL                                         !0, 'get_username'
         23        DO_FCALL                                      0  $17     
         24        CONCAT                                           ~18     $17, '%0A'
         25        ECHO                                                     ~18
   38    26        INIT_METHOD_CALL                                         !1, 'get_id'
         27        DO_FCALL                                      0  $19     
         28        CONCAT                                           ~20     $19, '%0A'
         29        ECHO                                                     ~20
   39    30        INIT_METHOD_CALL                                         !1, 'get_name'
         31        DO_FCALL                                      0  $21     
         32        CONCAT                                           ~22     $21, '%0A'
         33        ECHO                                                     ~22
   40    34        INIT_METHOD_CALL                                         !1, 'get_username'
         35        DO_FCALL                                      0  $23     
         36        CONCAT                                           ~24     $23, '%0A'
         37        ECHO                                                     ~24
         38      > JMP                                                      ->43
   41    39  E > > CATCH                                       last         'Exception'
   42    40    >   INIT_METHOD_CALL                                         !2, 'getMessage'
         41        DO_FCALL                                      0  $25     
         42        ECHO                                                     $25
  166    43    > > RETURN                                                   1

Class Validator:
Function validate_data_against_schema:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 57
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 57
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 40
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 33
Branch analysis from position: 22
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 40
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 56
Branch analysis from position: 44
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 56
Branch analysis from position: 57
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 57
filename:       /in/b8nFS
function name:  validate_data_against_schema
number of ops:  59
compiled vars:  !0 = $data, !1 = $schema, !2 = $requiredType, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   47     2      > FE_RESET_R                                       $4      !1, ->57
          3    > > FE_FETCH_R                                       ~5      $4, !2, ->57
          4    >   ASSIGN                                                   !3, ~5
   48     5        ARRAY_KEY_EXISTS                                 ~7      !3, !0
          6        BOOL_NOT                                         ~8      ~7
          7      > JMPZ                                                     ~8, ->16
   49     8    >   NEW                                              $9      'UnexpectedValueException'
          9        INIT_FCALL                                               'sprintf'
         10        SEND_VAL                                                 'Required+key+%22%25s%22+does+not+exist.'
         11        SEND_VAR                                                 !3
         12        DO_ICALL                                         $10     
         13        SEND_VAR_NO_REF_EX                                       $10
         14        DO_FCALL                                      0          
         15      > THROW                                         0          $9
   52    16    >   TYPE_CHECK                                  128          !2
         17      > JMPZ                                                     ~12, ->40
   53    18    >   FETCH_DIM_R                                      ~13     !0, !3
         19        TYPE_CHECK                                  128  ~14     ~13
         20        BOOL_NOT                                         ~15     ~14
         21      > JMPZ                                                     ~15, ->33
   54    22    >   NEW                                              $16     'UnexpectedValueException'
         23        INIT_FCALL                                               'sprintf'
   55    24        SEND_VAL                                                 'Key+%22%25s%22+must+be+an+array%2C+%25s+provided.'
   56    25        SEND_VAR                                                 !3
   57    26        FETCH_DIM_R                                      ~17     !0, !3
         27        GET_TYPE                                         ~18     ~17
         28        SEND_VAL                                                 ~18
         29        DO_ICALL                                         $19     
         30        SEND_VAR_NO_REF_EX                                       $19
         31        DO_FCALL                                      0          
         32      > THROW                                         0          $16
   62    33    >   INIT_METHOD_CALL                                         'validate_data_against_schema'
         34        CHECK_FUNC_ARG                                           
         35        FETCH_DIM_FUNC_ARG                               $21     !0, !3
         36        SEND_FUNC_ARG                                            $21
         37        SEND_VAR_EX                                              !2
         38        DO_FCALL                                      0          
         39      > JMP                                                      ->56
   65    40    >   FETCH_DIM_R                                      ~23     !0, !3
         41        GET_TYPE                                         ~24     ~23
         42        IS_NOT_IDENTICAL                                         !2, ~24
         43      > JMPZ                                                     ~25, ->56
   66    44    >   NEW                                              $26     'UnexpectedValueException'
         45        INIT_FCALL                                               'sprintf'
   67    46        SEND_VAL                                                 'Key+%22%25s%22+must+be+of+type+%25s%2C+%25s+provided.'
   68    47        SEND_VAR                                                 !3
   69    48        SEND_VAR                                                 !2
   70    49        FETCH_DIM_R                                      ~27     !0, !3
         50        GET_TYPE                                         ~28     ~27
         51        SEND_VAL                                                 ~28
         52        DO_ICALL                                         $29     
         53        SEND_VAR_NO_REF_EX                                       $29
         54        DO_FCALL                                      0          
         55      > THROW                                         0          $26
   47    56    > > JMP                                                      ->3
         57    >   FE_FREE                                                  $4
   75    58      > RETURN                                                   null

End of function validate_data_against_schema

End of class Validator.

Class DTO:
Function getschema:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  getSchema
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   VERIFY_RETURN_TYPE                                       
          1      > RETURN                                                   null

End of function getschema

Function initalisefromarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  initaliseFromArray
number of ops:  2
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function initalisefromarray

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  __construct
number of ops:  9
compiled vars:  !0 = $validator, !1 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   84     2        INIT_METHOD_CALL                                         !0, 'validate_data_against_schema'
          3        SEND_VAR_EX                                              !1
          4        INIT_METHOD_CALL                                         'getSchema'
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR_NO_REF_EX                                       $2
          7        DO_FCALL                                      0          
   85     8      > RETURN                                                   null

End of function __construct

End of class DTO.

Class User_DTO:
Function getschema:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  getSchema
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   95     0  E > > RETURN                                                   <array>
  117     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function getschema

Function initalisefromarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  initaliseFromArray
number of ops:  17
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  119     0  E >   RECV                                             !0      
  120     1        FETCH_OBJ_R                                      ~2      'data'
          2        FETCH_DIM_R                                      ~3      ~2, 'id'
          3        CAST                                          4  ~4      ~3
          4        ASSIGN_OBJ                                               'id'
          5        OP_DATA                                                  ~4
  121     6        FETCH_OBJ_R                                      ~6      'data'
          7        FETCH_DIM_R                                      ~7      ~6, 'name'
          8        CAST                                          6  ~8      ~7
          9        ASSIGN_OBJ                                               'name'
         10        OP_DATA                                                  ~8
  122    11        FETCH_OBJ_R                                      ~10     'data'
         12        FETCH_DIM_R                                      ~11     ~10, 'username'
         13        CAST                                          6  ~12     ~11
         14        ASSIGN_OBJ                                               'username'
         15        OP_DATA                                                  ~12
  123    16      > RETURN                                                   null

End of function initalisefromarray

Function get_id:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  get_id
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  126     0  E >   FETCH_OBJ_R                                      ~0      'id'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
  127     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function get_id

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

End of function get_name

Function get_username:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  get_username
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  134     0  E >   FETCH_OBJ_R                                      ~0      'username'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
  135     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function get_username

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  __construct
number of ops:  9
compiled vars:  !0 = $validator, !1 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   84     2        INIT_METHOD_CALL                                         !0, 'validate_data_against_schema'
          3        SEND_VAR_EX                                              !1
          4        INIT_METHOD_CALL                                         'getSchema'
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR_NO_REF_EX                                       $2
          7        DO_FCALL                                      0          
   85     8      > RETURN                                                   null

End of function __construct

End of class User_DTO.

Class User_List_DTO:
Function getschema:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  getSchema
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  143     0  E > > RETURN                                                   <array>
  147     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function getschema

Function initalisefromarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  initaliseFromArray
number of ops:  17
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  149     0  E >   RECV                                             !0      
  150     1        FETCH_OBJ_R                                      ~2      'data'
          2        FETCH_DIM_R                                      ~3      ~2, 'id'
          3        CAST                                          4  ~4      ~3
          4        ASSIGN_OBJ                                               'id'
          5        OP_DATA                                                  ~4
  151     6        FETCH_OBJ_R                                      ~6      'data'
          7        FETCH_DIM_R                                      ~7      ~6, 'name'
          8        CAST                                          6  ~8      ~7
          9        ASSIGN_OBJ                                               'name'
         10        OP_DATA                                                  ~8
  152    11        FETCH_OBJ_R                                      ~10     'data'
         12        FETCH_DIM_R                                      ~11     ~10, 'username'
         13        CAST                                          6  ~12     ~11
         14        ASSIGN_OBJ                                               'username'
         15        OP_DATA                                                  ~12
  153    16      > RETURN                                                   null

End of function initalisefromarray

Function get_id:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  get_id
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  156     0  E >   FETCH_OBJ_R                                      ~0      'id'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
  157     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function get_id

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

End of function get_name

Function get_username:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  get_username
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  164     0  E >   FETCH_OBJ_R                                      ~0      'username'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
  165     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function get_username

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/b8nFS
function name:  __construct
number of ops:  9
compiled vars:  !0 = $validator, !1 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   84     2        INIT_METHOD_CALL                                         !0, 'validate_data_against_schema'
          3        SEND_VAR_EX                                              !1
          4        INIT_METHOD_CALL                                         'getSchema'
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR_NO_REF_EX                                       $2
          7        DO_FCALL                                      0          
   85     8      > RETURN                                                   null

End of function __construct

End of class User_List_DTO.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.46 ms | 1420 KiB | 15 Q