Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Convert XML to PHP array with XMLUtils::convertDomElementToArray : numbers who start by "0" convert into octal number (which is wrong) #41532

Closed
@kwenne

Description

@kwenne

Symfony version(s) affected: 5.2.5 (at least)

Description
If you have some value in your XML file with a number starting by "0" (example : 080000 ), XMLUtils::convertDomElementToArray will return "ArrivalTime" => 0, which isn't correct.

How to reproduce

Let's take the following XML file :

$xml_file = <?xml version="1.0"?><ResponseItinerary><Header><CruiseLineCode>SBN</CruiseLineCode><SubsystemId>3</SubsystemId><AgencyId1>58080</AgencyId1><AgencyId2>58080</AgencyId2><Currency/><AgencyConsumer/><TransactionCounter/></Header><Itinerary><GeneralInfo><Errors/><Warnings/><HowMuch>29</HowMuch></GeneralInfo><List ItineraryCode="E1N12BVN" PackageId="V175"><ListElement><DWeek>SAMEDI</DWeek><DepartureDate>11/12/2021</DepartureDate><PortName Code="GRW" Activity="">GREENWICH (LONDON)</PortName><ArrivalTime>090000</ArrivalTime><DepartureTime>000000</DepartureTime><Indicator/></ListElement></List></Itinerary></ResponseItinerary>;

Let's convert it into PHP file using XmlUtils Symfony class :

$oDom = new \DOMDocument;
$oDom->preserveWhiteSpace = false;
$oDom->loadXML($xml_file);
$oDom->formatOutput = true;
  
$xml_list_element = $oDom->getElementsByTagName('List')->item(0); 

$content = self::convertDomElementToArray($content);
dump($content);

// Here is the dump 
/*
array:3 [
  "ItineraryCode" => "E1N12BVN"
  "PackageId" => "V175"
  "ListElement" => array:16 [
    0 => array:6 [
      "DWeek" => "SAMEDI"
      "DepartureDate" => "11/12/2021"
      "PortName" => array:3 [
        "Code" => "GRW"
        "Activity" => ""
        "value" => "GREENWICH (LONDON)"
      ]
      "ArrivalTime" => 0 // IT SHOULD BE "090000" 
      "DepartureTime" => 0
      "Indicator" => null
    ]
]
*/

The "ArrivalTime" property shouldn't show 0, but 090000.

Possible Solution
Here's the solution :

  1. add the following function into app/vendor/symfony/config/Util/XmlUtils.php
    private function is_octal($x) { return decoct(octdec($x)) == $x; }
  2. change a line in public static function phpize() (still into app/vendor/symfony/config/Util/XmlUtils.php)
    change this
    return '0' == $value[0] ? octdec($value) : (((string) $raw === (string) $cast) ? $cast : $raw);
    into this
    return self::is_octal($value) ? octdec($value) : (((string) $raw === (string) $cast) ? $cast : $raw);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions