ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
ilGeoLocationFileMockRepository Class Reference
+ Inheritance diagram for ilGeoLocationFileMockRepository:
+ Collaboration diagram for ilGeoLocationFileMockRepository:

Public Member Functions

 createGeoLocation (string $a_title, float $a_latitude, float $a_longitude, \DateTimeImmutable $a_expiration_timestamp)
 Create a new geo location entry. More...
 
 getGeoLocationById (int $a_id)
 Get a single geo location, identified by its id. More...
 
 getGeoLocationsByCoordinates (float $a_latitude, float $a_longitude)
 Example for reading an array of geo locations which have a given attribute. More...
 
 ifGeoLocationExistsById (int $a_id)
 Example for checking if a geo location (one or more) with a given attribute exists. More...
 
 ifAnyGeoLocationExistsByCoordinates (float $a_latitude, float $a_longitude)
 Example for checking if a geo location (one or more) with a given attribute exists. More...
 
 updateGeoLocation (ilGeoLocation $a_obj)
 Update all attributes of a given geo location. More...
 
 updateGeoLocationTimestampByCoordinates (float $a_searched_latitude, float $a_searched_longitude, \DateTimeImmutable $a_update_timestamp)
 Example for updating multiple objects at once. More...
 
 deleteGeoLocation (int $a_id)
 Delete single geo location identified by its id. More...
 
 deleteGeoLocationsByCoordinates (float $a_latitude, float $a_longitude)
 Example for a condition based deletion of multiple geo locations. More...
 
 deleteExpiredGeoLocations ()
 Example for a condition based deletion of multiple geo locations. More...
 

Protected Member Functions

 readFileAndReturnAsList ($file)
 Protected function. More...
 
 writeGeoLocationListToFile ($file, $list)
 Protected function. More...
 

Detailed Description

Definition at line 3 of file ilGeoLocationFileMockRepository.php.

Member Function Documentation

◆ createGeoLocation()

ilGeoLocationFileMockRepository::createGeoLocation ( string  $a_title,
float  $a_latitude,
float  $a_longitude,
\DateTimeImmutable  $a_expiration_timestamp 
)

Create a new geo location entry.

Implements ilGeoLocationRepository.

Definition at line 9 of file ilGeoLocationFileMockRepository.php.

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  }

◆ deleteExpiredGeoLocations()

ilGeoLocationFileMockRepository::deleteExpiredGeoLocations ( )

Example for a condition based deletion of multiple geo locations.

Implements ilGeoLocationRepository.

Definition at line 219 of file ilGeoLocationFileMockRepository.php.

References readFileAndReturnAsList(), and writeGeoLocationListToFile().

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  }
writeGeoLocationListToFile($file, $list)
Protected function.
+ Here is the call graph for this function:

◆ deleteGeoLocation()

ilGeoLocationFileMockRepository::deleteGeoLocation ( int  $a_id)

Delete single geo location identified by its id.

Implements ilGeoLocationRepository.

Definition at line 170 of file ilGeoLocationFileMockRepository.php.

References readFileAndReturnAsList(), and writeGeoLocationListToFile().

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  }
writeGeoLocationListToFile($file, $list)
Protected function.
+ Here is the call graph for this function:

◆ deleteGeoLocationsByCoordinates()

ilGeoLocationFileMockRepository::deleteGeoLocationsByCoordinates ( float  $a_latitude,
float  $a_longitude 
)

Example for a condition based deletion of multiple geo locations.

Implements ilGeoLocationRepository.

Definition at line 192 of file ilGeoLocationFileMockRepository.php.

References readFileAndReturnAsList(), and writeGeoLocationListToFile().

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  }
writeGeoLocationListToFile($file, $list)
Protected function.
+ Here is the call graph for this function:

◆ getGeoLocationById()

ilGeoLocationFileMockRepository::getGeoLocationById ( int  $a_id)

Get a single geo location, identified by its id.

Implements ilGeoLocationRepository.

Definition at line 39 of file ilGeoLocationFileMockRepository.php.

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  }

◆ getGeoLocationsByCoordinates()

ilGeoLocationFileMockRepository::getGeoLocationsByCoordinates ( float  $a_latitude,
float  $a_longitude 
)

Example for reading an array of geo locations which have a given attribute.

Implements ilGeoLocationRepository.

Definition at line 58 of file ilGeoLocationFileMockRepository.php.

58  : 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  }

◆ ifAnyGeoLocationExistsByCoordinates()

ilGeoLocationFileMockRepository::ifAnyGeoLocationExistsByCoordinates ( float  $a_latitude,
float  $a_longitude 
)

Example for checking if a geo location (one or more) with a given attribute exists.

Implements ilGeoLocationRepository.

Definition at line 98 of file ilGeoLocationFileMockRepository.php.

98  : 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  }

◆ ifGeoLocationExistsById()

ilGeoLocationFileMockRepository::ifGeoLocationExistsById ( int  $a_id)

Example for checking if a geo location (one or more) with a given attribute exists.

Implements ilGeoLocationRepository.

Definition at line 78 of file ilGeoLocationFileMockRepository.php.

78  : 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  }

◆ readFileAndReturnAsList()

ilGeoLocationFileMockRepository::readFileAndReturnAsList (   $file)
protected

Protected function.

Just for development purpose

Definition at line 248 of file ilGeoLocationFileMockRepository.php.

Referenced by deleteExpiredGeoLocations(), deleteGeoLocation(), deleteGeoLocationsByCoordinates(), updateGeoLocation(), and updateGeoLocationTimestampByCoordinates().

249  {
250  $geo_locations = array();
251  while($row = fgetcsv($file)) $geo_locations[] = $row;
252  return $geo_locations;
253  }
+ Here is the caller graph for this function:

◆ updateGeoLocation()

ilGeoLocationFileMockRepository::updateGeoLocation ( ilGeoLocation  $a_obj)

Update all attributes of a given geo location.

Implements ilGeoLocationRepository.

Definition at line 117 of file ilGeoLocationFileMockRepository.php.

References ilGeoLocation\getExpirationAsTimestamp(), ilGeoLocation\getId(), ilGeoLocation\getLatitude(), ilGeoLocation\getLongitude(), ilGeoLocation\getTitle(), readFileAndReturnAsList(), and writeGeoLocationListToFile().

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  }
writeGeoLocationListToFile($file, $list)
Protected function.
+ Here is the call graph for this function:

◆ updateGeoLocationTimestampByCoordinates()

ilGeoLocationFileMockRepository::updateGeoLocationTimestampByCoordinates ( float  $a_searched_latitude,
float  $a_searched_longitude,
\DateTimeImmutable  $a_update_timestamp 
)

Example for updating multiple objects at once.

Implements ilGeoLocationRepository.

Definition at line 145 of file ilGeoLocationFileMockRepository.php.

References readFileAndReturnAsList(), and writeGeoLocationListToFile().

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  }
writeGeoLocationListToFile($file, $list)
Protected function.
+ Here is the call graph for this function:

◆ writeGeoLocationListToFile()

ilGeoLocationFileMockRepository::writeGeoLocationListToFile (   $file,
  $list 
)
protected

Protected function.

Just for development purpose

Definition at line 258 of file ilGeoLocationFileMockRepository.php.

Referenced by deleteExpiredGeoLocations(), deleteGeoLocation(), deleteGeoLocationsByCoordinates(), updateGeoLocation(), and updateGeoLocationTimestampByCoordinates().

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  }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: