| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Wabel\CertainAPI\Services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Wabel\CertainAPI\Ressources\AppointmentsCertain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class DetectAppointmentsChangingsService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @var AppointmentsCertain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private $appointmentsCertain; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     public function __construct(AppointmentsCertain $appointmentsCertain) | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         $this->appointmentsCertain = $appointmentsCertain; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param $eventCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param null|int $start | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param null|int $maxResult | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function getCurrentAppoiments($eventCode,$start=null,$maxResult=null){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if(!$start){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $start = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         if(!$maxResult){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $maxResult = 999999; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return $this->certainAppointmentsList = $this->appointmentsCertain->get($eventCode,['start_index'=>$start,'max_results'=>$maxResult])->getResults()->appointments; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param array $appointmentsOld | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param array $appointmentsNew | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function hasChanged(array $appointmentsOld,array $appointmentsNew){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $hasChanged = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $appointmentsOld = self::recursiveArrayObjectToFullArray($appointmentsOld); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $appointmentsNew = self::recursiveArrayObjectToFullArray($appointmentsNew); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         //Has change by update or delete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         foreach ($appointmentsOld as $appointmentOld){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             if(!in_array($appointmentOld,$appointmentsNew)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $hasChanged = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         //Has changes by insertion or update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if(!$hasChanged){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             foreach ($appointmentsNew as $appointmentNew){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 if(!in_array($appointmentNew,$appointmentsOld)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     $hasChanged = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $hasChanged; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public static  function objectToArray($object) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if(is_object($object)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             return (array) $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param $appointments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public static function recursiveArrayObjectToFullArray($appointments){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return json_decode(json_encode($appointments), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param array $arrayOlds | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param array $arrayNews | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     private function arrayRecursiveDiff(array $arrayOlds, array $arrayNews) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $difference = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         foreach($arrayOlds as $key => $arrayOld){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             if(!in_array($arrayOld,$arrayNews)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $difference[$key] = $arrayOld; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $difference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param array $arrayOlds | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param array $arrayNews | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param array $existedUpdateOrDelete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     private function arrayRecursiveDiffNew(array $arrayOlds, array $arrayNews, array $existedUpdateOrDelete) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $difference = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         foreach($arrayNews as $key => $arrayNew){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             if(!in_array($arrayNew,$arrayOlds) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 && !in_array(self::recursiveArrayObjectToFullArray($arrayNew), $existedUpdateOrDelete)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $difference[$key] = $arrayNew; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return $difference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param array $appointmentsOld | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param array $appointmentsNew | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function getListChangings(array $appointmentsOld,array $appointmentsNew){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $appointmentsOld = self::recursiveArrayObjectToFullArray($appointmentsOld); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $appointmentsNew = self::recursiveArrayObjectToFullArray($appointmentsNew); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $changesListUpdateOrDelete = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $changesListInsert = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if($this->hasChanged($appointmentsOld,$appointmentsNew)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $changesListUpdateOrDelete = self::recursiveArrayObjectToFullArray($this->arrayRecursiveDiff($appointmentsOld,$appointmentsNew)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $changesListInsert = self::recursiveArrayObjectToFullArray($this->arrayRecursiveDiffNew($appointmentsOld, $appointmentsNew, $changesListUpdateOrDelete)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             'inserted' => $changesListInsert, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             'updated_deleted' => $changesListUpdateOrDelete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param array $currentAppointments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @param array $changingsDetected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @return array ['deleted'=>[],'updated'=>[]] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     public function detectDeleteOrUpdated(array $currentAppointments, array $changingsDetected){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $delete = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $update = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         //@Todo: Detect Fields has changed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $appointmentsNew = self::recursiveArrayObjectToFullArray($currentAppointments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $changings = self::recursiveArrayObjectToFullArray($changingsDetected); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         foreach ($changings as $changing){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             $registration = $changing['registration']['regCode']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             $registrationTarget = $changing['targetRegistration']['regCode']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             foreach ($appointmentsNew as $currentAppointment){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 $registrationCurrent = $currentAppointment['registration']['regCode']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 $registrationTargetCurrent = $currentAppointment['targetRegistration']['regCode']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 if(in_array($registration,[$registrationCurrent,$registrationTargetCurrent]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                     && in_array($registrationTarget,[$registrationCurrent,$registrationTargetCurrent]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                      && !in_array($changing,$update) && !in_array($changing,$delete)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                     $update[] = $changing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             if(!in_array($changing, $update) && !in_array($changing,$delete)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                 $delete[] = $changing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             'deleted' => $delete, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             'updated' => $update, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @param array $appointments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * @param $timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     public static function insertDateTimeChanges(array $appointments,$timestamp){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         foreach ($appointments as $key => $appointment){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             $appointments[$key]['dateDetectChanges'] = $timestamp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         return $appointments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      * @param array $appointmentsOld | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      * @param array $appointmentsNew | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * @param string $timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * @return array ['deleted'=>[],'updated'=>[]] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     public function detectAppointmentsChangings(array $appointmentsOld,array $appointmentsNew,$timestamp){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         $changings = $this->getListChangings($appointmentsOld,$appointmentsNew); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         $changesList = $this->detectDeleteOrUpdated($appointmentsNew,$changings['updated_deleted']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         $changesList['inserted'] = self::insertDateTimeChanges($changings['inserted'],$timestamp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         $changesList['updated'] = self::insertDateTimeChanges($changesList['updated'],$timestamp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         $changesList['deleted'] = self::insertDateTimeChanges($changesList['deleted'],$timestamp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         return $changesList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 199 |  |  | } | 
            
                        
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: