Squashed commit of the following: commit 74ed84617fcbecf661695763323e50d049a88db7 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:46:29 2018 +0000 Test passes so remove the dump statement commit a7d3323be02da64f76e8ec88713e3de84a13ded7 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:40:35 2018 +0000 Values with spaces need to be quoted commit 58a120bb238f14346793c388b948b7351d3b51fd Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:37:23 2018 +0000 We need a diplay name for the tests to work now we are using strict type checking commit b46f177053bd697db9a4835d073f2f37e088b26f Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:31:29 2018 +0000 Get travis to show more info about failing test commit 60323f3ce5a0561329a1721ee94821571cdcc86a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:23:27 2018 +0000 Remove un-used namnepsace imports commit 096d3505920bc94ff8677c77430eca0aae0be58a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:21:55 2018 +0000 we need php7.2 for object type-hint commit bb818bc19c73d02d510af9f002199f5718a54608 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon Jan 15 12:15:48 2018 +0000 Added lots of strict_types
183 lines
4.2 KiB
PHP
183 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Cviebrock\EloquentSluggable\Sluggable;
|
|
use Phaza\LaravelPostgis\Geometries\Point;
|
|
use Phaza\LaravelPostgis\Eloquent\PostgisTrait;
|
|
|
|
class Place extends Model
|
|
{
|
|
use Sluggable;
|
|
use PostgisTrait;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = ['name', 'slug'];
|
|
|
|
/**
|
|
* The attributes that are Postgis geometry objects.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $postgisFields = [
|
|
'location',
|
|
'polygon',
|
|
];
|
|
|
|
/**
|
|
* Return the sluggable configuration array for this model.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function sluggable(): array
|
|
{
|
|
return [
|
|
'slug' => [
|
|
'source' => 'name',
|
|
'onUpdate' => true,
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Define the relationship with Notes.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function notes()
|
|
{
|
|
return $this->hasMany('App\Models\Note');
|
|
}
|
|
|
|
/**
|
|
* Select places near a given location.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @param \Phaza\LaravelPostgis\Geometries\Point $point
|
|
* @param int $distance
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function scopeNear(Builder $query, Point $point, $distance = 1000): Builder
|
|
{
|
|
$field = DB::raw(
|
|
sprintf(
|
|
"ST_Distance(%s.location, ST_GeogFromText('%s'))",
|
|
$this->getTable(),
|
|
$point->toWKT()
|
|
)
|
|
);
|
|
|
|
return $query->where($field, '<=', $distance)->orderBy($field);
|
|
}
|
|
|
|
/**
|
|
* Select places based on a URL.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @param string $url
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function scopeWhereExternalURL(Builder $query, string $url): Builder
|
|
{
|
|
return $query->where('external_urls', '@>', json_encode([
|
|
$this->getType($url) => $url,
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Get the latitude from the `location` property.
|
|
*
|
|
* @return float
|
|
*/
|
|
public function getLatitudeAttribute(): float
|
|
{
|
|
return $this->location->getLat();
|
|
}
|
|
|
|
/**
|
|
* Get the longitude from the `location` property.
|
|
*
|
|
* @return float
|
|
*/
|
|
public function getLongitudeAttribute(): float
|
|
{
|
|
return $this->location->getLng();
|
|
}
|
|
|
|
/**
|
|
* The Long URL for a place.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getLongurlAttribute(): string
|
|
{
|
|
return config('app.url') . '/places/' . $this->slug;
|
|
}
|
|
|
|
/**
|
|
* The Short URL for a place.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getShorturlAttribute(): string
|
|
{
|
|
return config('app.shorturl') . '/places/' . $this->slug;
|
|
}
|
|
|
|
/**
|
|
* This method is an alternative for `longurl`.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getUriAttribute(): string
|
|
{
|
|
return $this->longurl;
|
|
}
|
|
|
|
/**
|
|
* Dealing with a jsonb column, so we check input first.
|
|
*
|
|
* @param string|null $url
|
|
*/
|
|
public function setExternalUrlsAttribute(?string $url)
|
|
{
|
|
if ($url === null) {
|
|
return;
|
|
}
|
|
$type = $this->getType($url);
|
|
$already = [];
|
|
if (array_key_exists('external_urls', $this->attributes)) {
|
|
$already = json_decode($this->attributes['external_urls'], true);
|
|
}
|
|
$already[$type] = $url;
|
|
$this->attributes['external_urls'] = json_encode($already);
|
|
}
|
|
|
|
/**
|
|
* Given a URL, see if it is one of our known types.
|
|
*
|
|
* @param string $url
|
|
* @return string
|
|
*/
|
|
private function getType(string $url): string
|
|
{
|
|
$host = parse_url($url, PHP_URL_HOST);
|
|
if (ends_with($host, 'foursquare.com') === true) {
|
|
return 'foursquare';
|
|
}
|
|
if (ends_with($host, 'openstreetmap.org') === true) {
|
|
return 'osm';
|
|
}
|
|
|
|
return 'default';
|
|
}
|
|
}
|