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; protected $charset; public function __construct(array $context, $charset = 'UTF-8') { if (count($context) != 1) { throw new Exception('Must be only one root element', 500); } $this->context = $context; $this->charset = $charset; $this->init(key($this->context)); } public function conv() { return $this->formatXml($this->a2x(current($this->context), $this->res)); } protected function init($root) { $header = "<?xml version=\"1.0\" encoding=\"{$this->charset}\"?><{$root}/>"; $this->res = new SimpleXMLElement($header); } 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 formatXml(SimpleXMLElement $xml) { $dom = new DOMDocument("1.0"); $dom->formatOutput = true; return $dom->loadXML($xml->asXML())->saveXML(); } 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/rJN80
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>
  166     1        NEW                                              $3      'Conv'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
  167     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 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rJN80
function name:  __construct
number of ops:  22
compiled vars:  !0 = $context, !1 = $charset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'UTF-8'
   71     2        COUNT                                            ~2      !0
          3        IS_NOT_EQUAL                                             ~2, 1
          4      > JMPZ                                                     ~3, ->10
   72     5    >   NEW                                              $4      'Exception'
          6        SEND_VAL_EX                                              'Must+be+only+one+root+element'
          7        SEND_VAL_EX                                              500
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $4
   74    10    >   ASSIGN_OBJ                                               'context'
         11        OP_DATA                                                  !0
   75    12        ASSIGN_OBJ                                               'charset'
         13        OP_DATA                                                  !1
   76    14        INIT_METHOD_CALL                                         'init'
         15        INIT_FCALL                                               'key'
         16        FETCH_OBJ_R                                      ~8      'context'
         17        SEND_VAL                                                 ~8
         18        DO_ICALL                                         $9      
         19        SEND_VAR_NO_REF_EX                                       $9
         20        DO_FCALL                                      0          
   77    21      > 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/rJN80
function name:  conv
number of ops:  15
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   INIT_METHOD_CALL                                         'formatXml'
          1        INIT_METHOD_CALL                                         'a2x'
          2        INIT_FCALL                                               'current'
          3        FETCH_OBJ_R                                      ~0      'context'
          4        SEND_VAL                                                 ~0
          5        DO_ICALL                                         $1      
          6        SEND_VAR_NO_REF_EX                                       $1
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $2      'res'
          9        SEND_FUNC_ARG                                            $2
         10        DO_FCALL                                      0  $3      
         11        SEND_VAR_NO_REF_EX                                       $3
         12        DO_FCALL                                      0  $4      
         13      > RETURN                                                   $4
   81    14*     > 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/rJN80
function name:  init
number of ops:  14
compiled vars:  !0 = $root, !1 = $header
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
   84     1        ROPE_INIT                                     5  ~4      '%3C%3Fxml+version%3D%221.0%22+encoding%3D%22'
          2        FETCH_OBJ_R                                      ~2      'charset'
          3        ROPE_ADD                                      1  ~4      ~4, ~2
          4        ROPE_ADD                                      2  ~4      ~4, '%22%3F%3E%3C'
          5        ROPE_ADD                                      3  ~4      ~4, !0
          6        ROPE_END                                      4  ~3      ~4, '%2F%3E'
          7        ASSIGN                                                   !1, ~3
   85     8        NEW                                              $9      'SimpleXMLElement'
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0          
         11        ASSIGN_OBJ                                               'res'
         12        OP_DATA                                                  $9
   86    13      > 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/rJN80
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
-------------------------------------------------------------------------------------
   88     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   90     2        COUNT                                            ~4      !0
          3      > JMPZ                                                     ~4, ->13
   91     4    > > FE_RESET_R                                       $5      !0, ->12
          5    > > FE_FETCH_R                                       ~6      $5, !2, ->12
          6    >   ASSIGN                                                   !3, ~6
   92     7        INIT_METHOD_CALL                                         !1, 'addAttribute'
          8        SEND_VAR_EX                                              !3
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0          
   91    11      > JMP                                                      ->5
         12    >   FE_FREE                                                  $5
   95    13    > > RETURN                                                   !1
   96    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/rJN80
