Skip to content

Commit e32af3b

Browse files
committed
Fixed an issue where "0" as an attribute value would be treated as false or null. Resolves #2
1 parent a434578 commit e32af3b

9 files changed

+118
-103
lines changed

src/Data/XMLElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class XMLElement extends SimpleXMLElement
1616
*/
1717
public function attribute(string $name, mixed $default = null): mixed
1818
{
19-
return (string) $this->attributes()->{$name} ?: $default;
19+
return $this->attributes()->{$name} ?? $default;
2020
}
2121

2222
/**
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
{
2-
"@attributes": {
3-
"count": "1"
4-
},
5-
"note": [
6-
{
7-
"@attributes": {
8-
"completed": "true"
9-
},
10-
"to.user": "test",
11-
"to": "Foo",
12-
"from": "Bar",
13-
"heading": "Baz",
14-
"body": "FooBar!",
15-
"created_at": {},
16-
"updated_at": {},
17-
"completed_at": "01-01-1970 12:00"
18-
}
19-
]
2+
"@attributes": {
3+
"count": "1"
4+
},
5+
"note": [
6+
{
7+
"@attributes": {
8+
"completed": "true",
9+
"stars": "0",
10+
"shares": "1"
11+
},
12+
"to.user": "test",
13+
"to": "Foo",
14+
"from": "Bar",
15+
"heading": "Baz",
16+
"body": "FooBar!",
17+
"created_at": {},
18+
"updated_at": {},
19+
"completed_at": "01-01-1970 12:00"
20+
}
21+
]
2022
}
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
{
2-
"@attributes": {
3-
"count": "1"
4-
},
5-
"note": [
6-
{
7-
"@attributes": {
8-
"completed": "true"
9-
},
10-
"to.user": "test",
11-
"to": "Foo",
12-
"from": "Bar",
13-
"heading": "Baz",
14-
"body": "FooBar!",
15-
"created_at": {},
16-
"updated_at": {},
17-
"completed_at": "01-01-1970 12:00"
18-
}
19-
]
2+
"@attributes": {
3+
"count": "1"
4+
},
5+
"note": [
6+
{
7+
"@attributes": {
8+
"completed": "true",
9+
"stars": "0",
10+
"shares": "1"
11+
},
12+
"to.user": "test",
13+
"to": "Foo",
14+
"from": "Bar",
15+
"heading": "Baz",
16+
"body": "FooBar!",
17+
"created_at": {},
18+
"updated_at": {},
19+
"completed_at": "01-01-1970 12:00"
20+
}
21+
]
2022
}
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
{
2-
"@attributes": {
3-
"count": "1"
4-
},
5-
"note": {
62
"@attributes": {
7-
"completed": "true"
3+
"count": "1"
84
},
9-
"to.user": "test",
10-
"to": "Foo",
11-
"from": "Bar",
12-
"heading": "Baz",
13-
"body": "FooBar!",
14-
"created_at": {},
15-
"updated_at": {},
16-
"completed_at": "01-01-1970 12:00"
17-
}
5+
"note": {
6+
"@attributes": {
7+
"completed": "true",
8+
"stars": "0",
9+
"shares": "1"
10+
},
11+
"to.user": "test",
12+
"to": "Foo",
13+
"from": "Bar",
14+
"heading": "Baz",
15+
"body": "FooBar!",
16+
"created_at": {},
17+
"updated_at": {},
18+
"completed_at": "01-01-1970 12:00"
19+
}
1820
}
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
{
2-
"@attributes": {
3-
"count": "1"
4-
},
5-
"note": {
62
"@attributes": {
7-
"completed": "true"
3+
"count": "1"
84
},
9-
"to_user": "test",
10-
"to": "Foo",
11-
"from": "Bar",
12-
"heading": "Baz",
13-
"body": "FooBar!",
14-
"created_at": {
15-
"@attributes": null
16-
},
17-
"updated_at": {
18-
"@attributes": null
19-
},
20-
"completed_at": "01-01-1970 12:00"
21-
}
5+
"note": {
6+
"@attributes": {
7+
"completed": "true",
8+
"stars": "0",
9+
"shares": "1"
10+
},
11+
"to_user": "test",
12+
"to": "Foo",
13+
"from": "Bar",
14+
"heading": "Baz",
15+
"body": "FooBar!",
16+
"created_at": {
17+
"@attributes": null
18+
},
19+
"updated_at": {
20+
"@attributes": null
21+
},
22+
"completed_at": "01-01-1970 12:00"
23+
}
2224
}
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
{
2-
"@attributes": {
3-
"count": "1"
4-
},
5-
"note": {
62
"@attributes": {
7-
"completed": "true"
3+
"count": "1"
84
},
9-
"toUser": "test",
10-
"to": "Foo",
11-
"from": "Bar",
12-
"heading": "Baz",
13-
"body": "FooBar!",
14-
"createdAt": {
15-
"@attributes": null
16-
},
17-
"updatedAt": {
18-
"@attributes": null
19-
},
20-
"completedAt": "01-01-1970 12:00"
21-
}
5+
"note": {
6+
"@attributes": {
7+
"completed": "true",
8+
"stars": "0",
9+
"shares": "1"
10+
},
11+
"toUser": "test",
12+
"to": "Foo",
13+
"from": "Bar",
14+
"heading": "Baz",
15+
"body": "FooBar!",
16+
"createdAt": {
17+
"@attributes": null
18+
},
19+
"updatedAt": {
20+
"@attributes": null
21+
},
22+
"completedAt": "01-01-1970 12:00"
23+
}
2224
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<notes count="1">
2-
<note completed="true">
2+
<note completed="true" stars="0" shares="1">
33
<to.user>test</to.user>
44
<to attr="test">Foo</to>
55
<from>Bar</from>
@@ -9,4 +9,4 @@
99
<updated_at/>
1010
<completed_at>01-01-1970 12:00</completed_at>
1111
</note>
12-
</notes>
12+
</notes>

tests/Unit/XMLImportTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public function test_can_get_element_attributes()
3131
$xml = XML::import($path)->get()->note;
3232

3333
$this->assertEquals('true', $xml->attribute('completed'));
34+
$this->assertEquals('0', $xml->attribute('stars', '2'));
35+
$this->assertEquals('1', $xml->attribute('shares', '2'));
3436
$this->assertFalse($xml->hasAttribute('foobar'));
3537
$this->assertEquals('default value', $xml->attribute('baz', 'default value'));
38+
$this->assertEquals('default value', $xml->attribute('baz', 'default value'));
3639
}
3740
}
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
{
2-
"@attributes": {
3-
"count": "1"
4-
},
5-
"note": {
62
"@attributes": {
7-
"completed": "true"
3+
"count": "1"
84
},
9-
"to.user": "test",
10-
"to": "Foo",
11-
"from": "Bar",
12-
"heading": "Baz",
13-
"body": "FooBar!",
14-
"created_at": {},
15-
"updated_at": {},
16-
"completed_at": "01-01-1970 12:00"
17-
}
5+
"note": {
6+
"@attributes": {
7+
"completed": "true",
8+
"stars": "0",
9+
"shares": "1"
10+
},
11+
"to.user": "test",
12+
"to": "Foo",
13+
"from": "Bar",
14+
"heading": "Baz",
15+
"body": "FooBar!",
16+
"created_at": {},
17+
"updated_at": {},
18+
"completed_at": "01-01-1970 12:00"
19+
}
1820
}

0 commit comments

Comments
 (0)