3v4l.org

run code in 300+ PHP versions simultaneously
<?php $context = [ 'mes' => [ 'attributes' => [ 't' => 'AddEvent', 'id' => 'test_id', 'text' => 'русский текст' ], 'ev' => [ 'attributes' => [ 'evt' => 'packet20130828ki3si0', 'src' => 'NVV', 'st' => 'test', 'start' => '12423511247', 'stop' => '12423991978', 'addrtype' => 'MB', 'clntID' => '+380957700418', 'lg' => '', 'bank' => '', 'lang' => '', 'prior' => '', 'brnm' => '', 'extref' => '', 'intref' => '', 'extaddrtype' => '', 'extaddrid' => '', 'recall' => '', ], 'attr' => [ [ 'attributes' => [ 'code' => '1', 'val' => '11', ], ], [ 'attributes' => [ 'code' => '2', 'val' => '12', ], ], ], 'hist' => [ [ 'attributes' => [ 'st' => '', 'ch' => '', 'lg' => '', 'con' => '', 'com' => '', 'addrtype' => '', 'addrid' => '', 'dt' => '', 'contactId' => '', ], ], ], ], ], ]; class Conv { protected $res; protected $context; public function __construct(array $context) { if (count($context) != 1) { throw new Exception('Must be only one root element', 500); } $this->context = $context; $this->init(key($this->context)); } public function conv() { return $this->a2x(current($this->context), $this->res)->asXML(); } protected function init($root) { $this->res = new SimpleXMLElement("<{$root}/>"); } protected function addAttribs(array $attribs, SimpleXMLElement $node) { if (count($attribs)) { foreach ($attribs as $attrKey => $attrVal) { $node->addAttribute($attrKey, $attrVal); } } return $node; } protected function tryToAddAttribs(array $inner, SimpleXMLElement $node) { if (isset($inner['attributes'])) { $this->addAttribs($inner['attributes'], $node); unset($inner['attributes']); } return $node; } protected function addEnum(array $items, SimpleXMLElement $node, $caption) { $this->dump('adding enum', ['caption'=>$caption,'items'=>$items]); if (count($items)) { foreach ($items as $item) { $subNode = $node->addChild($caption); $this->tryToAddAttribs($item, $subNode); unset($item['attributes']); } } } protected function a2x(array $inner, SimpleXMLElement $node) { $this->tryToAddAttribs($inner, $node); unset($inner['attributes']); if (count($inner)) { foreach ($inner as $key => $value) { $this->dump('cur', ['key'=>$key,'val'=>$value]); if ($this->isEnumeration($value)) { $this->dump('is enum', $value); $this->addEnum($value, $node, $key); } else { $this->dump('is NOT enum', $value); $subNode = $node->addChild($key); $this->a2x($value, $subNode); } } } return $this->res; } protected function isEnumeration(array $items) { $keys = array_keys($items); foreach ($keys as $key) { if (!is_numeric($key)) { return false; } } return true; } protected function dump($title, $var) { if (false) { echo $title . ': ', json_encode($var) . '<hr/>'; } } } $a2x = new Conv($context); var_dump($a2x->conv());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Qati
function name:  (null)
number of ops:  11
compiled vars:  !0 = $context, !1 = $a2x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
  156     1        NEW                                              $3      'Conv'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
  157     5        INIT_FCALL                                               'var_dump'
          6        INIT_METHOD_CALL                                         !1, 'conv'
          7        DO_FCALL                                      0  $6      
          8        SEND_VAR                                                 $6
          9        DO_ICALL                                                 
         10      > RETURN                                                   1

Class Conv:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Qati
function name:  __construct
number of ops:  19
compiled vars:  !0 = $context
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   RECV                                             !0      
   70     1        COUNT                                            ~1      !0
          2        IS_NOT_EQUAL                                             ~1, 1
          3      > JMPZ                                                     ~2, ->9
   71     4    >   NEW                                              $3      'Exception'
          5        SEND_VAL_EX                                              'Must+be+only+one+root+element'
          6        SEND_VAL_EX                                              500
          7        DO_FCALL                                      0          
          8      > THROW                                         0          $3
   73     9    >   ASSIGN_OBJ                                               'context'
         10        OP_DATA                                                  !0
   74    11        INIT_METHOD_CALL                                         'init'
         12        INIT_FCALL                                               'key'
         13        FETCH_OBJ_R                                      ~6      'context'
         14        SEND_VAL                                                 ~6
         15        DO_ICALL                                         $7      
         16        SEND_VAR_NO_REF_EX                                       $7
         17        DO_FCALL                                      0          
   75    18      > RETURN                                                   null

