File tree 4 files changed +27
-7
lines changed
4 files changed +27
-7
lines changed Original file line number Diff line number Diff line change @@ -30,13 +30,14 @@ abstract class StaticEntity implements StaticEntityInterface
30
30
/**
31
31
* Get a static entity instance
32
32
*
33
- * @param mixed $identifier
33
+ * @param mixed $id
34
+ * @param boolean $forceInstance Whether returns an empty instance when no result
34
35
*
35
36
* @return mixed
36
37
*/
37
- static public function get ($ identifier )
38
+ static public function get ($ id , $ forceInstance = false )
38
39
{
39
- return self ::getManager ()->get ($ identifier );
40
+ return self ::getManager ()->get ($ id , $ forceInstance );
40
41
}
41
42
42
43
/**
Original file line number Diff line number Diff line change @@ -50,16 +50,18 @@ public function __construct($staticEntityClass)
50
50
}
51
51
52
52
/**
53
- * @param mixed $id
53
+ * @param mixed $id
54
+ * @param boolean $forceInstance Whether returns an empty instance when no result
54
55
*
55
56
* @return StaticEntity|null
56
57
*/
57
- public function get ($ id )
58
+ public function get ($ id, $ forceInstance = false )
58
59
{
59
60
if (array_key_exists ($ id , $ this ->instances )) {
60
- return $ this ->instances [$ id ];
61
+ return null === $ this -> instances [ $ id ] ? $ this -> reflection -> newInstance () : $ this ->instances [$ id ];
61
62
} elseif (!$ data = $ this ->getData ($ id )) {
62
- return $ this ->instances [$ id ] = null ;
63
+ $ this ->instances [$ id ] = null ;
64
+ return $ forceInstance ? $ this ->reflection ->newInstance () : null ;
63
65
}
64
66
65
67
$ this ->instances [$ id ] = $ this ->reflection ->newInstance ();
Original file line number Diff line number Diff line change @@ -37,6 +37,15 @@ public function testNotFound()
37
37
$ this ->assertNull ($ civility );
38
38
}
39
39
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
+
40
49
public function testSameInstances ()
41
50
{
42
51
$ builder = new StaticEntityManager ('\Byscripts\StaticEntity\Tests\Fixtures\Civility ' );
Original file line number Diff line number Diff line change @@ -33,6 +33,14 @@ public function testNotFound()
33
33
$ this ->assertNull ($ civility );
34
34
}
35
35
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
+
36
44
public function testSameInstances ()
37
45
{
38
46
$ civility1 = Civility::get ('mr ' );
You can’t perform that action at this time.
0 commit comments