@@ -53,32 +53,30 @@ final public function __construct()
5353 */
5454 public function __get ($ key )
5555 {
56- $ keyTypeName = $ this ->keyType ($ key );
56+ $ keyType = $ this ->keyType ($ key );
5757 $ keyDataType = $ this ->dataType ($ key );
58- if (isset ( $ this -> $ keyTypeName ) && !isset ($ this ->processed [$ key ])) {
58+ if ($ keyType && !isset ($ this ->processed [$ key ])) {
5959 if (isset ($ this ->modelData [$ key ])) {
6060 $ val = $ this ->modelData [$ key ];
61- } else if (isset ($ this ->$ keyDataType ) &&
62- ($ this ->$ keyDataType == 'array ' || $ this ->$ keyDataType == 'map ' )) {
61+ } elseif ($ keyDataType == 'array ' || $ keyDataType == 'map ' ) {
6362 $ val = array ();
6463 } else {
6564 $ val = null ;
6665 }
6766
6867 if ($ this ->isAssociativeArray ($ val )) {
69- if (isset ( $ this -> $ keyDataType) && 'map ' == $ this -> $ keyDataType ) {
68+ if ($ keyDataType && 'map ' == $ keyDataType ) {
7069 foreach ($ val as $ arrayKey => $ arrayItem ) {
7170 $ this ->modelData [$ key ][$ arrayKey ] =
72- $ this -> createObjectFromName ( $ keyTypeName , $ arrayItem );
71+ new $ keyType ( $ arrayItem );
7372 }
7473 } else {
75- $ this ->modelData [$ key ] = $ this -> createObjectFromName ( $ keyTypeName , $ val );
74+ $ this ->modelData [$ key ] = new $ keyType ( $ val );
7675 }
7776 } else if (is_array ($ val )) {
7877 $ arrayObject = array ();
7978 foreach ($ val as $ arrayIndex => $ arrayItem ) {
80- $ arrayObject [$ arrayIndex ] =
81- $ this ->createObjectFromName ($ keyTypeName , $ arrayItem );
79+ $ arrayObject [$ arrayIndex ] = new $ keyType ($ arrayItem );
8280 }
8381 $ this ->modelData [$ key ] = $ arrayObject ;
8482 }
@@ -98,22 +96,21 @@ protected function mapTypes($array)
9896 {
9997 // Hard initialise simple types, lazy load more complex ones.
10098 foreach ($ array as $ key => $ val ) {
101- if (property_exists ($ this , $ this ->keyType ($ key ))) {
102- $ propertyClass = $ this ->{$ this ->keyType ($ key )};
103- $ dataType = $ this ->{$ this ->dataType ($ key )};
99+ if ($ keyType = $ this ->keyType ($ key )) {
100+ $ dataType = $ this ->dataType ($ key );
104101 if ($ dataType == 'array ' || $ dataType == 'map ' ) {
105102 $ this ->$ key = array ();
106103 foreach ($ val as $ itemKey => $ itemVal ) {
107- if ($ itemVal instanceof $ propertyClass ) {
104+ if ($ itemVal instanceof $ keyType ) {
108105 $ this ->{$ key }[$ itemKey ] = $ itemVal ;
109106 } else {
110- $ this ->{$ key }[$ itemKey ] = new $ propertyClass ($ itemVal );
107+ $ this ->{$ key }[$ itemKey ] = new $ keyType ($ itemVal );
111108 }
112109 }
113- } elseif ($ val instanceof $ propertyClass ) {
110+ } elseif ($ val instanceof $ keyType ) {
114111 $ this ->$ key = $ val ;
115112 } else {
116- $ this ->$ key = new $ propertyClass ($ val );
113+ $ this ->$ key = new $ keyType ($ val );
117114 }
118115 unset($ array [$ key ]);
119116 } elseif (property_exists ($ this , $ key )) {
@@ -234,19 +231,6 @@ protected function isAssociativeArray($array)
234231 return false ;
235232 }
236233
237- /**
238- * Given a variable name, discover its type.
239- *
240- * @param $name
241- * @param $item
242- * @return object The object from the item.
243- */
244- private function createObjectFromName ($ name , $ item )
245- {
246- $ type = $ this ->$ name ;
247- return new $ type ($ item );
248- }
249-
250234 /**
251235 * Verify if $obj is an array.
252236 * @throws Google_Exception Thrown if $obj isn't an array.
@@ -291,12 +275,21 @@ public function offsetUnset($offset)
291275
292276 protected function keyType ($ key )
293277 {
294- return $ key . "Type " ;
278+ $ keyType = $ key . "Type " ;
279+
280+ // ensure keyType is a valid class
281+ if (property_exists ($ this , $ keyType ) && class_exists ($ this ->$ keyType )) {
282+ return $ this ->$ keyType ;
283+ }
295284 }
296285
297286 protected function dataType ($ key )
298287 {
299- return $ key . "DataType " ;
288+ $ dataType = $ key . "DataType " ;
289+
290+ if (property_exists ($ this , $ dataType )) {
291+ return $ this ->$ dataType ;
292+ }
300293 }
301294
302295 public function __isset ($ key )
0 commit comments