1818abstract class StaticEntity implements StaticEntityInterface
1919{
2020 /**
21- * @var \ReflectionClass[]
21+ * @var StaticEntityBuilder
2222 */
23- static private $ reflections = array () ;
23+ private static $ builders ;
2424
2525 /**
26- * @var array[]
27- */
28- static private $ dataSets = array ();
29-
30- /**
31- * @var array[]
26+ * @var mixed
3227 */
33- static private $ instances = array ();
34-
35- static private $ allLoaded = false ;
28+ protected $ id ;
3629
3730 /**
38- * @var mixed
31+ * Get a static entity instance
32+ *
33+ * @param mixed $identifier
34+ *
35+ * @return mixed
3936 */
40- protected $ id ;
37+ static public function get ($ identifier )
38+ {
39+ return self ::getBuilder ()->get ($ identifier );
40+ }
4141
4242 /**
4343 * Check the existence of the ID
@@ -50,11 +50,7 @@ abstract class StaticEntity implements StaticEntityInterface
5050 */
5151 static public function exists ($ id )
5252 {
53- $ class = self ::ensureClass (__METHOD__ );
54-
55- self ::initDataSet ($ class );
56-
57- return array_key_exists ($ id , self ::$ dataSets [$ class ]);
53+ return in_array ($ id , self ::getBuilder ()->getIds (), true );
5854 }
5955
6056 /**
@@ -66,19 +62,7 @@ static public function exists($id)
6662 */
6763 static public function getAll ()
6864 {
69- $ class = self ::ensureClass (__METHOD__ );
70-
71- if (self ::$ allLoaded [$ class ]) {
72- return self ::$ instances [$ class ];
73- }
74-
75- foreach (self ::getIds () as $ id ) {
76- self ::get ($ id );
77- }
78-
79- self ::$ allLoaded [$ class ] = true ;
80-
81- return self ::$ instances [$ class ];
65+ return self ::getBuilder ()->getAll ();
8266 }
8367
8468 /**
@@ -92,20 +76,11 @@ static public function getAll()
9276 */
9377 static public function getAssoc ($ valueKey = 'name ' )
9478 {
95- $ class = self ::ensureClass (__METHOD__ );
96-
97- self ::initDataSet ($ class );
98-
9979 if (empty ($ valueKey )) {
10080 $ valueKey = 'name ' ;
10181 }
10282
103- return array_map (
104- function ($ arr ) use ($ valueKey ) {
105- return $ arr [$ valueKey ];
106- },
107- self ::$ dataSets [$ class ]
108- );
83+ return self ::getBuilder ()->getAssociativeArray ($ valueKey );
10984 }
11085
11186 /**
@@ -117,11 +92,7 @@ function ($arr) use ($valueKey) {
11792 */
11893 static public function getIds ()
11994 {
120- $ class = self ::ensureClass (__METHOD__ );
121-
122- self ::initDataSet ($ class );
123-
124- return array_keys (self ::$ dataSets [$ class ]);
95+ return self ::getBuilder ()->getIds ();
12596 }
12697
12798 /**
@@ -145,94 +116,26 @@ static public function toId($idOrEntity)
145116 throw new \Exception (sprintf ('%s => Invalid parameter: %s ' , __METHOD__ , $ idOrEntity ));
146117 }
147118
119+
148120 /**
149- * @param mixed $id
121+ * Get the StaticEntity builder
150122 *
151123 * @throws \Exception
152- *
153- * @return null|static
124+ * @return StaticEntityBuilder
154125 */
155- static public function get ($ id )
156- {
157- $ class = self ::ensureClass (__METHOD__ );
158-
159- self ::init ($ class );
160-
161- if (array_key_exists ($ id , self ::$ instances [$ class ])) {
162- return self ::$ instances [$ class ][$ id ];
163- } elseif (!array_key_exists ($ id , self ::$ dataSets [$ class ])) {
164- return self ::$ instances [$ class ][$ id ] = null ;
165- }
166-
167- $ instance = new $ class ;
168-
169- foreach (self ::$ dataSets [$ class ][$ id ] as $ propertyName => $ propertyValue ) {
170- $ property = self ::$ reflections [$ class ]->getProperty ($ propertyName );
171- $ property ->setAccessible (true );
172- $ property ->setValue ($ instance , $ propertyValue );
173- }
174-
175- return self ::$ instances [$ class ][$ id ] = $ instance ;
176- }
177-
178- static private function init ($ class )
126+ private static function getBuilder ()
179127 {
180- if (array_key_exists ($ class , self ::$ instances )) {
181- return ;
182- }
128+ $ class = get_called_class ();
183129
184- self ::initDataSet ($ class );
185-
186- self ::$ instances [$ class ] = array ();
187- self ::$ reflections [$ class ] = new \ReflectionClass ($ class );
188- }
189-
190- private static function initDataSet ($ class )
191- {
192- if (array_key_exists ($ class , self ::$ dataSets )) {
193- return ;
130+ if (__CLASS__ === $ class ) {
131+ throw new \Exception ('You cannot call methods directly on StaticEntity class ' );
194132 }
195133
196- $ dataSet = static ::getDataSet ();
197-
198- if (!is_array ($ dataSet )) {
199- throw new \Exception (sprintf ('%s::getDataSet() must returns an array ' , $ class ));
200- }
201-
202- foreach ($ dataSet as $ id => $ data ) {
203- self ::checkData ($ class , $ id , $ data );
204- $ dataSet [$ id ]['id ' ] = $ id ;
205- }
206-
207- self ::$ dataSets [$ class ] = $ dataSet ;
208- }
209-
210- private static function checkData ($ class , $ id , $ data )
211- {
212- if (!is_array ($ data )) {
213- throw new \Exception (
214- sprintf ('%s::getDataSet() => Data at index "%s" must be an array ' , $ class , $ id )
215- );
216- }
217-
218- foreach (array_keys ($ data ) as $ property ) {
219- if (!property_exists ($ class , $ property )) {
220- throw new \Exception (
221- sprintf ('Property "%s" not exists in class "%s" ' , $ property , $ class )
222- );
223- }
224- }
225- }
226-
227- private static function ensureClass ($ method )
228- {
229- $ calledClass = get_called_class ();
230-
231- if (__CLASS__ === $ calledClass ) {
232- throw new \Exception (sprintf ('You cannot call %s() directly ' , $ method ));
134+ if (!isset (self ::$ builders [$ class ])) {
135+ self ::$ builders [$ class ] = new StaticEntityBuilder ($ class );
233136 }
234137
235- return $ calledClass ;
138+ return self :: $ builders [ $ class ] ;
236139 }
237140
238141 /**
0 commit comments