ILIAS  release_7 Revision v7.30-3-g800a261c036
ilGeoLocationFileMockRepository.php
Go to the documentation of this file.
1<?php
2
4{
5
9 public function createGeoLocation(
10 string $a_title,
11 float $a_latitude,
12 float $a_longitude,
13 \DateTimeImmutable $a_expiration_timestamp
14 ) : ilGeoLocation {
15 // Generate a random object id. This should just work fine for development
16 $generated_id = rand(1, 100000);
17 $file = fopen('mocked_geolocation_data.txt', FILE_APPEND);
18
19 // Create a csv-string for object data for the file
20 $write_string = "$generated_id;$a_title;$a_latitude;$a_longitude;$a_expiration_timestamp\n";
21
22 // Write new object to file
23 fwrite($file, $write_string);
24 fclose($file);
25
26 return new ilGeoLocation(
27 $generated_id,
28 $a_title,
29 $a_latitude,
30 $a_longitude,
31 $a_expiration_timestamp
32 );
33 }
34
38 public function getGeoLocationById(int $a_id) : ilGeoLocation
39 {
40 $file = fopen('mocked_geolocation_data.txt', 'r');
41
42 // Go line by line through the file and return object if it was found
43 while ($row = fgetcsv($file)) {
44 if ($row[0] == $a_id) {
45 return new ilGeoLocation((int) $a_id, $row[1], (float) $row[2], (float) $row[3], new DateTimeImmutable($row[4]));
46 }
47 }
48
49 throw new \InvalidArgumentException("Unknown id for geolocation: $a_id");
50 }
51
55 public function getGeoLocationsByCoordinates(float $a_latitude, float $a_longitude) : array
56 {
57 $file = fopen('mocked_geolocation_data.txt', 'r');
58 $geo_locations = array();
59
60 // Go line by line through the file and add searched objects to list
61 while ($row = fgetcsv($file)) {
62 if ($row[2] == $a_latitude && $row[3] == $a_longitude) {
63 $geo_locations[] = new ilGeoLocation((int) $row[0], $row[1], (float) $row[2], (float) $row[3], new DateTimeImmutable($row[4]));
64 }
65 }
66
67 return $geo_locations;
68 }
69
73 public function ifGeoLocationExistsById(int $a_id) : bool
74 {
75 $file = fopen('mocked_geolocation_data.txt', 'r');
76
77 // Go line by line through the file and search for given id
78 while ($row = fgetcsv($file)) {
79 if ($row[0] == $a_id) {
80 return true;
81 }
82 }
83
84 return false;
85 }
86
87
91 public function ifAnyGeoLocationExistsByCoordinates(float $a_latitude, float $a_longitude) : bool
92 {
93 $file = fopen('mocked_geolocation_data.txt', 'r');
94
95 // Go line by line through the file and search for given attributes
96 while ($row = fgetcsv($file)) {
97 if ($row[2] == $a_latitude && $row[3] == $a_longitude) {
98 return true;
99 }
100 }
101
102 return false;
103 }
104
108 public function updateGeoLocation(ilGeoLocation $a_obj)
109 {
110 // Read entire file
111 $file = fopen('mocked_geolocation_data.txt', 'r');
112 $geo_locations = $this->readFileAndReturnAsList($file);
113 fclose($file);
114
115 // Update searched object in list
116 foreach ($geo_locations as $key => $row) {
117 if ($row[0] == $a_obj->getId()) {
118 $row[1] = $a_obj->getTitle();
119 $row[2] = $a_obj->getLatitude();
120 $row[3] = $a_obj->getLongitude();
121 $row[4] = $a_obj->getExpirationAsTimestamp();
122 }
123 }
124
125 // Write back all geo locations to file
126 $file = fopen('mocked_geolocation_data.txt', 'w');
127 $this->writeGeoLocationListToFile($file, $geo_locations);
128 fclose($file);
129 }
130
134 public function updateGeoLocationTimestampByCoordinates(float $a_searched_latitude, float $a_searched_longitude, \DateTimeImmutable $a_update_timestamp)
135 {
136 // Read entire file
137 $file = fopen('mocked_geolocation_data.txt', 'r');
138 $geo_locations = $this->readFileAndReturnAsList($file);
139 fclose($file);
140
141 // Update searched objects in list
142 foreach ($geo_locations as $key => $row) {
143 if ($row[2] == $a_searched_latitude && $row[3] == $a_searched_longitude) {
144 $row[4] = $a_update_timestamp->getTimestamp();
145 }
146 }
147
148 // Write back all geo locations to file
149 $file = fopen('mocked_geolocation_data.txt', 'w');
150 $this->writeGeoLocationListToFile($file, $geo_locations);
151 fclose($file);
152 }
153
157 public function deleteGeoLocation(int $a_id)
158 {
159 $file = fopen('mocked_geolocation_data.txt', 'r');
160 $geo_locations = $this->readFileAndReturnAsList($file);
161 fclose($file);
162
163 // Delete searched object from list
164 foreach ($geo_locations as $key => $row) {
165 // Check if current row has searched id
166 if ($row[0] == $a_id) {
167 unset($geo_locations[$key]);
168 }
169 }
170
171 // Write back all geo locations to file
172 $file = fopen('mocked_geolocation_data.txt', 'w');
173 $this->writeGeoLocationListToFile($file, $geo_locations);
174 fclose($file);
175 }
176
177 public function deleteGeoLocationsByCoordinates(float $a_latitude, float $a_longitude)
178 {
179 // Read all geo locations from file
180 $file = fopen('mocked_geolocation_data.txt', 'r');
181 $geo_locations = $this->readFileAndReturnAsList($file);
182 fclose($file);
183
184 // Filter out expired objects
185 $now = new DateTimeImmutable();
186 foreach ($geo_locations as $key => $row) {
187 // Check if current row has searched attributes
188 if ($row[2] == $a_latitude && $row[3] == $a_longitude) {
189 unset($geo_locations[$key]);
190 }
191 }
192
193 // Write objects back to file
194 $file = fopen('mocked_geolocation_data.txt', 'w');
195 $this->writeGeoLocationListToFile($file, $geo_locations);
196 fclose($file);
197 }
198
203 {
204 // Read all geo locations from file
205 $file = fopen('mocked_geolocation_data.txt', 'r');
206 $geo_locations = $this->readFileAndReturnAsList($file);
207 fclose($file);
208
209 // Filter out expired objects
210 $now = new DateTimeImmutable();
211 foreach ($geo_locations as $key => $row) {
212 // Check if current row contains an expired timestamp
213 // Note: This is an example but it needs to be tested if differences can be calculated like this
214 $dt = new DateTimeImmutable($row[4]);
215 if ($now->diff($dt)->s >= 0) {
216 unset($geo_locations[$key]);
217 }
218 }
219
220 // Write objects back to file
221 $file = fopen('mocked_geolocation_data.txt', 'w');
222 $this->writeGeoLocationListToFile($file, $geo_locations);
223 fclose($file);
224 }
225
229 protected function readFileAndReturnAsList($file)
230 {
231 $geo_locations = array();
232 while ($row = fgetcsv($file)) {
233 $geo_locations[] = $row;
234 }
235 return $geo_locations;
236 }
237
241 protected function writeGeoLocationListToFile($file, $list)
242 {
243 foreach ($list as $obj_data) {
244 // implode(';', $list) . "\n"; // <- this way might also work
245 $write_string = $obj_data[0] . ';' . $obj_data[1] . ';' . $obj_data[2] . ';' . $obj_data[3] . ';' . $obj_data[4] . "\n";
246 fwrite($file, $write_string . "\n");
247 }
248 }
249}
An exception for terminatinating execution or to throw for unit testing.
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...