3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Class JsonSimpleXMLElementDecorator * * Implement JsonSerializable for SimpleXMLElement as a Decorator */ class JsonSimpleXMLElementDecorator implements JsonSerializable { /** * @var SimpleXMLElement */ private $subject; public function __construct(SimpleXMLElement $element) { $this->subject = $element; } /** * Specify data which should be serialized to JSON * * @return mixed data which can be serialized by json_encode. */ public function jsonSerialize() { $subject = $this->subject; $array = array(); // json encode attributes if any. if ($attributes = $subject->attributes()) { $array['@attributes'] = array_map('strval', iterator_to_array($attributes)); } // traverse into children if applicable $children = $subject; // json encode child elements if any. group on duplicate names as an array. foreach ($children as $name => $element) { $decorator = new self($element); if (isset($array[$name])) { if (!is_array($array[$name])) { $array[$name] = [$array[$name]]; } $array[$name][] = $decorator; } else { $array[$name] = $decorator; } } // json encode non-whitespace element simplexml text values. $text = trim($subject); if (strlen($text)) { if ($array) { $array['@text'] = $text; } else { $array = $text; } } // return empty elements as NULL (self-closing or empty tags) if (!$array) { $array = NULL; } return $array; } } $buffer = '<?xml version="1.0"?> <root attribute="variable"> <sampleElement> Text <sampleChild /> <sampleChild attribute="variable">text</sampleChild> </sampleElement> <sampleElement> test </sampleElement> </root>'; $xml = new SimpleXMLElement($buffer); $xml = new JsonSimpleXMLElementDecorator($xml); echo json_encode($xml, JSON_PRETTY_PRINT), "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XOiVD
function name:  (null)
number of ops:  17
compiled vars:  !0 = $buffer, !1 = $xml
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   DECLARE_CLASS                                            'jsonsimplexmlelementdecorator'
   70     1        ASSIGN                                                   !0, '%3C%3Fxml+version%3D%221.0%22%3F%3E%0A%3Croot+attribute%3D%22variable%22%3E%0A+++%3CsampleElement%3E%0A++++++++Text%0A++++++++%3CsampleChild+%2F%3E%0A++++++++%3CsampleChild+attribute%3D%22variable%22%3Etext%3C%2FsampleChild%3E%0A++++%3C%2FsampleElement%3E%0A++++%3CsampleElement%3E%0A++++++++test%0A++++%3C%2FsampleElement%3E%0A%3C%2Froot%3E'
   82     2        NEW                                              $3      'SimpleXMLElement'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $3
   83     6        NEW                                              $6      'JsonSimpleXMLElementDecorator'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !1, $6
   85    10        INIT_FCALL                                               'json_encode'
         11        SEND_VAR                                                 !1
         12        SEND_VAL                                                 128
         13        DO_ICALL                                         $9      
         14        ECHO                                                     $9
         15        ECHO                                                     '%0A'
         16      > RETURN                                                   1

Class JsonSimpleXMLElementDecorator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XOiVD
function name:  __construct
number of ops:  4
compiled vars:  !0 = $element
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   16     1        ASSIGN_OBJ                                               'subject'
          2        OP_DATA                                                  !0
   17     3      > RETURN                                                   null

End of function __construct

Function jsonserialize:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 16
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 41
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 41
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 38
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 34
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 34
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 48, Position 2 = 53
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 52
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 56
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 56
Branch analysis from position: 52
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 56
Branch analysis from position: 55
Branch analysis from position: 56
Branch analysis from position: 53
Branch analysis from position: 41
Branch analysis from position: 16
filename:       /in/XOiVD
function name:  jsonSerialize
number of ops:  58
compiled vars:  !0 = $subject, !1 = $array, !2 = $attributes, !3 = $children, !4 = $element, !5 = $name, !6 = $decorator, !7 = $text
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   FETCH_OBJ_R                                      ~8      'subject'
          1        ASSIGN                                                   !0, ~8
   27     2        ASSIGN                                                   !1, <array>
   30     3        INIT_METHOD_CALL                                         !0, 'attributes'
          4        DO_FCALL                                      0  $11     
          5        ASSIGN                                           ~12     !2, $11
          6      > JMPZ                                                     ~12, ->16
   31     7    >   INIT_FCALL                                               'array_map'
          8        SEND_VAL                                                 'strval'
          9        INIT_FCALL                                               'iterator_to_array'
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                         $14     
         12        SEND_VAR                                                 $14
         13        DO_ICALL                                         $15     
         14        ASSIGN_DIM                                               !1, '%40attributes'
         15        OP_DATA                                                  $15
   35    16    >   ASSIGN                                                   !3, !0
   38    17      > FE_RESET_R                                       $17     !3, ->41
         18    > > FE_FETCH_R                                       ~18     $17, !4, ->41
         19    >   ASSIGN                                                   !5, ~18
   39    20        NEW                          self                $20     
         21        SEND_VAR_EX                                              !4
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !6, $20
   41    24        ISSET_ISEMPTY_DIM_OBJ                         0          !1, !5
         25      > JMPZ                                                     ~23, ->38
   42    26    >   FETCH_DIM_R                                      ~24     !1, !5
         27        TYPE_CHECK                                  128  ~25     ~24
         28        BOOL_NOT                                         ~26     ~25
         29      > JMPZ                                                     ~26, ->34
   43    30    >   FETCH_DIM_R                                      ~28     !1, !5
         31        INIT_ARRAY                                       ~29     ~28
         32        ASSIGN_DIM                                               !1, !5
         33        OP_DATA                                                  ~29
   45    34    >   FETCH_DIM_W                                      $30     !1, !5
         35        ASSIGN_DIM                                               $30
         36        OP_DATA                                                  !6
         37      > JMP                                                      ->40
   47    38    >   ASSIGN_DIM                                               !1, !5
         39        OP_DATA                                                  !6
   38    40    > > JMP                                                      ->18
         41    >   FE_FREE                                                  $17
   52    42        INIT_FCALL                                               'trim'
         43        SEND_VAR                                                 !0
         44        DO_ICALL                                         $33     
         45        ASSIGN                                                   !7, $33
   53    46        STRLEN                                           ~35     !7
         47      > JMPZ                                                     ~35, ->53
   54    48    > > JMPZ                                                     !1, ->52
   55    49    >   ASSIGN_DIM                                               !1, '%40text'
         50        OP_DATA                                                  !7
         51      > JMP                                                      ->53
   57    52    >   ASSIGN                                                   !1, !7
   62    53    >   BOOL_NOT                                         ~38     !1
         54      > JMPZ                                                     ~38, ->56
   63    55    >   ASSIGN                                                   !1, null
   66    56    > > RETURN                                                   !1
   67    57*     > RETURN                                                   null

End of function jsonserialize

End of class JsonSimpleXMLElementDecorator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
250.71 ms | 1404 KiB | 21 Q