| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace garethp\ews\API; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Type converter for handling SOAP stdClass objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Automatically converts stdClass objects to proper EWS type objects | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | class TypeConverter | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |      * Convert stdClass objects to proper EWS types using reflection | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      * @param mixed $value The value to convert | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * @param string $targetType The expected EWS type class name | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * @return mixed Converted value or original if no conversion needed | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 2 |  |     public static function convertToType($value, string $targetType) | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 2 |  |         if (is_array($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 2 |  |             return array_map(function ($item) use ($targetType) { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 2 |  |                 return self::convertSingleObject($item, $targetType); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 2 |  |             }, $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         return self::convertSingleObject($value, $targetType); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * Convert a single stdClass object to target type | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @param mixed $value The value to convert | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param string $targetType The target EWS type class name | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @return mixed Converted object or original value | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 2 |  |     private static function convertSingleObject($value, string $targetType) | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 2 |  |         if (!($value instanceof \stdClass)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             return $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 2 |  |         if (!class_exists($targetType)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             return $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 2 |  |         $instance = new $targetType(); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         // Map stdClass properties to typed object | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 2 |  |         foreach (get_object_vars($value) as $property => $propertyValue) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 2 |  |             self::setProperty($instance, $property, $propertyValue); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 2 |  |         return $instance; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * Set property on target object using setter method or direct assignment | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @param object $instance Target object | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @param string $property Property name | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @param mixed $value Property value | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 2 |  |     private static function setProperty(object $instance, string $property, $value): void | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         // Try setter method first | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 2 |  |         $setterMethod = 'set' . ucfirst($property); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 2 |  |         if (method_exists($instance, $setterMethod)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 2 |  |             $instance->$setterMethod($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 2 |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         // Try direct property assignment | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 2 |  |         if (property_exists($instance, $property)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 2 |  |             $instance->$property = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * Check if value contains any stdClass objects | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param mixed $value Value to check | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * @return bool True if contains stdClass objects | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 37 |  |     public static function containsStdClass($value): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 37 |  |         if ($value instanceof \stdClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 37 |  |         if (is_array($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 25 |  |             foreach ($value as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 25 |  |                 if ($item instanceof \stdClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |                     return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |          | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 | 37 |  |         return false; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 101 |  |  |  |