Skip to content

Commit b5f46ac

Browse files
committed
Fix error when converting empty value to id. Now return null
1 parent 151f368 commit b5f46ac

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"name": "byscripts/static-entity",
33
"type": "library",
44
"description": "Provide some kind of static entities",
5-
"minimum-stability": "dev",
65
"license": "MIT",
76
"authors": [
87
{

src/StaticEntityManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ public function hasId($id)
131131
*/
132132
public function convertToId($staticEntity)
133133
{
134-
if ($staticEntity instanceof StaticEntity) {
134+
if (empty($staticEntity)) {
135+
return null;
136+
} elseif ($staticEntity instanceof StaticEntity) {
135137
return $staticEntity->getId();
136138
} elseif (!$this->hasId($staticEntity)) {
137139
throw new \Exception(

tests/StaticEntityBuilderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ public function testToId()
9898

9999
$this->assertEquals('mr', $builder->convertToId($civility));
100100
$this->assertEquals('mr', $builder->convertToId('mr'));
101+
}
101102

103+
public function testEmptyToId()
104+
{
105+
$builder = new StaticEntityManager('\Byscripts\StaticEntity\Tests\Fixtures\Civility');
106+
$this->assertNull($builder->convertToId(""));
102107
}
103108

104109
/**

tests/StaticEntityTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ public function testToId()
8585

8686
$this->assertEquals('mr', Civility::toId($civility));
8787
$this->assertEquals('mr', Civility::toId('mr'));
88+
}
8889

90+
public function testEmptyToId()
91+
{
92+
$this->assertNull(Civility::toId(""));
8993
}
9094

9195
/**

0 commit comments

Comments
 (0)