3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xpath = "/bids_offers/market_submit/sem_gen_technical_offer/block_deloading/point"; $xml = new EformDOMDocument(); //$root = $xml->createElement("bids_offers"); //$xml->appendChild($root); $str = <<<XML <?xml version="1.0" encoding="UTF-8" standalone="no"?> <bids_offers> </bids_offers> XML; $xml->loadXML($str); $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[0]); $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/4Qnuv
function name:  (null)
number of ops:  17
compiled vars:  !0 = $xpath, !1 = $xml, !2 = $str
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'
    3     1        NEW                                              $4      'EformDOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $4
    6     4        ASSIGN                                                   !2, '%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22+standalone%3D%22no%22%3F%3E%0A%3Cbids_offers%3E%0A%3C%2Fbids_offers%3E'
   11     5        INIT_METHOD_CALL                                         !1, 'loadXML'
          6        SEND_VAR_EX                                              !2
          7        DO_FCALL                                      0          
   12     8        INIT_METHOD_CALL                                         !1, 'buildMissingTree'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0          
   13    11        ASSIGN_OBJ                                               !1, 'formatOutput'
         12        OP_DATA                                                  <true>
   14    13        INIT_METHOD_CALL                                         !1, 'saveXML'
         14        DO_FCALL                                      0  $11     
         15        ECHO                                                     $11
  123    16      > RETURN                                                   1

Class EformDOMDocument:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4Qnuv
function name:  __construct
number of ops:  13
compiled vars:  !0 = $version, !1 = $encoding
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV_INIT                                        !0      null
          1        RECV_INIT                                        !1      null
   21     2        INIT_STATIC_METHOD_CALL                                  
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
   22     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
   23    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/4Qnuv
function name:  setAttributeX
number of ops:  25
compiled vars:  !0 = $path, !1 = $attributeName, !2 = $attributeValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   27     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
   28    10    >   INIT_METHOD_CALL                                         'buildMissingTree'
         11        SEND_VAR_EX                                              !0
         12        DO_FCALL                                      0          
   30    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          
   31    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 = 85
Branch analysis from position: 85
2 jumps found. (Code = 44) Position 1 = 91, Position 2 = 81
Branch analysis from position: 91
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 81
2 jumps found. (Code = 44) Position 1 = 91, Position 2 = 81
Branch analysis from position: 91
Branch analysis from position: 81
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/4Qnuv
function name:  buildMissingTree
number of ops:  92
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
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   34     1        ASSIGN                                                   !1, <array>
   35     2        ASSIGN                                                   !2, !0
   37     3      > JMP                                                      ->65
   38     4    >   INIT_FCALL                                               'basename'
          5        SEND_VAR                                                 !2
          6        DO_ICALL                                         $12     
          7        ASSIGN                                                   !3, $12
   39     8        INIT_FCALL                                               'dirname'
          9        SEND_VAR                                                 !2
         10        DO_ICALL                                         $14     
         11        ASSIGN                                                   !2, $14
   41    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
   43    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
   46    24    >   INIT_FCALL                                               'explode'
         25        SEND_VAL                                                 '%5B'
         26        SEND_VAR                                                 !3
         27        DO_ICALL                                         $20     
         28        ASSIGN                                                   !4, $20
   47    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
   50    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
   52    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
   53    49        INIT_FCALL                                               'explode'
         50        SEND_VAL                                                 '%3D'
         51        SEND_VAR                                                 !6
         52        DO_ICALL                                         $31     
         53        ASSIGN                                                   !7, $31
   54    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          
   58    62    >   ASSIGN_DIM                                               !1
         63        OP_DATA                                                  !5
   59    64        ASSIGN                                                   !5, null
   37    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
   67    72    >   FETCH_OBJ_R                                      ~42     'xpath'
         73        INIT_METHOD_CALL                                         ~42, 'query'
         74        SEND_VAR_EX                                              !2
         75        DO_FCALL                                      0  $43     
         76        INIT_METHOD_CALL                                         $43, 'item'
         77        SEND_VAL_EX                                              0
         78        DO_FCALL                                      0  $44     
         79        ASSIGN                                                   !8, $44
   68    80      > JMP                                                      ->85
   69    81    >   INIT_METHOD_CALL                                         !8, 'appendChild'
         82        SEND_VAR_EX                                              !9
         83        DO_FCALL                                      0          
   70    84        ASSIGN                                                   !8, !9
   68    85    >   INIT_FCALL                                               'array_pop'
         86        SEND_REF                                                 !1
         87        DO_ICALL                                         $48     
         88        ASSIGN                                           ~49     !9, $48
         89        TYPE_CHECK                                  1020          ~49
         90      > JMPNZ                                                    ~50, ->81
   72    91    > > 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/4Qnuv
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
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
   81     2        INIT_METHOD_CALL                                         'getElementsByTagName'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $4      
          5        ASSIGN                                                   !2, $4
   83     6        FETCH_OBJ_R                                      ~6      !2, 'length'
          7        IS_SMALLER                                               0, ~6
          8      > JMPZ                                                     ~7, ->29
   84     9    >   IS_EQUAL                                                 !1, ''
         10      > JMPZ                                                     ~8, ->12
   85    11    > > RETURN                                                   <true>
   89    12    >   ASSIGN                                                   !3, 0
         13      > JMP                                                      ->26
   91    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
   92    21    >   INIT_METHOD_CALL                                         !2, 'item'
         22        SEND_VAR_EX                                              !3
         23        DO_FCALL                                      0  $12     
         24      > RETURN                                                   $12
   89    25    >   PRE_INC                                                  !3
         26    >   FETCH_OBJ_R                                      ~14     !2, 'length'
         27        IS_SMALLER                                               !3, ~14
         28      > JMPNZ                                                    ~15, ->14
   98    29    > > RETURN                                                   <false>
   99    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/4Qnuv
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
-------------------------------------------------------------------------------------
  107     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
  108     2        INIT_METHOD_CALL                                         'createElement'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $6      
          5        ASSIGN                                                   !2, $6
  110     6        IS_NOT_EQUAL                                             !1, ''
          7      > JMPZ                                                     ~8, ->30
  112     8    >   INIT_FCALL                                               'explode'
          9        SEND_VAL                                                 '%3B'
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                         $9      
         12        ASSIGN                                                   !3, $9
  114    13      > FE_RESET_R                                       $11     !3, ->29
         14    > > FE_FETCH_R                                               $11, !4, ->29
  115    15    >   INIT_FCALL                                               'explode'
         16        SEND_VAL                                                 '%3D'
         17        SEND_VAR                                                 !4
         18        DO_ICALL                                         $12     
         19        ASSIGN                                                   !5, $12
  116    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          
  114    28      > JMP                                                      ->14
         29    >   FE_FREE                                                  $11
  120    30    > > RETURN                                                   !2
  121    31*     > RETURN                                                   null

End of function createelementwithattributes

End of class EformDOMDocument.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.01 ms | 1408 KiB | 25 Q