ILIAS  release_7 Revision v7.30-3-g800a261c036
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...
 
 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 geo location with a certain id 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)
 Example for updating 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)
 Exaple for deleting 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 // 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 }

◆ deleteExpiredGeoLocations()

ilGeoLocationFileMockRepository::deleteExpiredGeoLocations ( )

Example for a condition based deletion of multiple geo locations.

Implements ilGeoLocationRepository.

Definition at line 202 of file ilGeoLocationFileMockRepository.php.

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 }
writeGeoLocationListToFile($file, $list)
Protected function.

◆ deleteGeoLocation()

ilGeoLocationFileMockRepository::deleteGeoLocation ( int  $a_id)

Delete single geo location identified by its id.

Implements ilGeoLocationRepository.

Definition at line 157 of file ilGeoLocationFileMockRepository.php.

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 }

◆ deleteGeoLocationsByCoordinates()

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

Example for a condition based deletion of multiple geo locations.

Implements ilGeoLocationRepository.

Definition at line 177 of file ilGeoLocationFileMockRepository.php.

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 }

◆ getGeoLocationById()

ilGeoLocationFileMockRepository::getGeoLocationById ( int  $a_id)

Get a single geo location, identified by its id.

Implements ilGeoLocationRepository.

Definition at line 38 of file ilGeoLocationFileMockRepository.php.

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 }

◆ 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 55 of file ilGeoLocationFileMockRepository.php.

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

◆ 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 91 of file ilGeoLocationFileMockRepository.php.

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

◆ 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 73 of file ilGeoLocationFileMockRepository.php.

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

◆ readFileAndReturnAsList()

ilGeoLocationFileMockRepository::readFileAndReturnAsList (   $file)
protected

Protected function.

Just for development purpose

Definition at line 229 of file ilGeoLocationFileMockRepository.php.

230 {
231 $geo_locations = array();
232 while ($row = fgetcsv($file)) {
233 $geo_locations[] = $row;
234 }
235 return $geo_locations;
236 }

◆ updateGeoLocation()

ilGeoLocationFileMockRepository::updateGeoLocation ( ilGeoLocation  $a_obj)

Update all attributes of a given geo location.

Implements ilGeoLocationRepository.

Definition at line 108 of file ilGeoLocationFileMockRepository.php.

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 }

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

+ 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 134 of file ilGeoLocationFileMockRepository.php.

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 }

◆ writeGeoLocationListToFile()

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

Protected function.

Just for development purpose

Definition at line 241 of file ilGeoLocationFileMockRepository.php.

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 }

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