1515
1616use OpenLRW \OpenLRW ;
1717use OpenLRW \Exception \InternalServerErrorException ;
18+ use Symfony \Component \Debug \Exception \UndefinedFunctionException ;
1819
1920abstract class OneRoster extends Model
2021{
@@ -41,7 +42,7 @@ protected static function generateHeader(): array
4142 ];
4243 }
4344
44- public static function find ($ id )
45+ protected static function find ($ id )
4546 {
4647 $ header = self ::generateHeader ();
4748 $ json = OpenLRW::httpGet (self ::PREFIX . static ::$ collection . "/ $ id " , $ header );
@@ -52,13 +53,29 @@ public static function getClassName(){
5253 return (new \ReflectionClass (get_called_class ()))->getShortName ();
5354 }
5455
56+ /**
57+ * Return the real content of a OneRoster object
58+ *
59+ * @param $object
60+ * @return mixed
61+ */
5562 protected static function extract ($ object )
5663 {
57- $ name = mb_strtolower (self ::getClassName ());
58- return $ object ->$ name ;
64+ try {
65+ $ name = mb_strtolower (self ::getClassName ());
66+ return $ object ->$ name ;
67+ } catch (\ErrorException $ e ) {
68+ return $ object ;
69+ }
70+
5971 }
6072
61- public static function all ()
73+ /**
74+ * Return a list of objects
75+ *
76+ * @return array
77+ */
78+ protected static function all ()
6279 {
6380 $ header = self ::generateHeader ();
6481 $ json_array = OpenLRW::httpGet (self ::PREFIX . static ::$ collection , $ header );
@@ -71,7 +88,7 @@ public static function all()
7188 return $ results ;
7289 }
7390
74- public static function save ($ data = null )
91+ protected static function save ($ data = null )
7592 {
7693 if ($ data === null ) {
7794 $ data = static ::toJson ();
@@ -80,7 +97,7 @@ public static function save($data = null)
8097 return OpenLRW::httpPost (self ::PREFIX . static ::$ collection , $ header , $ data );
8198 }
8299
83- public static function update ($ id , $ data )
100+ protected static function update ($ id , $ data )
84101 {
85102 $ header = self ::generateHeader ();
86103 return OpenLRW::httpPatch (self ::PREFIX . static ::$ collection ."/ $ id " , $ header , $ data );
@@ -92,7 +109,7 @@ public static function update($id, $data)
92109 *
93110 * @return int
94111 */
95- public static function destroy ($ id )
112+ protected static function destroy ($ id )
96113 {
97114 $ header = self ::generateHeader ();
98115 return OpenLRW::httpDelete (self ::PREFIX . static ::$ collection . "/ $ id " , $ header );
@@ -102,18 +119,49 @@ public static function destroy($id)
102119 * Delete the model from the database.
103120 *
104121 */
105- public function delete ()
122+ protected function delete ()
106123 {
107124 return self ::destroy ($ this ->getPrimaryKeyValue ());
108125 }
109126
110- public static function get ($ route )
127+ /**
128+ * HTTP Get that returns a generic array
129+ *
130+ * @param $route
131+ * @return mixed|null
132+ */
133+ protected static function httpGet ($ route )
111134 {
112135 $ header = self ::generateHeader ();
113136 return OpenLRW::httpGet (self ::PREFIX . $ route , $ header );
114137 }
115138
116- public static function post ($ route , $ data )
139+ /**
140+ * HTTP Get that returns an object or a collection of objects
141+ *
142+ * @param $route
143+ * @param $class the type of the object wanted
144+ * @return mixed
145+ */
146+ protected static function get ($ route , $ class ) {
147+ $ header = self ::generateHeader ();
148+ $ json = OpenLRW::httpGet (self ::PREFIX . $ route , $ header );
149+ if (count ($ json ) < 2 ) {
150+ return new static ((array )$ json [0 ]);
151+ } else {
152+ $ results = [];
153+ foreach ($ json as $ item ) {
154+ $ object = self ::extract ($ item );
155+ $ results [] = new $ class ((array )$ object );
156+ }
157+
158+ return $ results ;
159+ }
160+
161+ }
162+
163+
164+ protected static function post ($ route , $ data )
117165 {
118166 $ header = self ::generateHeader ();
119167 return OpenLRW::httpPost (self ::PREFIX . $ route , $ header , $ data );
0 commit comments