3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xpath = "/bids_offers/market_submit/sem_gen_technical_offer/block_deloading/point[2]"; $xml = new EformDOMDocument(); $root = $xml->createElement("bids_offers"); $xml->appendChild($root); $xml->buildMissingTree($xpath); $xml->formatOutput = true; echo $xml->saveXML(); class EformDOMDocument extends DOMDocument { public $xpath; public function __construct($version = null, $encoding = null) { parent::__construct($version, $encoding); $this->xpath = new DOMXPath($this); } public function setAttributeX($path, $attributeName, $attributeValue) { // check if path returns anything valid if ($this->xpath->query($path)->length == 0) { $this->buildMissingTree($path); } $this->xpath->query($path)->item(0)->setAttribute($attributeName, $attributeValue); } public function buildMissingTree($path) { $missingNodes = array(); $tempPath = $path; // build an array of missing elements in the path while ($this->xpath->query($tempPath)->length == 0) { $tempName = basename($tempPath); $tempPath = dirname($tempPath); // deal with such: testnode[@type='HOT'] or [#] if (strpos($tempName, '[') === FALSE) { // no attributes use $tempName $missingNodes[] = $this->createElement($tempName); } else { // split element name and attribute, create both separate, append attribute to element, push into the aray $tempHolder = explode('[', $tempName); $tempElement = $this->createElement($tempHolder[0]); // [1] is attribute, clean it up, split name and value if (strpos($tempHolder[1], '@') !== FALSE) { // element has attiribute if it has @ in the path piece $attParam = str_replace(array('@', '\'', ']'), '', $tempHolder[1]); $attribute = explode('=', $attParam); $tempElement->setAttribute($attribute[0], $attribute[1]); } // otherwise its a number of the node in the sequence, skip it, as if xml is generated is in order, //previous nodes should exist already $missingNodes[] = $tempElement; $tempElement = null; } } // build the structure with missing elements (use $missingNodes array) // get existing end echo $tempPath; var_dump($missingNodes); $currentElement = $this->xpath->query($tempPath)->item(0); while (($newElement = array_pop($missingNodes) !== NULL)) { $currentElement->appendChild($newElement); $currentElement = $newElement; } } /** * Checks if document has at least one element with a name (and optional attribute) provided * * @param string $name * @return mixed */ public function hasElement($name, $attribute = '') { $elements = $this->getElementsByTagName($name); if ($elements->length > 0) { if ($attribute == '') { return true; } // if attribute provided, check through the list for the element with provided attribute for ($i = 0; $i < $elements->length; $i++) { // TODO fix that checks on attibute value too, not just name if ($elements->item($i)->hasAttribute($attribute)) { return $elements->item($i); //return true; } } } return false; } /** * * @param string $name * @param string $attributeParameters provided as "attr1name=value1;attr2name=value2...etc." * @return DOMElement */ public function createElementWithAttributes($name, $attributeParameters = '') { $element = $this->createElement($name); // atributes provided as "attr1name=value1;attr2name=value2...etc." if ($attributeParameters != '') { // separate multiple parameters $attributes = explode(';', $attributeParameters); // add parameters to the element foreach ($attributes as $attribute) { $keyvalue = explode('=', $attribute); $element->setAttribute($keyvalue[0], $keyvalue[1]); } } return $element; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ebVBR
function name:  (null)
number of ops:  20
compiled vars:  !0 = $xpath, !1 = $xml, !2 = $root
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, '%2Fbids_offers%2Fmarket_submit%2Fsem_gen_technical_offer%2Fblock_deloading%2Fpoint%5B2%5D'
    3     1        NEW                                              $4      'EformDOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $4
    4     4        INIT_METHOD_CALL                                         !1, 'createElement'
          5        SEND_VAL_EX                                              'bids_offers'
          6        DO_FCALL                                      0  $7      
          7        ASSIGN                                                   !2, $7
    5     8        INIT_METHOD_CALL                                         !1, 'appendChild'
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0          
    6    11        INIT_METHOD_CALL                                         !1, 'buildMissingTree'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0          
    7    14        ASSIGN_OBJ                                               !1, 'formatOutput'
         15        OP_DATA                                                  <true>
    8    16        INIT_METHOD_CALL                                         !1, 'saveXML'
         17        DO_FCALL                                      0  $12     
         18        ECHO                                                     $12
  117    19      > RETURN                                                   1

Class EformDOMDocument:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ebVBR
function name:  __construct
number of ops:  13
compiled vars:  !0 = $version, !1 = $encoding
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV_INIT                                        !0      null
          1        RECV_INIT                                        !1      null
   15     2        INIT_STATIC_METHOD_CALL                                  
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
   16     6        NEW                                              $4      'DOMXPath'
          7        FETCH_THIS                                       $5      
          8        SEND_VAR_EX                                              $5
          9        DO_FCALL                                      0          
         10        ASSIGN_OBJ                                               'xpath'
         11        OP_DATA                                                  $4
   17    12      > RETURN                                                   null

End of function __construct

Function setattributex:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/ebVBR
function name:  setAttributeX
number of ops:  25
compiled vars:  !0 = $path, !1 = $attributeName, !2 = $attributeValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   21     3        FETCH_OBJ_R                                      ~3      'xpath'
          4        INIT_METHOD_CALL                                         ~3, 'query'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $4      
          7        FETCH_OBJ_R                                      ~5      $4, 'length'
          8        IS_EQUAL                                                 ~5, 0
          9      > JMPZ                                                     ~6, ->13
   22    10    >   INIT_METHOD_CALL                                         'buildMissingTree'
         11        SEND_VAR_EX                                              !0
         12        DO_FCALL                                      0          
   24    13    >   FETCH_OBJ_R                                      ~8      'xpath'
         14        INIT_METHOD_CALL                                         ~8, 'query'
         15        SEND_VAR_EX                                              !0
         16        DO_FCALL                                      0  $9      
         17        INIT_METHOD_CALL                                         $9, 'item'
         18        SEND_VAL_EX                                              0
         19        DO_FCALL                                      0  $10     
         20        INIT_METHOD_CALL                                         $10, 'setAttribute'
         21        SEND_VAR_EX                                              !1
         22        SEND_VAR_EX                                              !2
         23        DO_FCALL                                      0          
   25    24      > RETURN                                                   null

End of function setattributex

Function buildmissingtree:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
2 jumps found. (Code = 44) Position 1 = 72, Position 2 = 4
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 89
Branch analysis from position: 89
2 jumps found. (Code = 44) Position 1 = 95, Position 2 = 85
Branch analysis from position: 95
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 85
2 jumps found. (Code = 44) Position 1 = 95, Position 2 = 85
Branch analysis from position: 95
Branch analysis from position: 85
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 24
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 62
Branch analysis from position: 42
2 jumps found. (Code = 44) Position 1 = 72, Position 2 = 4
Branch analysis from position: 72
Branch analysis from position: 4
Branch analysis from position: 62
filename:       /in/ebVBR
function name:  buildMissingTree
number of ops:  96
compiled vars:  !0 = $path, !1 = $missingNodes, !2 = $tempPath, !3 = $tempName, !4 = $tempHolder, !5 = $tempElement, !6 = $attParam, !7 = $attribute, !8 = $currentElement, !9 = $newElement
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   28     1        ASSIGN                                                   !1, <array>
   29     2        ASSIGN                                                   !2, !0
   31     3      > JMP                                                      ->65
   32     4    >   INIT_FCALL                                               'basename'
          5        SEND_VAR                                                 !2
          6        DO_ICALL                                         $12     
          7        ASSIGN                                                   !3, $12
   33     8        INIT_FCALL                                               'dirname'
          9        SEND_VAR                                                 !2
         10        DO_ICALL                                         $14     
         11        ASSIGN                                                   !2, $14
   35    12        INIT_FCALL                                               'strpos'
         13        SEND_VAR                                                 !3
         14        SEND_VAL                                                 '%5B'
         15        DO_ICALL                                         $16     
         16        TYPE_CHECK                                    4          $16
         17      > JMPZ                                                     ~17, ->24
   37    18    >   INIT_METHOD_CALL                                         'createElement'
         19        SEND_VAR_EX                                              !3
         20        DO_FCALL                                      0  $19     
         21        ASSIGN_DIM                                               !1
         22        OP_DATA                                                  $19
         23      > JMP                                                      ->65
   40    24    >   INIT_FCALL                                               'explode'
         25        SEND_VAL                                                 '%5B'
         26        SEND_VAR                                                 !3
         27        DO_ICALL                                         $20     
         28        ASSIGN                                                   !4, $20
   41    29        INIT_METHOD_CALL                                         'createElement'
         30        CHECK_FUNC_ARG                                           
         31        FETCH_DIM_FUNC_ARG                               $22     !4, 0
         32        SEND_FUNC_ARG                                            $22
         33        DO_FCALL                                      0  $23     
         34        ASSIGN                                                   !5, $23
   44    35        INIT_FCALL                                               'strpos'
         36        FETCH_DIM_R                                      ~25     !4, 1
         37        SEND_VAL                                                 ~25
         38        SEND_VAL                                                 '%40'
         39        DO_ICALL                                         $26     
         40        TYPE_CHECK                                  1018          $26
         41      > JMPZ                                                     ~27, ->62
   46    42    >   INIT_FCALL                                               'str_replace'
         43        SEND_VAL                                                 <array>
         44        SEND_VAL                                                 ''
         45        FETCH_DIM_R                                      ~28     !4, 1
         46        SEND_VAL                                                 ~28
         47        DO_ICALL                                         $29     
         48        ASSIGN                                                   !6, $29
   47    49        INIT_FCALL                                               'explode'
         50        SEND_VAL                                                 '%3D'
         51        SEND_VAR                                                 !6
         52        DO_ICALL                                         $31     
         53        ASSIGN                                                   !7, $31
   48    54        INIT_METHOD_CALL                                         !5, 'setAttribute'
         55        CHECK_FUNC_ARG                                           
         56        FETCH_DIM_FUNC_ARG                               $33     !7, 0
         57        SEND_FUNC_ARG                                            $33
         58        CHECK_FUNC_ARG                                           
         59        FETCH_DIM_FUNC_ARG                               $34     !7, 1
         60        SEND_FUNC_ARG                                            $34
         61        DO_FCALL                                      0          
   52    62    >   ASSIGN_DIM                                               !1
         63        OP_DATA                                                  !5
   53    64        ASSIGN                                                   !5, null
   31    65    >   FETCH_OBJ_R                                      ~38     'xpath'
         66        INIT_METHOD_CALL                                         ~38, 'query'
         67        SEND_VAR_EX                                              !2
         68        DO_FCALL                                      0  $39     
         69        FETCH_OBJ_R                                      ~40     $39, 'length'
         70        IS_EQUAL                                                 ~40, 0
         71      > JMPNZ                                                    ~41, ->4
   58    72    >   ECHO                                                     !2
   59    73        INIT_FCALL                                               'var_dump'
         74        SEND_VAR                                                 !1
         75        DO_ICALL                                                 
   61    76        FETCH_OBJ_R                                      ~43     'xpath'
         77        INIT_METHOD_CALL                                         ~43, 'query'
         78        SEND_VAR_EX                                              !2
         79        DO_FCALL                                      0  $44     
         80        INIT_METHOD_CALL                                         $44, 'item'
         81        SEND_VAL_EX                                              0
         82        DO_FCALL                                      0  $45     
         83        ASSIGN                                                   !8, $45
   62    84      > JMP                                                      ->89
   63    85    >   INIT_METHOD_CALL                                         !8, 'appendChild'
         86        SEND_VAR_EX                                              !9
         87        DO_FCALL                                      0          
   64    88        ASSIGN                                                   !8, !9
   62    89    >   INIT_FCALL                                               'array_pop'
         90        SEND_REF                                                 !1
         91        DO_ICALL                                         $49     
         92        TYPE_CHECK                                  1020  ~50     $49
         93        ASSIGN                                           ~51     !9, ~50
         94      > JMPNZ                                                    ~51, ->85
   66    95    > > RETURN                                                   null

End of function buildmissingtree

Function haselement:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 29
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 14
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 25
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 14
Branch analysis from position: 29
Branch analysis from position: 14
Branch analysis from position: 29
filename:       /in/ebVBR
function name:  hasElement
number of ops:  31
compiled vars:  !0 = $name, !1 = $attribute, !2 = $elements, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
   75     2        INIT_METHOD_CALL                                         'getElementsByTagName'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $4      
          5        ASSIGN                                                   !2, $4
   77     6        FETCH_OBJ_R                                      ~6      !2, 'length'
          7        IS_SMALLER                                               0, ~6
          8      > JMPZ                                                     ~7, ->29
   78     9    >   IS_EQUAL                                                 !1, ''
         10      > JMPZ                                                     ~8, ->12
   79    11    > > RETURN                                                   <true>
   83    12    >   ASSIGN                                                   !3, 0
         13      > JMP                                                      ->26
   85    14    >   INIT_METHOD_CALL                                         !2, 'item'
         15        SEND_VAR_EX                                              !3
         16        DO_FCALL                                      0  $10     
         17        INIT_METHOD_CALL                                         $10, 'hasAttribute'
         18        SEND_VAR_EX                                              !1
         19        DO_FCALL                                      0  $11     
         20      > JMPZ                                                     $11, ->25
   86    21    >   INIT_METHOD_CALL                                         !2, 'item'
         22        SEND_VAR_EX                                              !3
         23        DO_FCALL                                      0  $12     
         24      > RETURN                                                   $12
   83    25    >   PRE_INC                                                  !3
         26    >   FETCH_OBJ_R                                      ~14     !2, 'length'
         27        IS_SMALLER                                               !3, ~14
         28      > JMPNZ                                                    ~15, ->14
   92    29    > > RETURN                                                   <false>
   93    30*     > RETURN                                                   null

End of function haselement

Function createelementwithattributes:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 30
Branch analysis from position: 8
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 29
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 29
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
Branch analysis from position: 30
filename:       /in/ebVBR
function name:  createElementWithAttributes
number of ops:  32
compiled vars:  !0 = $name, !1 = $attributeParameters, !2 = $element, !3 = $attributes, !4 = $attribute, !5 = $keyvalue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  101     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
  102     2        INIT_METHOD_CALL                                         'createElement'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $6      
          5        ASSIGN                                                   !2, $6
  104     6        IS_NOT_EQUAL                                             !1, ''
          7      > JMPZ                                                     ~8, ->30
  106     8    >   INIT_FCALL                                               'explode'
          9        SEND_VAL                                                 '%3B'
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                         $9      
         12        ASSIGN                                                   !3, $9
  108    13      > FE_RESET_R                                       $11     !3, ->29
         14    > > FE_FETCH_R                                               $11, !4, ->29
  109    15    >   INIT_FCALL                                               'explode'
         16        SEND_VAL                                                 '%3D'
         17        SEND_VAR                                                 !4
         18        DO_ICALL                                         $12     
         19        ASSIGN                                                   !5, $12
  110    20        INIT_METHOD_CALL                                         !2, 'setAttribute'
         21        CHECK_FUNC_ARG                                           
         22        FETCH_DIM_FUNC_ARG                               $14     !5, 0
         23        SEND_FUNC_ARG                                            $14
         24        CHECK_FUNC_ARG                                           
         25        FETCH_DIM_FUNC_ARG                               $15     !5, 1
         26        SEND_FUNC_ARG                                            $15
         27        DO_FCALL                                      0          
  108    28      > JMP                                                      ->14
         29    >   FE_FREE                                                  $11
  114    30    > > RETURN                                                   !2
  115    31*     > RETURN                                                   null

End of function createelementwithattributes

End of class EformDOMDocument.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.6 ms | 1416 KiB | 27 Q