End of function __construct

Function conv:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Qati
function name:  conv
number of ops:  14
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   INIT_METHOD_CALL                                         'a2x'
          1        INIT_FCALL                                               'current'
          2        FETCH_OBJ_R                                      ~0      'context'
          3        SEND_VAL                                                 ~0
          4        DO_ICALL                                         $1      
          5        SEND_VAR_NO_REF_EX                                       $1
          6        CHECK_FUNC_ARG                                           
          7        FETCH_OBJ_FUNC_ARG                               $2      'res'
          8        SEND_FUNC_ARG                                            $2
          9        DO_FCALL                                      0  $3      
         10        INIT_METHOD_CALL                                         $3, 'asXML'
         11        DO_FCALL                                      0  $4      
         12      > RETURN                                                   $4
   79    13*     > RETURN                                                   null

End of function conv

Function init:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Qati
function name:  init
number of ops:  10
compiled vars:  !0 = $root
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV                                             !0      
   82     1        NEW                                              $2      'SimpleXMLElement'
          2        ROPE_INIT                                     3  ~4      '%3C'
          3        ROPE_ADD                                      1  ~4      ~4, !0
          4        ROPE_END                                      2  ~3      ~4, '%2F%3E'
          5        SEND_VAL_EX                                              ~3
          6        DO_FCALL                                      0          
          7        ASSIGN_OBJ                                               'res'
          8        OP_DATA                                                  $2
   83     9      > RETURN                                                   null

End of function init

Function addattribs:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
Branch analysis from position: 13
filename:       /in/5Qati
function name:  addAttribs
number of ops:  15
compiled vars:  !0 = $attribs, !1 = $node, !2 = $attrVal, !3 = $attrKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   87     2        COUNT                                            ~4      !0
          3      > JMPZ                                                     ~4, ->13
   88     4    > > FE_RESET_R                                       $5      !0, ->12
          5    > > FE_FETCH_R                                       ~6      $5, !2, ->12
          6    >   ASSIGN                                                   !3, ~6
   89     7        INIT_METHOD_CALL                                         !1, 'addAttribute'
          8        SEND_VAR_EX                                              !3
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0          
   88    11      > JMP                                                      ->5
         12    >   FE_FREE                                                  $5
   92    13    > > RETURN                                                   !1
   93    14*     > RETURN                                                   null

End of function addattribs

Function trytoaddattribs:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/5Qati
function name:  tryToAddAttribs
number of ops:  13
compiled vars:  !0 = $inner, !1 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   95     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   97     2        ISSET_ISEMPTY_DIM_OBJ                         0          !0, 'attributes'
          3      > JMPZ                                                     ~2, ->11
   98     4    >   INIT_METHOD_CALL                                         'addAttribs'
          5        CHECK_FUNC_ARG                                           
          6        FETCH_DIM_FUNC_ARG                               $3      !0, 'attributes'
          7        SEND_FUNC_ARG                                            $3
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0          
   99    10        UNSET_DIM                                                !0, 'attributes'
  101    11    > > RETURN                                                   !1
  102    12*     > RETURN                                                   null

End of function trytoaddattribs

Function addenum:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 24
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 23
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 23
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
Branch analysis from position: 24
filename:       /in/5Qati
function name:  addEnum
number of ops:  25
compiled vars:  !0 = $items, !1 = $node, !2 = $caption, !3 = $item, !4 = $subNode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  104     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
  106     3        INIT_METHOD_CALL                                         'dump'
          4        SEND_VAL_EX                                              'adding+enum'
          5        INIT_ARRAY                                       ~5      !2, 'caption'
          6        ADD_ARRAY_ELEMENT                                ~5      !0, 'items'
          7        SEND_VAL_EX                                              ~5
          8        DO_FCALL                                      0          
  107     9        COUNT                                            ~7      !0
         10      > JMPZ                                                     ~7, ->24
  108    11    > > FE_RESET_R                                       $8      !0, ->23
         12    > > FE_FETCH_R                                               $8, !3, ->23
  109    13    >   INIT_METHOD_CALL                                         !1, 'addChild'
         14        SEND_VAR_EX                                              !2
         15        DO_FCALL                                      0  $9      
         16        ASSIGN                                                   !4, $9
  110    17        INIT_METHOD_CALL                                         'tryToAddAttribs'
         18        SEND_VAR_EX                                              !3
         19        SEND_VAR_EX                                              !4
         20        DO_FCALL                                      0          
  111    21        UNSET_DIM                                                !3, 'attributes'
  108    22      > JMP                                                      ->12
         23    >   FE_FREE                                                  $8
  114    24    > > RETURN                                                   null

