Skip to content

Commit 43bda2a

Browse files
committed
::get() now take a flag as second parameter to force empty instance when no result (instead of null)
1 parent b5f46ac commit 43bda2a

4 files changed

+27
-7
lines changed

src/StaticEntity.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ abstract class StaticEntity implements StaticEntityInterface
3030
/**
3131
* Get a static entity instance
3232
*
33-
* @param mixed $identifier
33+
* @param mixed $id
34+
* @param boolean $forceInstance Whether returns an empty instance when no result
3435
*
3536
* @return mixed
3637
*/
37-
static public function get($identifier)
38+
static public function get($id, $forceInstance = false)
3839
{
39-
return self::getManager()->get($identifier);
40+
return self::getManager()->get($id, $forceInstance);
4041
}
4142

4243
/**

src/StaticEntityManager.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@ public function __construct($staticEntityClass)
5050
}
5151

5252
/**
53-
* @param mixed $id
53+
* @param mixed $id
54+
* @param boolean $forceInstance Whether returns an empty instance when no result
5455
*
5556
* @return StaticEntity|null
5657
*/
57-
public function get($id)
58+
public function get($id, $forceInstance = false)
5859
{
5960
if (array_key_exists($id, $this->instances)) {
60-
return $this->instances[$id];
61+
return null === $this->instances[$id] ? $this->reflection->newInstance() : $this->instances[$id];
6162
} elseif (!$data = $this->getData($id)) {
62-
return $this->instances[$id] = null;
63+
$this->instances[$id] = null;
64+
return $forceInstance ? $this->reflection->newInstance() : null;
6365
}
6466

6567
$this->instances[$id] = $this->reflection->newInstance();

tests/StaticEntityBuilderTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ public function testNotFound()
3737
$this->assertNull($civility);
3838
}
3939

40+
public function testNotFoundForceInstance()
41+
{
42+
$builder = new StaticEntityManager('\Byscripts\StaticEntity\Tests\Fixtures\Civility');
43+
$civility = $builder->get('not-exists', true);
44+
45+
$this->assertInstanceOf('\Byscripts\StaticEntity\Tests\Fixtures\Civility', $civility);
46+
$this->assertNull($civility->getId());
47+
}
48+
4049
public function testSameInstances()
4150
{
4251
$builder = new StaticEntityManager('\Byscripts\StaticEntity\Tests\Fixtures\Civility');

tests/StaticEntityTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public function testNotFound()
3333
$this->assertNull($civility);
3434
}
3535

36+
public function testNotFoundForceInstance()
37+
{
38+
$civility = Civility::get('not-exists', true);
39+
40+
$this->assertInstanceOf('\Byscripts\StaticEntity\Tests\Fixtures\Civility', $civility);
41+
$this->assertNull($civility->getId());
42+
}
43+
3644
public function testSameInstances()
3745
{
3846
$civility1 = Civility::get('mr');

0 commit comments

Comments
 (0)