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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kwenne opened this issue Jun 3, 2021 · 4 comments

Comments

@kwenne
Copy link

kwenne commented Jun 3, 2021

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);
@kwenne kwenne added the Bug label Jun 3, 2021
@kwenne kwenne changed the title Convert XML to PHP with XMLUtils::convertDomElementToArray : numbers who start by "0" convert into octal number Convert XML to PHP with XMLUtils::convertDomElementToArray : numbers who start by "0" convert into octal number (which is wrong) Jun 3, 2021
@kwenne kwenne changed the title Convert XML to PHP with XMLUtils::convertDomElementToArray : numbers who start by "0" convert into octal number (which is wrong) Convert XML to PHP array with XMLUtils::convertDomElementToArray : numbers who start by "0" convert into octal number (which is wrong) Jun 3, 2021
@alexandre-daubois
Copy link
Member

Hi, don't hesitate to create a pull-request with your solution, so the core team and the community will be able to easily review if it's the best way to do it! 👍

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@xabbuh
Copy link
Member

xabbuh commented Dec 6, 2021

Can you create a small example application that allows to reproduce your issue?

@alexandre-daubois
Copy link
Member

@xabbuh I'm looking into this.

@fabpot fabpot closed this as completed Dec 15, 2021
fabpot added a commit that referenced this issue Dec 15, 2021
…ring starting with a 0 (alexandre-daubois)

This PR was merged into the 4.4 branch.

Discussion
----------

[Config] In XmlUtils, avoid converting from octal every string starting with a 0

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #41532 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | N/A

One word on the use of `intval` instead of `octdec`: `octdec` leads to deprecation notices when testing a non-octal string:
```
Invalid characters passed for attempted conversion, these have been ignored
```
While `intval` doesn't, which worth the use in my opinion.

Commits
-------

30c3913 [Config] In XmlUtils, avoid converting from octal every string starting with a 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants