ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ilGeoLocationFileMockRepository.php
Go to the documentation of this file.
1<?
2
4{
5
9 public function createGeoLocation(
10 string $a_title,
11 float $a_latitude,
12 float $a_longitude,
13 \DateTimeImmutable $a_expiration_timestamp
15 {
16 // Generate a random object id. This should just work fine for development
17 $generated_id = rand(1, 100000);
18 $file = fopen('mocked_geolocation_data.txt', FILE_APPEND);
19
20 // Create a csv-string for object data for the file
21 $write_string = "$generated_id;$a_title;$a_latitude;$a_longitude;$a_expiration_timestamp\n";
22
23 // Write new object to file
24 fwrite($file, $write_string);
25 fclose($file);
26
27 return new ilGeoLocation(
28 $generated_id,
29 $a_title,
30 $a_latitude,
31 $a_longitude,
32 $a_expiration_timestamp
33 );
34 }
35
39 public function getGeoLocationById(int $a_id) : ilGeoLocation
40 {
41 $file = fopen('mocked_geolocation_data.txt', 'r');
42
43 // Go line by line through the file and return object if it was found
44 while($row = fgetcsv($file))
45 {
46 if($row[0] == $a_id)
47 {
48 return new ilGeoLocation((int)$a_id, $row[1], (float)$row[2], (float)$row[3], new DateTimeImmutable($row[4]));
49 }
50 }
51
52 throw new \InvalidArgumentException("Unknown id for geolocation: $a_id");
53 }
54
58 public function getGeoLocationsByCoordinates(float $a_latitude, float $a_longitude) : array
59 {
60 $file = fopen('mocked_geolocation_data.txt', 'r');
61 $geo_locations = array();
62
63 // Go line by line through the file and add searched objects to list
64 while($row = fgetcsv($file))
65 {
66 if($row[2] == $a_latitude && $row[3] == $a_longitude)
67 {
68 $geo_locations[] = new ilGeoLocation((int)$row[0], $row[1], (float)$row[2], (float)$row[3], new DateTimeImmutable($row[4]));
69 }
70 }
71
72 return $geo_locations;
73 }
74
78 public function ifGeoLocationExistsById(int $a_id): bool
79 {
80 $file = fopen('mocked_geolocation_data.txt', 'r');
81
82 // Go line by line through the file and search for given id
83 while($row = fgetcsv($file))
84 {
85 if($row[0] == $a_id)
86 {
87 return true;
88 }
89 }
90
91 return false;
92 }
93
94
98 public function ifAnyGeoLocationExistsByCoordinates(float $a_latitude, float $a_longitude): bool
99 {
100 $file = fopen('mocked_geolocation_data.txt', 'r');
101
102 // Go line by line through the file and search for given attributes
103 while($row = fgetcsv($file))
104 {
105 if($row[2] == $a_latitude && $row[3] == $a_longitude)
106 {
107 return true;
108 }
109 }
110
111 return false;
112 }
113
117 public function updateGeoLocation(ilGeoLocation $a_obj)
118 {
119 // Read entire file
120 $file = fopen('mocked_geolocation_data.txt', 'r');
121 $geo_locations = $this->readFileAndReturnAsList($file);
122 fclose($file);
123
124 // Update searched object in list
125 foreach($geo_locations as $key => $row)
126 {
127 if($row[0] == $a_obj->getId())
128 {
129 $row[1] = $a_obj->getTitle();
130 $row[2] = $a_obj->getLatitude();
131 $row[3] = $a_obj->getLongitude();
132 $row[4] = $a_obj->getExpirationAsTimestamp();
133 }
134 }
135
136 // Write back all geo locations to file
137 $file = fopen('mocked_geolocation_data.txt', 'w');
138 $this->writeGeoLocationListToFile($file, $geo_locations);
139 fclose($file);
140 }
141
145 public function updateGeoLocationTimestampByCoordinates(float $a_searched_latitude, float $a_searched_longitude, \DateTimeImmutable $a_update_timestamp)
146 {
147 // Read entire file
148 $file = fopen('mocked_geolocation_data.txt', 'r');
149 $geo_locations = $this->readFileAndReturnAsList($file);
150 fclose($file);
151
152 // Update searched objects in list
153 foreach($geo_locations as $key => $row)
154 {
155 if($row[2] == $a_searched_latitude && $row[3] == $a_searched_longitude)
156 {
157 $row[4] = $a_update_timestamp->getTimestamp();
158 }
159 }
160
161 // Write back all geo locations to file
162 $file = fopen('mocked_geolocation_data.txt', 'w');
163 $this->writeGeoLocationListToFile($file, $geo_locations);
164 fclose($file);
165 }
166
170 public function deleteGeoLocation(int $a_id)
171 {
172 $file = fopen('mocked_geolocation_data.txt', 'r');
173 $geo_locations = $this->readFileAndReturnAsList($file);
174 fclose($file);
175
176 // Delete searched object from list
177 foreach($geo_locations as $key => $row)
178 {
179 // Check if current row has searched id
180 if($row[0] == $a_id)
181 {
182 unset($geo_locations[$key]);
183 }
184 }
185
186 // Write back all geo locations to file
187 $file = fopen('mocked_geolocation_data.txt', 'w');
188 $this->writeGeoLocationListToFile($file, $geo_locations);
189 fclose($file);
190 }
191
192 public function deleteGeoLocationsByCoordinates(float $a_latitude, float $a_longitude)
193 {
194 // Read all geo locations from file
195 $file = fopen('mocked_geolocation_data.txt', 'r');
196 $geo_locations = $this->readFileAndReturnAsList($file);
197 fclose($file);
198
199 // Filter out expired objects
200 $now = new DateTimeImmutable();
201 foreach($geo_locations as $key => $row)
202 {
203 // Check if current row has searched attributes
204 if($row[2] == $a_latitude && $row[3] == $a_longitude)
205 {
206 unset($geo_locations[$key]);
207 }
208 }
209
210 // Write objects back to file
211 $file = fopen('mocked_geolocation_data.txt', 'w');
212 $this->writeGeoLocationListToFile($file, $geo_locations);
213 fclose($file);
214 }
215
220 {
221 // Read all geo locations from file
222 $file = fopen('mocked_geolocation_data.txt', 'r');
223 $geo_locations = $this->readFileAndReturnAsList($file);
224 fclose($file);
225
226 // Filter out expired objects
227 $now = new DateTimeImmutable();
228 foreach($geo_locations as $key => $row)
229 {
230 // Check if current row contains an expired timestamp
231 // Note: This is an example but it needs to be tested if differences can be calculated like this
232 $dt = new DateTimeImmutable($row[4]);
233 if($now->diff($dt)->s >= 0)
234 {
235 unset($geo_locations[$key]);
236 }
237 }
238
239 // Write objects back to file
240 $file = fopen('mocked_geolocation_data.txt', 'w');
241 $this->writeGeoLocationListToFile($file, $geo_locations);
242 fclose($file);
243 }
244
248 protected function readFileAndReturnAsList($file)
249 {
250 $geo_locations = array();
251 while($row = fgetcsv($file)) $geo_locations[] = $row;
252 return $geo_locations;
253 }
254
258 protected function writeGeoLocationListToFile($file, $list)
259 {
260 foreach($list as $obj_data)
261 {
262 // implode(';', $list) . "\n"; // <- this way might also work
263 $write_string = $obj_data[0].';'.$obj_data[1].';'.$obj_data[2].';'.$obj_data[3].';'.$obj_data[4]."\n";
264 fwrite($file, $write_string . "\n");
265 }
266 }
267}
writeGeoLocationListToFile($file, $list)
Protected function.
ifAnyGeoLocationExistsByCoordinates(float $a_latitude, float $a_longitude)
Example for checking if a geo location (one or more) with a given attribute exists.
updateGeoLocationTimestampByCoordinates(float $a_searched_latitude, float $a_searched_longitude, \DateTimeImmutable $a_update_timestamp)
Example for updating multiple objects at once.
getGeoLocationById(int $a_id)
Get a single geo location, identified by its id.
getGeoLocationsByCoordinates(float $a_latitude, float $a_longitude)
Example for reading an array of geo locations which have a given attribute.
updateGeoLocation(ilGeoLocation $a_obj)
Update all attributes of a given geo location.
deleteExpiredGeoLocations()
Example for a condition based deletion of multiple geo locations.
ifGeoLocationExistsById(int $a_id)
Example for checking if a geo location (one or more) with a given attribute exists.
deleteGeoLocation(int $a_id)
Delete single geo location identified by its id.
deleteGeoLocationsByCoordinates(float $a_latitude, float $a_longitude)
Example for a condition based deletion of multiple geo locations.
createGeoLocation(string $a_title, float $a_latitude, float $a_longitude, \DateTimeImmutable $a_expiration_timestamp)
Create a new geo location entry.
This code is just an example for the Repository Pattern! It is a basic interface to the 'ilGeoLocatio...