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;
|
|
|
|
|
|
|
|
use App\Place;
|
|
|
|
use Phaza\LaravelPostgis\Geometries\Point;
|
|
|
|
|
|
|
|
class PlaceService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a place.
|
|
|
|
*
|
2017-03-01 20:15:35 +00:00
|
|
|
* @param array $data
|
2016-05-19 15:01:28 +01:00
|
|
|
* @return \App\Place
|
|
|
|
*/
|
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
|
|
|
|
if (array_key_exists('geo', $data)) {
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a place from a h-card checkin, for exameple from OwnYourSwarm.
|
|
|
|
*
|
|
|
|
* @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
|
2017-06-30 13:21:40 +01:00
|
|
|
if (array_key_exists('url', $checkin['properties'])) {
|
|
|
|
$place = Place::whereExternalURL($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
|
|
|
}
|
|
|
|
}
|
|
|
|
if (array_key_exists('name', $checkin['properties']) === false) {
|
|
|
|
throw new \InvalidArgumentException('Missing required name');
|
|
|
|
}
|
|
|
|
if (array_key_exists('latitude', $checkin['properties']) === false) {
|
|
|
|
throw new \InvalidArgumentException('Missing required longitude/latitude');
|
|
|
|
}
|
|
|
|
$place = new Place();
|
|
|
|
$place->name = $checkin['properties']['name'][0];
|
2017-06-30 13:21:40 +01:00
|
|
|
$place->external_urls = $checkin['properties']['url'][0];
|
2017-05-18 15:15:53 +01:00
|
|
|
$place->location = new Point(
|
|
|
|
(float) $checkin['properties']['latitude'][0],
|
|
|
|
(float) $checkin['properties']['longitude'][0]
|
|
|
|
);
|
|
|
|
$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
|
|
|
}
|