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
|
|
|
if($changing == $currentAppointment){ |
152
|
|
|
$insert[] = $changing; |
153
|
|
|
break; |
154
|
|
|
} |
155
|
|
|
$registrationCurrent = $currentAppointment['registration']['regCode']; |
156
|
|
|
$registrationTargetCurrent = $currentAppointment['targetRegistration']['regCode']; |
157
|
|
|
if(in_array($registration,[$registrationCurrent,$registrationTargetCurrent]) |
158
|
|
|
&& in_array($registrationTarget,[$registrationCurrent,$registrationTargetCurrent]) |
159
|
|
|
&& !in_array($changing,$insert) && !in_array($changing,$update) && !in_array($changing,$delete)) { |
160
|
|
|
$update[] = $changing; |
161
|
|
|
break; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
if(!in_array($changing,$update) && !in_array($changing,$delete)){ |
165
|
|
|
$delete[] = $changing; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
return [ |
170
|
|
|
'deleted' => $delete, |
171
|
|
|
'updated' => $update, |
172
|
|
|
'inserted' => $insert |
173
|
|
|
]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param array $appointments |
178
|
|
|
* @param $timestamp |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
public static function insertDateTimeChanges(array $appointments,$timestamp){ |
182
|
|
|
foreach ($appointments as $key => $appointment){ |
183
|
|
|
$appointments[$key]['dateDetectChanges'] = $timestamp; |
184
|
|
|
} |
185
|
|
|
return $appointments; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param array $appointmentsOld |
190
|
|
|
* @param array $appointmentsNew |
191
|
|
|
* @param string $timestamp |
192
|
|
|
* @return array ['deleted'=>[],'updated'=>[]] |
193
|
|
|
*/ |
194
|
|
|
public function detectAppointmentsChangings(array $appointmentsOld,array $appointmentsNew,$timestamp){ |
195
|
|
|
$changings = $this->getListChangings($appointmentsOld,$appointmentsNew); |
196
|
|
|
$changesList = $this->detectDeleteOrUpdatedOrInserted($appointmentsNew,$changings); |
197
|
|
|
$changesList['inserted'] = self::insertDateTimeChanges($changesList['inserted'],$timestamp); |
198
|
|
|
$changesList['updated'] = self::insertDateTimeChanges($changesList['updated'],$timestamp); |
199
|
|
|
$changesList['deleted'] = self::insertDateTimeChanges($changesList['deleted'],$timestamp); |
200
|
|
|
return $changesList; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: