|
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
|
|
|
$changesList = array_merge($changesListUpdateOrDelete, $changesListInsert); |
|
131
|
|
|
return $changesList; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* |
|
136
|
|
|
* @param array $currentAppointments |
|
137
|
|
|
* @param array $changingsDetected |
|
138
|
|
|
* @return array ['deleted'=>[],'updated'=>[]] |
|
139
|
|
|
*/ |
|
140
|
|
|
public function detectDeleteOrUpdatedOrInserted(array $currentAppointments, array $changingsDetected){ |
|
141
|
|
|
$delete = []; |
|
142
|
|
|
$update = []; |
|
143
|
|
|
$insert = []; |
|
144
|
|
|
//@Todo: Detect Fields has changed |
|
145
|
|
|
$appointmentsNew = self::recursiveArrayObjectToFullArray($currentAppointments); |
|
146
|
|
|
$changings = self::recursiveArrayObjectToFullArray($changingsDetected); |
|
147
|
|
|
foreach ($changings as $changing){ |
|
148
|
|
|
$registration = $changing['registration']['regCode']; |
|
149
|
|
|
$registrationTarget = $changing['targetRegistration']['regCode']; |
|
150
|
|
|
foreach ($appointmentsNew as $currentAppointment){ |
|
151
|
|
|
$registrationCurrent = $currentAppointment['registration']['regCode']; |
|
152
|
|
|
$registrationTargetCurrent = $currentAppointment['targetRegistration']['regCode']; |
|
153
|
|
|
if(in_array($registration,[$registrationCurrent,$registrationTargetCurrent]) |
|
154
|
|
|
&& in_array($registrationTarget,[$registrationCurrent,$registrationTargetCurrent]) |
|
155
|
|
|
&& !in_array($changing,$update) && !in_array($changing,$delete)) { |
|
156
|
|
|
$update[] = $changing; |
|
157
|
|
|
break; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
if(!in_array($changing,$update) && !in_array($changing,$delete)){ |
|
161
|
|
|
$delete[] = $changing; |
|
162
|
|
|
} |
|
163
|
|
|
$insert[] = $changing; |
|
164
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
return [ |
|
167
|
|
|
'deleted' => $delete, |
|
168
|
|
|
'updated' => $update, |
|
169
|
|
|
'$insert' => $insert |
|
170
|
|
|
]; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param array $appointments |
|
175
|
|
|
* @param $timestamp |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
|
|
public static function insertDateTimeChanges(array $appointments,$timestamp){ |
|
179
|
|
|
foreach ($appointments as $key => $appointment){ |
|
180
|
|
|
$appointments[$key]['dateDetectChanges'] = $timestamp; |
|
181
|
|
|
} |
|
182
|
|
|
return $appointments; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param array $appointmentsOld |
|
187
|
|
|
* @param array $appointmentsNew |
|
188
|
|
|
* @param string $timestamp |
|
189
|
|
|
* @return array ['deleted'=>[],'updated'=>[]] |
|
190
|
|
|
*/ |
|
191
|
|
|
public function detectAppointmentsChangings(array $appointmentsOld,array $appointmentsNew,$timestamp){ |
|
192
|
|
|
$changings = $this->getListChangings($appointmentsOld,$appointmentsNew); |
|
193
|
|
|
$changesList = $this->detectDeleteOrUpdatedOrInserted($appointmentsNew,$changings); |
|
194
|
|
|
$changesList['inserted'] = self::insertDateTimeChanges($changesList['inserted'],$timestamp); |
|
195
|
|
|
$changesList['updated'] = self::insertDateTimeChanges($changesList['updated'],$timestamp); |
|
196
|
|
|
$changesList['deleted'] = self::insertDateTimeChanges($changesList['deleted'],$timestamp); |
|
197
|
|
|
return $changesList; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
} |
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: