@@ -26,6 +26,13 @@ class FacadeHelper{
26
26
public static function reflectArrayToObject ($ classNameOrKeyName , $ data , $ throwException = TRUE ) {
27
27
if (!isset ($ classNameOrKeyName )){ throw new \Exception ("The Class Name or Key Name cannot be NULL when generating Objects. " );}
28
28
if (!isset ($ data ) || empty ($ data )){ throw new \Exception ("The passed data cannot be NULL. " );}
29
+ if (is_object ($ data )){
30
+ if (!Facadehelper::checkIfTheObjectIsAnInstanceOfTheClass ($ classNameOrKeyName ,$ data )){
31
+ throw new \Exception ("The assigned object is not an instance of required object:{ " . $ classNameOrKeyName . "}. " );
32
+ }else {
33
+ return $ data ;
34
+ }
35
+ }
29
36
//Get reflection class of FacadeHelper
30
37
$ trimedData = FacadeHelper::trimSpacesForArrayKeys ($ data );
31
38
//Any key in the Ignored List will not be processed
@@ -66,22 +73,37 @@ public static function reflectArrayToObject($classNameOrKeyName, $data, $throwEx
66
73
// }as $val
67
74
$ obj = FacadeHelper::reflectArrayToObject ($ key , $ val , $ throwException );
68
75
FacadeHelper::assignValue ($ currentObj , $ key , $ obj );
69
- }
70
- else if (FacadeHelper::isRecurrsiveArray ($ val )){
76
+ } else if (FacadeHelper::isArrayOfObj ($ val )){
77
+ // Array of LineItem object, for example
78
+ foreach ($ val as $ valobj ){
79
+ if (!Facadehelper::checkIfTheObjectIsAnInstanceOfTheClass ($ key ,$ valobj )){
80
+ throw new \Exception ("The assigned object is not an instance of required object:{ " . $ key . "}. " );
81
+ }
82
+ }
83
+ FacadeHelper::assignValue ($ currentObj , $ key , $ val );
84
+ } else {
71
85
//The array is a recursive array. It can be an Line or LinkedTxn
72
86
//Example:
73
87
// Line": [{ ....}, {...}]
74
88
//For each element in the array, it is a line
89
+ //or It can be an mix of lines and Objects
90
+ //Line": [{ ....}, $obj1, {...}]
75
91
$ list = array ();
76
92
foreach ($ val as $ index => $ element ) {
77
- $ obj = FacadeHelper::reflectArrayToObject ($ key , $ element , $ throwException );
78
- array_push ($ list , $ obj );
79
- }
93
+ if (is_object ($ element )){
94
+ if (!Facadehelper::checkIfTheObjectIsAnInstanceOfTheClass ($ key ,$ element )){
95
+ throw new \Exception ("The assigned object is not an instance of required object:{ " . $ key . "}. " );
96
+ }
97
+ array_push ($ list , $ element );
98
+ }else if (is_array ($ element )){
99
+ $ obj = FacadeHelper::reflectArrayToObject ($ key , $ element , $ throwException );
100
+ array_push ($ list , $ obj );
101
+ }else {
102
+ throw new \Exception ("Format Error. Expect an element of an array or an object. " );
103
+ }
104
+ }
80
105
FacadeHelper::assignValue ($ currentObj , $ key , $ list );
81
- }else {
82
- throw new \Exception ("Internal Error. The Passed Array is neither associated array or recursive array. " );
83
- }
84
-
106
+ }
85
107
}else {
86
108
//Even the value is a key, the key can be an Enum type or a wrapper
87
109
if (FacadeHelper::isKeyInComplexList ($ key )){
@@ -95,9 +117,14 @@ public static function reflectArrayToObject($classNameOrKeyName, $data, $throwEx
95
117
$ createdObj = FacadeHelper::getEnumType ($ enumTypeClassName , $ val );
96
118
FacadeHelper::assignValue ($ currentObj , $ key , $ createdObj );
97
119
}
98
- //It is a simple type
99
120
else
100
121
{
122
+ //If it is an object
123
+ if (is_object ($ val )){
124
+ if (!Facadehelper::checkIfTheObjectIsAnInstanceOfTheClass ($ key ,$ val )){
125
+ throw new \Exception ("The assigned object is not an instance of required object:{ " . $ key . "}. " );
126
+ }
127
+ }
101
128
FacadeHelper::assignValue ($ currentObj , $ key , $ val );
102
129
}
103
130
}
@@ -176,6 +203,13 @@ public static function isKeyEnumType($key){
176
203
public static function getEnumType ($ clazz , $ val ){
177
204
if (!isset ($ val )) throw new \Exception ("Passed param for Enum can't be null. " );
178
205
if (class_exists ($ clazz )){
206
+ if (is_object ($ val )){
207
+ if ($ val instanceof $ clazz ){
208
+ return $ val ;
209
+ }else {
210
+ throw new \Exception ("The assigned obj to the enum class Type:{ " . $ clazz . "} is not matched. " );
211
+ }
212
+ }
179
213
$ enumObj = new $ clazz ();
180
214
//If $val is string
181
215
if (is_array ($ val ) && !empty ($ val )){
@@ -212,12 +246,20 @@ public static function getClassMethod($className, $methodName){
212
246
/**
213
247
* Construct an IPPReferenceType based on passed Array or String.
214
248
* If it is passed as an array, handle it.
215
- * If it is passed an a String. Construct an array and put the String on the value
249
+ * If it is passed as a String. Construct an array and put the String on the value
250
+ * If it is passed as an obj, compare it with IPPReferenceType and return the object
216
251
* @param $data
217
- * It can either be an array or a String
252
+ * It can either be an array or a String, or obj
218
253
*/
219
254
public static function getIPPReferenceTypeBasedOnArray ($ data ){
220
255
$ trimedDataArray = FacadeHelper::trimSpacesForArrayKeys ($ data );
256
+ if (is_object ($ trimedDataArray )){
257
+ if ($ trimedDataArray instanceof IPPReferenceType){
258
+ return $ trimedDataArray ;
259
+ }else {
260
+ throw new \Exception ("The assigned obj to IPPReferenceType is not matched with IPPReferenceType. " );
261
+ }
262
+ }
221
263
//THe ReferenceDataType should only contain at most Two elements
222
264
if (is_array ($ trimedDataArray )){
223
265
if (sizeof ($ trimedDataArray ) >= 3 ){
@@ -249,12 +291,20 @@ public static function getIPPReferenceTypeBasedOnArray($data){
249
291
/**
250
292
* If passed params is array, the first element of Array is used in IPPid.
251
293
* If passed params is not an array, the the value is used for Ippid.
294
+ * If passed params is an obj, the the value is simply returned.
252
295
* @param $data
253
296
* It can either be an array or a numeric representation
254
297
*/
255
298
public static function getIPPId ($ data ){
256
299
//Convert an IPPId based on the Data
257
300
if (!isset ($ data )) throw new \Exception ("Passed param for IPPid can't be null " );
301
+ if (is_object ($ data )){
302
+ if ($ data instanceof IPPid){
303
+ return $ data ;
304
+ }else {
305
+ throw new \Exception ("The assigned obj to IPPid is not matched with IPPid. " );
306
+ }
307
+ }
258
308
if (is_array ($ data )){
259
309
$ firstElementValue = reset ($ data );
260
310
}else if (is_numeric ($ data )){
@@ -305,11 +355,19 @@ public static function trimSpacesForArrayKeys($data){
305
355
if (!isset ($ data ) || empty ($ data )) return $ data ;
306
356
if (is_array ($ data ))
307
357
{
308
- $ trimedKeys = array_map ('trim ' , array_keys ($ data ));
309
- $ trimedResult = array_combine ($ trimedKeys , $ data );
310
- return $ trimedResult ;
358
+ if (FacadeHelper::isArrayOfObj ($ data )){
359
+ return $ data ;
360
+ }else {
361
+ $ trimedKeys = array_map ('trim ' , array_keys ($ data ));
362
+ $ trimedResult = array_combine ($ trimedKeys , $ data );
363
+ return $ trimedResult ;
364
+ }
311
365
}else {
312
- return trim ($ data );
366
+ if (is_object ($ data )){
367
+ return $ data ;
368
+ }else {
369
+ return trim ($ data );
370
+ }
313
371
}
314
372
}
315
373
@@ -379,4 +437,29 @@ private static function assignValue($targetObject, $key, $value){
379
437
}
380
438
}
381
439
440
+ private static function checkIfTheObjectIsAnInstanceOfTheClass ($ className , $ object ){
441
+ if ($ object instanceof $ className ){
442
+ return true ;
443
+ }else {
444
+ $ className = FacadeHelper::decorateKeyWithNameSpaceAndPrefix ($ className );
445
+ if ($ object instanceof $ className ){
446
+ return true ;
447
+ }
448
+ }
449
+ return false ;
450
+ }
451
+
452
+ private static function isArrayOfObj ($ data ){
453
+ if (is_array ($ data )){
454
+ foreach ($ data as $ dataMemeber ){
455
+ if (!is_object ($ dataMemeber )){
456
+ return false ;
457
+ }
458
+ }
459
+ return true ;
460
+ }
461
+
462
+ return false ;
463
+ }
464
+
382
465
}
0 commit comments