File tree Expand file tree Collapse file tree 4 files changed +27
-7
lines changed Expand file tree Collapse file tree 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
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 /**
Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff 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 ' );
Original file line number Diff line number Diff 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 ' );
You can’t perform that action at this time.
0 commit comments