2016-05-19 15:01:28 +01:00
|
|
|
<?php
|
|
|
|
|
2017-03-01 20:15:35 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
namespace App\Services;
|
|
|
|
|
2017-12-19 16:00:42 +00:00
|
|
|
use App\Models\Place;
|
2019-02-01 18:49:35 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2020-06-13 17:26:59 +01:00
|
|
|
use MStaack\LaravelPostgis\Geometries\Point;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
|
|
|
class PlaceService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a place.
|
|
|
|
*
|
2020-02-21 22:46:48 +00:00
|
|
|
* @param array $data
|
|
|
|
* @return Place
|
2016-05-19 15:01:28 +01:00
|
|
|
*/
|
2017-03-01 20:15:35 +00:00
|
|
|
public function createPlace(array $data): Place
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
2017-03-01 20:15:35 +00:00
|
|
|
//obviously a place needs a lat/lng, but this could be sent in a geo-url
|
|
|
|
//if no geo array key, we assume the array already has lat/lng values
|
2017-12-18 15:51:02 +00:00
|
|
|
if (array_key_exists('geo', $data) && $data['geo'] !== null) {
|
2016-10-01 14:26:07 +01:00
|
|
|
preg_match_all(
|
|
|
|
'/([0-9\.\-]+)/',
|
2017-03-01 20:15:35 +00:00
|
|
|
$data['geo'],
|
2016-10-01 14:26:07 +01:00
|
|
|
$matches
|
|
|
|
);
|
2017-03-01 20:15:35 +00:00
|
|
|
$data['latitude'] = $matches[0][0];
|
|
|
|
$data['longitude'] = $matches[0][1];
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
$place = new Place();
|
2017-03-01 20:15:35 +00:00
|
|
|
$place->name = $data['name'];
|
|
|
|
$place->description = $data['description'];
|
|
|
|
$place->location = new Point((float) $data['latitude'], (float) $data['longitude']);
|
2016-05-19 15:01:28 +01:00
|
|
|
$place->save();
|
|
|
|
|
|
|
|
return $place;
|
|
|
|
}
|
2017-05-18 15:15:53 +01:00
|
|
|
|
|
|
|
/**
|
2020-02-21 22:46:48 +00:00
|
|
|
* Create a place from a h-card checkin, for example from OwnYourSwarm.
|
2017-05-18 15:15:53 +01:00
|
|
|
*
|
2020-02-21 22:46:48 +00:00
|
|
|
* @param array
|
2017-06-13 18:10:51 +01:00
|
|
|
* @return Place
|
2017-05-18 15:15:53 +01:00
|
|
|
*/
|
2017-06-13 18:10:51 +01:00
|
|
|
public function createPlaceFromCheckin(array $checkin): Place
|
2017-05-18 15:15:53 +01:00
|
|
|
{
|
|
|
|
//check if the place exists if from swarm
|
2019-02-01 18:49:35 +00:00
|
|
|
if (Arr::has($checkin, 'properties.url')) {
|
|
|
|
$place = Place::whereExternalURL(Arr::get($checkin, 'properties.url.0'))->get();
|
2017-06-13 18:10:51 +01:00
|
|
|
if (count($place) === 1) {
|
2017-06-27 17:40:37 +01:00
|
|
|
return $place->first();
|
2017-05-18 15:15:53 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-01 18:49:35 +00:00
|
|
|
if (Arr::has($checkin, 'properties.name') === false) {
|
2017-05-18 15:15:53 +01:00
|
|
|
throw new \InvalidArgumentException('Missing required name');
|
|
|
|
}
|
2019-02-01 18:49:35 +00:00
|
|
|
if (Arr::has($checkin, 'properties.latitude') === false) {
|
2017-05-18 15:15:53 +01:00
|
|
|
throw new \InvalidArgumentException('Missing required longitude/latitude');
|
|
|
|
}
|
|
|
|
$place = new Place();
|
2019-02-01 18:49:35 +00:00
|
|
|
$place->name = Arr::get($checkin, 'properties.name.0');
|
|
|
|
$place->external_urls = Arr::get($checkin, 'properties.url.0');
|
2017-05-18 15:15:53 +01:00
|
|
|
$place->location = new Point(
|
2019-02-01 18:49:35 +00:00
|
|
|
(float) Arr::get($checkin, 'properties.latitude.0'),
|
|
|
|
(float) Arr::get($checkin, 'properties.longitude.0')
|
2017-05-18 15:15:53 +01:00
|
|
|
);
|
|
|
|
$place->save();
|
|
|
|
|
2017-06-13 18:10:51 +01:00
|
|
|
return $place;
|
2017-05-18 15:15:53 +01:00
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|