Skip to content

Commit

Permalink
Add location to geocoded URLs. Still need to add reverse geocoding an…
Browse files Browse the repository at this point in the history
…d location for non geocoding endpoints. #46
  • Loading branch information
meezaan committed Nov 7, 2024
1 parent 7cc87d0 commit 1a16a42
Show file tree
Hide file tree
Showing 5 changed files with 436 additions and 425 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM islamicnetwork/php:8.2-unit
FROM islamicnetwork/php:8.3-unit

# Copy files
COPY . /var/www/
Expand Down
11 changes: 5 additions & 6 deletions api/Controllers/v1/PrayerTimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class PrayerTimes extends Slim

public HijriCalendar $hc;


public function __construct(ContainerInterface $container)
{
parent::__construct($container);
Expand Down Expand Up @@ -49,7 +48,7 @@ public function timings(ServerRequestInterface $request, ResponseInterface $resp
$r = $ptm->respond($datestring, 'timings');

return Http\Response::json($response,
['timings' => $r[0], 'date' => $r[1], 'meta' => PrayerTimesHelper::getMetaArray($r[2])],
['timings' => $r[0], 'date' => $r[1], 'meta' => PrayerTimesHelper::getMetaArray($r[2], $r[4])],
200,
true,
3600,
Expand Down Expand Up @@ -86,7 +85,7 @@ public function timingsByAddress(ServerRequestInterface $request, ResponseInterf
$r = $ptm->respond($datestring, 'timingsByAddress');

return Http\Response::json($response,
['timings' => $r[0], 'date' => $r[1], 'meta' => PrayerTimesHelper::getMetaArray($r[2], $enableMasking)],
['timings' => $r[0], 'date' => $r[1], 'meta' => PrayerTimesHelper::getMetaArray($r[2], $r[4], $enableMasking)],
200,
true,
3600,
Expand Down Expand Up @@ -121,7 +120,7 @@ public function timingsByCity(ServerRequestInterface $request, ResponseInterface
$r = $ptm->respond($datestring, 'timingsByCity');

return Http\Response::json($response,
['timings' => $r[0], 'date' => $r[1], 'meta' => PrayerTimesHelper::getMetaArray($r[2], $enableMasking)],
['timings' => $r[0], 'date' => $r[1], 'meta' => PrayerTimesHelper::getMetaArray($r[2], $r[4], $enableMasking)],
200,
true,
3600,
Expand Down Expand Up @@ -156,7 +155,7 @@ public function nextPrayer(ServerRequestInterface $request, ResponseInterface $r
$nextPrayer = PrayerTimesHelper::nextPrayerTime($r[2], $r[3], $ptm->latitude, $ptm->longitude, $ptm->latitudeAdjustmentMethod, $ptm->iso8601, $ptm->timezone);

return Http\Response::json($response,
['timings' => $nextPrayer, 'date' => $r[1], 'meta' => PrayerTimesHelper::getMetaArray($r[2])],
['timings' => $nextPrayer, 'date' => $r[1], 'meta' => PrayerTimesHelper::getMetaArray($r[2], $r[4])],
200,
true,
300,
Expand Down Expand Up @@ -194,7 +193,7 @@ public function nextPrayerByAddress(ServerRequestInterface $request, ResponseInt
$nextPrayer = PrayerTimesHelper::nextPrayerTime($r[2], $r[3], $ptm->latitude, $ptm->longitude, $ptm->latitudeAdjustmentMethod, $ptm->iso8601, $ptm->timezone);

return Http\Response::json($response,
['timings' => $nextPrayer, 'date' => $r[1], 'meta' => PrayerTimesHelper::getMetaArray($r[2], $enableMasking)],
['timings' => $nextPrayer, 'date' => $r[1], 'meta' => PrayerTimesHelper::getMetaArray($r[2], $r[4], $enableMasking)],
200,
true,
300,
Expand Down
22 changes: 14 additions & 8 deletions api/Models/PrayerTimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Api\Utils\Timezone;
use SevenEx\DTO\Geocode\Location;
use SevenEx\SDK\Geocode;
use SevenEx\DTO\Geocode\Geocode as GeocodeDTO;
use Slim\Exception\HttpBadRequestException;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Contracts\Cache\ItemInterface;
Expand All @@ -30,6 +32,7 @@ class PrayerTimes
public string $latitudeAdjustmentMethod;
public ?float $latitude;
public ?float $longitude;
public ?Location $location;
public string $method;
public ?string $timezone;
public int $adjustment;
Expand Down Expand Up @@ -114,28 +117,31 @@ public function validateAddress(): void

if ($this->address !== null && ApiRequest::isValidAddress($this->address)) {
// /timingsByAddress call. Geocode.
$coordinates = $this->mc->get(md5('addr.' . strtolower($this->address) . $this->SevenExApiKey), function (ItemInterface $item) {
$geocode = $this->mc->get(md5('addr.' . strtolower($this->address) . $this->SevenExApiKey), function (ItemInterface $item) {
$item->expiresAfter(604800);
$gc = new Geocode($this->SevenExApiKey, $this->SevenExGeocodeBaseUrl);
$gcode = $gc->geocode($this->address);
if(!empty($gcode->objects)) {
return $gcode->objects[0]->coordinates;
return $gcode->objects[0];
}

return false;
});

if ($coordinates) {
$this->latitude = $coordinates->latitude;
$this->longitude = $coordinates->longitude;
/**
* @var GeocodeDTO $geocode
*/
if ($geocode) {
$this->latitude = $geocode->coordinates->latitude;
$this->longitude = $geocode->coordinates->longitude;
$this->location = $geocode->location;
} else {
throw new HttpBadRequestException($this->request,'Unable to geocode address.');
}

}
}

public function respond(string $datestring, string $endpoint, int $expires = 604800): array
public function respond(string $datestring, string $endpoint, int $expires = 1): array
{
$r = $this->mc->get(md5($endpoint . $datestring . json_encode(get_object_vars($this))),
function (ItemInterface $item) use ($datestring, $expires) {
Expand All @@ -154,7 +160,7 @@ function (ItemInterface $item) use ($datestring, $expires) {
'gregorian' => $hd['gregorian']
];

return [$timings, $date, $pt, $d];
return [$timings, $date, $pt, $d, $this->location];
});

return $r;
Expand Down
9 changes: 8 additions & 1 deletion api/Utils/PrayerTimesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
use Api\Models\HijriCalendar;
use IslamicNetwork\PrayerTimes\Method;
use IslamicNetwork\PrayerTimes\PrayerTimes;
use Api\Models\PrayerTimes as PrayerTimesModel;
use Api\Utils\Request as ApiRequest;
use IslamicNetwork\MoonSighting\Isha;
use Mamluk\Kipchak\Components\Http;
use DateTime;
use DateTimeZone;
use SevenEx\DTO\Geocode\Country;
use SevenEx\DTO\Geocode\Location;

/**
* Class PrayerTimesHelper
Expand Down Expand Up @@ -253,13 +256,17 @@ public static function isRamadan(\DateTime $date, $adjustment = 0): bool
* @param bool $enableMasking
* @return array
*/
public static function getMetaArray(PrayerTimes $prayerTimesModel, bool $enableMasking = false): array
public static function getMetaArray(PrayerTimes $prayerTimesModel, ?Location $location, bool $enableMasking = false): array
{
$meta = $prayerTimesModel->getMeta();

if ($enableMasking) {
$meta['latitude'] = (float) 1.234567;
$meta['longitude'] = (float) 2.34567;
$meta['location'] = new Location('****', '****', new Country('**', '*******'), '*******', '*******', '*******', '*******');
} else {
// Enhance meta with location information
$meta['location'] = $location;
}

return $meta;
Expand Down
Loading

0 comments on commit 1a16a42

Please sign in to comment.