End of function addenum

Function a2x:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 46
Branch analysis from position: 9
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 45
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 45
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 32
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
Branch analysis from position: 46
filename:       /in/5Qati
function name:  a2x
number of ops:  49
compiled vars:  !0 = $inner, !1 = $node, !2 = $value, !3 = $key, !4 = $subNode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  116     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  118     2        INIT_METHOD_CALL                                         'tryToAddAttribs'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
  119     6        UNSET_DIM                                                !0, 'attributes'
  120     7        COUNT                                            ~6      !0
          8      > JMPZ                                                     ~6, ->46
  121     9    > > FE_RESET_R                                       $7      !0, ->45
         10    > > FE_FETCH_R                                       ~8      $7, !2, ->45
         11    >   ASSIGN                                                   !3, ~8
  122    12        INIT_METHOD_CALL                                         'dump'
         13        SEND_VAL_EX                                              'cur'
         14        INIT_ARRAY                                       ~10     !3, 'key'
         15        ADD_ARRAY_ELEMENT                                ~10     !2, 'val'
         16        SEND_VAL_EX                                              ~10
         17        DO_FCALL                                      0          
  123    18        INIT_METHOD_CALL                                         'isEnumeration'
         19        SEND_VAR_EX                                              !2
         20        DO_FCALL                                      0  $12     
         21      > JMPZ                                                     $12, ->32
  124    22    >   INIT_METHOD_CALL                                         'dump'
         23        SEND_VAL_EX                                              'is+enum'
         24        SEND_VAR_EX                                              !2
         25        DO_FCALL                                      0          
  125    26        INIT_METHOD_CALL                                         'addEnum'
         27        SEND_VAR_EX                                              !2
         28        SEND_VAR_EX                                              !1
         29        SEND_VAR_EX                                              !3
         30        DO_FCALL                                      0          
         31      > JMP                                                      ->44
  127    32    >   INIT_METHOD_CALL                                         'dump'
         33        SEND_VAL_EX                                              'is+NOT+enum'
         34        SEND_VAR_EX                                              !2
         35        DO_FCALL                                      0          
  128    36        INIT_METHOD_CALL                                         !1, 'addChild'
         37        SEND_VAR_EX                                              !3
         38        DO_FCALL                                      0  $16     
         39        ASSIGN                                                   !4, $16
  129    40        INIT_METHOD_CALL                                         'a2x'
         41        SEND_VAR_EX                                              !2
         42        SEND_VAR_EX                                              !4
         43        DO_FCALL                                      0          
  121    44    > > JMP                                                      ->10
         45    >   FE_FREE                                                  $7
  133    46    >   FETCH_OBJ_R                                      ~19     'res'
         47      > RETURN                                                   ~19
  134    48*     > RETURN                                                   null

End of function a2x

Function isenumeration:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 15
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/5Qati
function name:  isEnumeration
number of ops:  18
compiled vars:  !0 = $items, !1 = $keys, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  136     0  E >   RECV                                             !0      
  138     1        INIT_FCALL                                               'array_keys'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $3      
          4        ASSIGN                                                   !1, $3
  139     5      > FE_RESET_R                                       $5      !1, ->15
          6    > > FE_FETCH_R                                               $5, !2, ->15
  140     7    >   INIT_FCALL                                               'is_numeric'
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                         $6      
         10        BOOL_NOT                                         ~7      $6
         11      > JMPZ                                                     ~7, ->14
  141    12    >   FE_FREE                                                  $5
         13      > RETURN                                                   <false>
  139    14    > > JMP                                                      ->6
         15    >   FE_FREE                                                  $5
  144    16      > RETURN                                                   <true>
  145    17*     > RETURN                                                   null

End of function isenumeration

Function dump:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/5Qati
function name:  dump
number of ops:  11
compiled vars:  !0 = $title, !1 = $var
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  147     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  149     2      > JMPZ                                                     <false>, ->10
  150     3    >   CONCAT                                           ~2      !0, '%3A+'
          4        ECHO                                                     ~2
          5        INIT_FCALL                                               'json_encode'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $3      
          8        CONCAT                                           ~4      $3, '%3Chr%2F%3E'
          9        ECHO                                                     ~4
  152    10    > > RETURN                                                   null

End of function dump

End of class Conv.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.9 ms | 1416 KiB | 25 Q