Skip to content
New issue

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

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

Already on GitHub? # to your account

Numeric zero being read as empty string #17

Closed
mtvbrianking opened this issue Apr 15, 2024 · 1 comment
Closed

Numeric zero being read as empty string #17

mtvbrianking opened this issue Apr 15, 2024 · 1 comment

Comments

@mtvbrianking
Copy link
Owner

$xmlStr = <<<XML
<variables>
    <empty/>
    <zero>0</zero>
    <false>false</false>
    <!--<missing/>-->
</variables>
XML;

// $variables = new \SimpleXMLElement($xmlStr);
$variables = simplexml_load_string($xmlStr, \Bmatovu\LaravelXml\Support\XmlElement::class);

// Access via SimpleXMLElement::class
echo $variables->zero->__toString();  // Expected: "0", Actual: "0"

// Access via the package's XmlElement::class
echo (string) $variables->get('zero'); // Expected: "0", Actual: ""

Correction

XmlElement.php

  public function get($attr, $default = null)
  {
-     if (empty($this->{$attr})) {
+     if (is_null($this->{$attr})) {
          return $default;
      }

      return $this->{$attr};
  }
@mtvbrianking
Copy link
Owner Author

Fixed . 19f1187

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant