Skip to content

Commit

Permalink
Fix reading zero value
Browse files Browse the repository at this point in the history
  • Loading branch information
mtvbrianking committed Apr 15, 2024
1 parent 419e60e commit 19f1187
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Support/XmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class XmlElement extends \SimpleXMLElement
*/
public function get($attr, $default = null)
{
if (empty($this->{$attr})) {
if (is_null($this->{$attr})) {
return $default;
}

Expand Down
24 changes: 19 additions & 5 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,25 @@ public function testXmlStrIsValidAgainstXsdSchema()

public function testXmlElementGetter()
{
$user = new XmlElement('<document><alias>jdoe</alias></document>');

static::assertSame('jdoe', (string) $user->get('alias'));
static::assertNull($user->get('email'));
static::assertFalse((bool) $user->get('is_admin', false));
$xmlStr = <<<XML
<variables>
<empty/>
<zero>0</zero>
<false>false</false>
<!--<missing/>-->
<key>value</key>
<number>3</number>
</variables>
XML;

$variables = new XmlElement($xmlStr);

static::assertEmpty((string) $variables->get('empty'));
static::assertSame('0', (string) $variables->get('zero'));
static::assertSame(0, (int) $variables->get('zero'));
static::assertSame('false', (string) $variables->get('false'));
static::assertSame('value', (string) $variables->get('key'));
static::assertSame(3, (int) $variables->get('number'));
}

// Bmatovu\LaravelXml\Http\Middleware\RequireXml
Expand Down

0 comments on commit 19f1187

Please sign in to comment.