function name:  tryToAddAttribs
number of ops:  13
compiled vars:  !0 = $inner, !1 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   98     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  100     2        ISSET_ISEMPTY_DIM_OBJ                         0          !0, 'attributes'
          3      > JMPZ                                                     ~2, ->11
  101     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          
  102    10        UNSET_DIM                                                !0, 'attributes'
  104    11    > > RETURN                                                   !1
  105    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/rJN80
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
-------------------------------------------------------------------------------------
  107     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
  109     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          
  110     9        COUNT                                            ~7      !0
         10      > JMPZ                                                     ~7, ->24
  111    11    > > FE_RESET_R                                       $8      !0, ->23
         12    > > FE_FETCH_R                                               $8, !3, ->23
  112    13    >   INIT_METHOD_CALL                                         !1, 'addChild'
         14        SEND_VAR_EX                                              !2
         15        DO_FCALL                                      0  $9      
         16        ASSIGN                                                   !4, $9
  113    17        INIT_METHOD_CALL                                         'tryToAddAttribs'
         18        SEND_VAR_EX                                              !3
         19        SEND_VAR_EX                                              !4
         20        DO_FCALL                                      0          
  114    21        UNSET_DIM                                                !3, 'attributes'
  111    22      > JMP                                                      ->12
         23    >   FE_FREE                                                  $8
  117    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/rJN80
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
-------------------------------------------------------------------------------------
  119     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  121     2        INIT_METHOD_CALL                                         'tryToAddAttribs'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
  122     6        UNSET_DIM                                                !0, 'attributes'
  123     7        COUNT                                            ~6      !0
          8      > JMPZ                                                     ~6, ->46
  124     9    > > FE_RESET_R                                       $7      !0, ->45
         10    > > FE_FETCH_R                                       ~8      $7, !2, ->45
         11    >   ASSIGN                                                   !3, ~8
  125    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          
  126    18        INIT_METHOD_CALL                                         'isEnumeration'
         19        SEND_VAR_EX                                              !2
         20        DO_FCALL                                      0  $12     
         21      > JMPZ                                                     $12, ->32
  127    22    >   INIT_METHOD_CALL                                         'dump'
         23        SEND_VAL_EX                                              'is+enum'
         24        SEND_VAR_EX                                              !2
         25        DO_FCALL                                      0          
  128    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
  130    32    >   INIT_METHOD_CALL                                         'dump'
         33        SEND_VAL_EX                                              'is+NOT+enum'
         34        SEND_VAR_EX                                              !2
         35        DO_FCALL                                      0          
  131    36        INIT_METHOD_CALL                                         !1, 'addChild'
         37        SEND_VAR_EX                                              !3
         38        DO_FCALL                                      0  $16     
         39        ASSIGN                                                   !4, $16
  132    40        INIT_METHOD_CALL                                         'a2x'
         41        SEND_VAR_EX                                              !2
         42        SEND_VAR_EX                                              !4
         43        DO_FCALL                                      0          
  124    44    > > JMP                                                      ->10
         45    >   FE_FREE                                                  $7
  136    46    >   FETCH_OBJ_R                                      ~19     'res'
         47      > RETURN                                                   ~19
  137    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/rJN80
function name:  isEnumeration
number of ops:  18
compiled vars:  !0 = $items, !1 = $keys, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  139     0  E >   RECV                                             !0      
  141     1        INIT_FCALL                                               'array_keys'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $3      
          4        ASSIGN                                                   !1, $3
  142     5      > FE_RESET_R                                       $5      !1, ->15
          6    > > FE_FETCH_R                                               $5, !2, ->15
  143     7    >   INIT_FCALL                                               'is_numeric'
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                         $6      
         10        BOOL_NOT                                         ~7      $6
         11      > JMPZ                                                     ~7, ->14
  144    12    >   FE_FREE                                                  $5
         13      > RETURN                                                   <false>
  142    14    > > JMP                                                      ->6
         15    >   FE_FREE                                                  $5
  147    16      > RETURN                                                   <true>
  148    17*     > RETURN                                                   null

End of function isenumeration

Function formatxml:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rJN80
function name:  formatXml
number of ops:  16
compiled vars:  !0 = $xml, !1 = $dom
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  150     0  E >   RECV                                             !0      
  152     1        NEW                                              $2      'DOMDocument'
          2        SEND_VAL_EX                                              '1.0'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $2
  153     5        ASSIGN_OBJ                                               !1, 'formatOutput'
          6        OP_DATA                                                  <true>
  154     7        INIT_METHOD_CALL                                         !1, 'loadXML'
          8        INIT_METHOD_CALL                                         !0, 'asXML'
          9        DO_FCALL                                      0  $6      
         10        SEND_VAR_NO_REF_EX                                       $6
         11        DO_FCALL                                      0  $7      
         12        INIT_METHOD_CALL                                         $7, 'saveXML'
         13        DO_FCALL                                      0  $8      
         14      > RETURN                                                   $8
  155    15*     > RETURN                                                   null

End of function formatxml

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/rJN80
function name:  dump
number of ops:  11
compiled vars:  !0 = $title, !1 = $var
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  157     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  159     2      > JMPZ                                                     <false>, ->10
  160     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
  162    10    > > RETURN                                                   null

End of function dump

End of class Conv.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.8 ms | 1420 KiB | 25 Q