Struct Types
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
This commit is contained in:
parent
053e19a457
commit
e4fe2ecde3
64 changed files with 911 additions and 406 deletions
|
@ -1,8 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Cviebrock\EloquentSluggable\Sluggable;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
@ -31,7 +34,7 @@ class Article extends Model
|
|||
*
|
||||
* @return array
|
||||
*/
|
||||
public function sluggable()
|
||||
public function sluggable(): array
|
||||
{
|
||||
return [
|
||||
'titleurl' => [
|
||||
|
@ -52,7 +55,7 @@ class Article extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHtmlAttribute()
|
||||
public function getHtmlAttribute(): string
|
||||
{
|
||||
$markdown = new CommonMarkConverter();
|
||||
$html = $markdown->convertToHtml($this->main);
|
||||
|
@ -70,7 +73,7 @@ class Article extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getW3cTimeAttribute()
|
||||
public function getW3cTimeAttribute(): string
|
||||
{
|
||||
return $this->updated_at->toW3CString();
|
||||
}
|
||||
|
@ -80,7 +83,7 @@ class Article extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTooltipTimeAttribute()
|
||||
public function getTooltipTimeAttribute(): string
|
||||
{
|
||||
return $this->updated_at->toRFC850String();
|
||||
}
|
||||
|
@ -90,7 +93,7 @@ class Article extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHumanTimeAttribute()
|
||||
public function getHumanTimeAttribute(): string
|
||||
{
|
||||
return $this->updated_at->diffForHumans();
|
||||
}
|
||||
|
@ -100,7 +103,7 @@ class Article extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPubdateAttribute()
|
||||
public function getPubdateAttribute(): string
|
||||
{
|
||||
return $this->updated_at->toRSSString();
|
||||
}
|
||||
|
@ -110,7 +113,7 @@ class Article extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLinkAttribute()
|
||||
public function getLinkAttribute(): string
|
||||
{
|
||||
return '/blog/' . $this->updated_at->year . '/' . $this->updated_at->format('m') . '/' . $this->titleurl;
|
||||
}
|
||||
|
@ -120,7 +123,7 @@ class Article extends Model
|
|||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeDate($query, int $year = null, int $month = null)
|
||||
public function scopeDate($query, int $year = null, int $month = null): Builder
|
||||
{
|
||||
if ($year == null) {
|
||||
return $query;
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
@ -24,6 +26,8 @@ class Bookmark extends Model
|
|||
|
||||
/**
|
||||
* The tags that belong to the bookmark.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function tags()
|
||||
{
|
||||
|
@ -32,8 +36,10 @@ class Bookmark extends Model
|
|||
|
||||
/**
|
||||
* The full url of a bookmark.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLongurlAttribute()
|
||||
public function getLongurlAttribute(): string
|
||||
{
|
||||
return config('app.url') . '/bookmarks/' . $this->id;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Mf2;
|
||||
|
@ -11,17 +13,33 @@ class Like extends Model
|
|||
{
|
||||
protected $fillable = ['url'];
|
||||
|
||||
public function setUrlAttribute($value)
|
||||
/**
|
||||
* Normalize the URL of a Like.
|
||||
*
|
||||
* @param string $value The provided URL
|
||||
*/
|
||||
public function setUrlAttribute(string $value)
|
||||
{
|
||||
$this->attributes['url'] = normalize_url($value);
|
||||
}
|
||||
|
||||
public function setAuthorUrlAttribute($value)
|
||||
/**
|
||||
* Normalize the URL of the author of the like.
|
||||
*
|
||||
* @param string $value The author’s url
|
||||
*/
|
||||
public function setAuthorUrlAttribute(?string $value)
|
||||
{
|
||||
$this->attributes['author_url'] = normalize_url($value);
|
||||
}
|
||||
|
||||
public function getContentAttribute($value)
|
||||
/**
|
||||
* If the content contains HTML, filter it.
|
||||
*
|
||||
* @param string $value The content of the like
|
||||
* @return string|null
|
||||
*/
|
||||
public function getContentAttribute(?string $value): ?string
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
|
@ -38,7 +56,13 @@ class Like extends Model
|
|||
return $value;
|
||||
}
|
||||
|
||||
public function filterHTML($html)
|
||||
/**
|
||||
* Filter some HTML with HTMLPurifier.
|
||||
*
|
||||
* @param string $html
|
||||
* @return string
|
||||
*/
|
||||
private function filterHTML(string $html): string
|
||||
{
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$config->set('Cache.SerializerPath', storage_path() . '/HTMLPurifier');
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Media extends Model
|
||||
{
|
||||
|
@ -22,8 +25,10 @@ class Media extends Model
|
|||
|
||||
/**
|
||||
* Get the note that owns this media.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function note()
|
||||
public function note(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo('App\Models\Note');
|
||||
}
|
||||
|
@ -33,7 +38,7 @@ class Media extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUrlAttribute()
|
||||
public function getUrlAttribute(): string
|
||||
{
|
||||
if (starts_with($this->path, 'https://')) {
|
||||
return $this->path;
|
||||
|
@ -47,7 +52,7 @@ class Media extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMediumurlAttribute()
|
||||
public function getMediumurlAttribute(): string
|
||||
{
|
||||
$basename = $this->getBasename($this->path);
|
||||
$extension = $this->getExtension($this->path);
|
||||
|
@ -60,7 +65,7 @@ class Media extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSmallurlAttribute()
|
||||
public function getSmallurlAttribute(): string
|
||||
{
|
||||
$basename = $this->getBasename($this->path);
|
||||
$extension = $this->getExtension($this->path);
|
||||
|
@ -68,7 +73,13 @@ class Media extends Model
|
|||
return config('filesystems.disks.s3.url') . '/' . $basename . '-small.' . $extension;
|
||||
}
|
||||
|
||||
public function getBasename($path)
|
||||
/**
|
||||
* Give the real part of a filename, i.e. strip the file extension.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public function getBasename(string $path): string
|
||||
{
|
||||
// the following achieves this data flow
|
||||
// foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
|
||||
|
@ -81,7 +92,13 @@ class Media extends Model
|
|||
return $basename;
|
||||
}
|
||||
|
||||
public function getExtension($path)
|
||||
/**
|
||||
* Get the extension from a given filename.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public function getExtension(string $path): string
|
||||
{
|
||||
$parts = explode('.', $path);
|
||||
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class MicropubClient extends Model
|
||||
{
|
||||
|
@ -23,9 +26,9 @@ class MicropubClient extends Model
|
|||
/**
|
||||
* Define the relationship with notes.
|
||||
*
|
||||
* @return void
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function notes()
|
||||
public function notes(): HasMany
|
||||
{
|
||||
return $this->hasMany('App\Models\Note', 'client_id', 'client_url');
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Cache;
|
||||
|
@ -14,6 +16,7 @@ use League\CommonMark\Environment;
|
|||
use League\CommonMark\HtmlRenderer;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Jonnybarnes\EmojiA11y\EmojiModifier;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Jonnybarnes\CommonmarkLinkify\LinkifyExtension;
|
||||
|
||||
|
@ -29,8 +32,16 @@ class Note extends Model
|
|||
*/
|
||||
private const USERNAMES_REGEX = '/\[.*?\](*SKIP)(*F)|@(\w+)/';
|
||||
|
||||
/**
|
||||
* This variable is used to keep track of contacts in a note.
|
||||
*/
|
||||
protected $contacts;
|
||||
|
||||
/**
|
||||
* Set our contacts variable to null.
|
||||
*
|
||||
* @param array $attributes
|
||||
*/
|
||||
public function __construct(array $attributes = [])
|
||||
{
|
||||
parent::__construct($attributes);
|
||||
|
@ -65,7 +76,7 @@ class Note extends Model
|
|||
/**
|
||||
* Define the relationship with tags.
|
||||
*
|
||||
* @var array
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function tags()
|
||||
{
|
||||
|
@ -75,7 +86,7 @@ class Note extends Model
|
|||
/**
|
||||
* Define the relationship with clients.
|
||||
*
|
||||
* @var array?
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function client()
|
||||
{
|
||||
|
@ -85,7 +96,7 @@ class Note extends Model
|
|||
/**
|
||||
* Define the relationship with webmentions.
|
||||
*
|
||||
* @var array
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
||||
*/
|
||||
public function webmentions()
|
||||
{
|
||||
|
@ -93,9 +104,9 @@ class Note extends Model
|
|||
}
|
||||
|
||||
/**
|
||||
* Definte the relationship with places.
|
||||
* Define the relationship with places.
|
||||
*
|
||||
* @var array
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function place()
|
||||
{
|
||||
|
@ -105,7 +116,7 @@ class Note extends Model
|
|||
/**
|
||||
* Define the relationship with media.
|
||||
*
|
||||
* @return void
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function media()
|
||||
{
|
||||
|
@ -117,7 +128,7 @@ class Note extends Model
|
|||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toSearchableArray()
|
||||
public function toSearchableArray(): array
|
||||
{
|
||||
return [
|
||||
'note' => $this->note,
|
||||
|
@ -127,30 +138,36 @@ class Note extends Model
|
|||
/**
|
||||
* Normalize the note to Unicode FORM C.
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
* @param string|null $value
|
||||
*/
|
||||
public function setNoteAttribute($value)
|
||||
public function setNoteAttribute(?string $value)
|
||||
{
|
||||
$normalized = normalizer_normalize($value, Normalizer::FORM_C);
|
||||
if ($normalized === '') { //we don’t want to save empty strings to the db
|
||||
$normalized = null;
|
||||
if ($value !== null) {
|
||||
$normalized = normalizer_normalize($value, Normalizer::FORM_C);
|
||||
if ($normalized === '') { //we don’t want to save empty strings to the db
|
||||
$normalized = null;
|
||||
}
|
||||
$this->attributes['note'] = $normalized;
|
||||
}
|
||||
$this->attributes['note'] = $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-process notes for web-view.
|
||||
*
|
||||
* @param string
|
||||
* @return string
|
||||
* @param string|null $value
|
||||
* @return string|null
|
||||
*/
|
||||
public function getNoteAttribute($value)
|
||||
public function getNoteAttribute(?string $value): ?string
|
||||
{
|
||||
if ($value === null && $this->place !== null) {
|
||||
$value = '📍: <a href="' . $this->place->longurl . '">' . $this->place->name . '</a>';
|
||||
}
|
||||
|
||||
// if $value is still null, just return null
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hcards = $this->makeHCards($value);
|
||||
$hashtags = $this->autoLinkHashtag($hcards);
|
||||
$html = $this->convertMarkdown($hashtags);
|
||||
|
@ -164,11 +181,10 @@ class Note extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNb60idAttribute()
|
||||
public function getNb60idAttribute(): string
|
||||
{
|
||||
$numbers = new Numbers();
|
||||
|
||||
return $numbers->numto60($this->id);
|
||||
// we cast to string because sometimes the nb60id is an “int”
|
||||
return (string) resolve(Numbers::class)->numto60($this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -176,7 +192,7 @@ class Note extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLongurlAttribute()
|
||||
public function getLongurlAttribute(): string
|
||||
{
|
||||
return config('app.url') . '/notes/' . $this->nb60id;
|
||||
}
|
||||
|
@ -186,7 +202,7 @@ class Note extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getShorturlAttribute()
|
||||
public function getShorturlAttribute(): string
|
||||
{
|
||||
return config('app.shorturl') . '/notes/' . $this->nb60id;
|
||||
}
|
||||
|
@ -196,7 +212,7 @@ class Note extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getIso8601Attribute()
|
||||
public function getIso8601Attribute(): string
|
||||
{
|
||||
return $this->updated_at->toISO8601String();
|
||||
}
|
||||
|
@ -206,7 +222,7 @@ class Note extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHumandiffAttribute()
|
||||
public function getHumandiffAttribute(): string
|
||||
{
|
||||
return $this->updated_at->diffForHumans();
|
||||
}
|
||||
|
@ -216,7 +232,7 @@ class Note extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPubdateAttribute()
|
||||
public function getPubdateAttribute(): string
|
||||
{
|
||||
return $this->updated_at->toRSSString();
|
||||
}
|
||||
|
@ -224,9 +240,9 @@ class Note extends Model
|
|||
/**
|
||||
* Get the latitude value.
|
||||
*
|
||||
* @return string|null
|
||||
* @return float|null
|
||||
*/
|
||||
public function getLatitudeAttribute()
|
||||
public function getLatitudeAttribute(): ?float
|
||||
{
|
||||
if ($this->place !== null) {
|
||||
return $this->place->location->getLat();
|
||||
|
@ -235,16 +251,18 @@ class Note extends Model
|
|||
$pieces = explode(':', $this->location);
|
||||
$latlng = explode(',', $pieces[0]);
|
||||
|
||||
return trim($latlng[0]);
|
||||
return (float) trim($latlng[0]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the longitude value.
|
||||
*
|
||||
* @return string|null
|
||||
* @return float|null
|
||||
*/
|
||||
public function getLongitudeAttribute()
|
||||
public function getLongitudeAttribute(): ?float
|
||||
{
|
||||
if ($this->place !== null) {
|
||||
return $this->place->location->getLng();
|
||||
|
@ -253,8 +271,10 @@ class Note extends Model
|
|||
$pieces = explode(':', $this->location);
|
||||
$latlng = explode(',', $pieces[0]);
|
||||
|
||||
return trim($latlng[1]);
|
||||
return (float) trim($latlng[1]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -263,7 +283,7 @@ class Note extends Model
|
|||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getAddressAttribute()
|
||||
public function getAddressAttribute(): ?string
|
||||
{
|
||||
if ($this->place !== null) {
|
||||
return $this->place->name;
|
||||
|
@ -271,12 +291,19 @@ class Note extends Model
|
|||
if ($this->location !== null) {
|
||||
return $this->reverseGeoCode((float) $this->latitude, (float) $this->longitude);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getTwitterAttribute()
|
||||
/**
|
||||
* Get the OEmbed html for a tweet the note is a reply to.
|
||||
*
|
||||
* @return object|null
|
||||
*/
|
||||
public function getTwitterAttribute(): ?object
|
||||
{
|
||||
if ($this->in_reply_to == null || mb_substr($this->in_reply_to, 0, 20, 'UTF-8') !== 'https://twitter.com/') {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$tweetId = basename($this->in_reply_to);
|
||||
|
@ -292,7 +319,7 @@ class Note extends Model
|
|||
'maxwidth' => 512,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
Cache::put($tweetId, $oEmbed, ($oEmbed->cache_age / 60));
|
||||
|
||||
|
@ -301,20 +328,24 @@ class Note extends Model
|
|||
|
||||
/**
|
||||
* Show a specific form of the note for twitter.
|
||||
*
|
||||
* That is we swap the contacts names for their known Twitter handles.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getTwitterContentAttribute()
|
||||
public function getTwitterContentAttribute(): ?string
|
||||
{
|
||||
if ($this->contacts === null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (count($this->contacts) === 0) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (count(array_unique(array_values($this->contacts))) === 1
|
||||
&& array_unique(array_values($this->contacts))[0] === null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
// swap in twitter usernames
|
||||
|
@ -338,15 +369,22 @@ class Note extends Model
|
|||
return $this->convertMarkdown($swapped);
|
||||
}
|
||||
|
||||
public function getFacebookContentAttribute()
|
||||
/**
|
||||
* Show a specific form of the note for facebook.
|
||||
*
|
||||
* That is we swap the contacts names for their known Facebook usernames.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getFacebookContentAttribute(): ?string
|
||||
{
|
||||
if (count($this->contacts) === 0) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (count(array_unique(array_values($this->contacts))) === 1
|
||||
&& array_unique(array_values($this->contacts))[0] === null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
// swap in facebook usernames
|
||||
|
@ -374,27 +412,27 @@ class Note extends Model
|
|||
/**
|
||||
* Scope a query to select a note via a NewBase60 id.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $nb60id
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $nb60id
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeNb60($query, $nb60id)
|
||||
public function scopeNb60(Builder $query, string $nb60id): Builder
|
||||
{
|
||||
$numbers = new Numbers();
|
||||
|
||||
return $query->where('id', $numbers->b60tonum($nb60id));
|
||||
return $query->where('id', resolve(Numbers::class)->b60tonum($nb60id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Swap contact’s nicks for a full mf2 h-card.
|
||||
*
|
||||
* Take note that this method does two things, given @username (NOT [@username](URL)!)
|
||||
* we try to create a fancy hcard from our contact info. If this is not possible
|
||||
* due to lack of contact info, we assume @username is a twitter handle and link it
|
||||
* as such.
|
||||
*
|
||||
* @param string The note’s text
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
private function makeHCards($text)
|
||||
private function makeHCards(string $text): string
|
||||
{
|
||||
$this->getContacts();
|
||||
|
||||
|
@ -424,6 +462,9 @@ class Note extends Model
|
|||
return $hcards;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the `contacts` property.
|
||||
*/
|
||||
public function getContacts()
|
||||
{
|
||||
if ($this->contacts === null) {
|
||||
|
@ -431,6 +472,9 @@ class Note extends Model
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the note and save the contacts to the `contacts` property.
|
||||
*/
|
||||
public function setContacts()
|
||||
{
|
||||
$contacts = [];
|
||||
|
@ -446,14 +490,16 @@ class Note extends Model
|
|||
}
|
||||
|
||||
/**
|
||||
* Turn text hashtags to full HTML links.
|
||||
*
|
||||
* Given a string and section, finds all hashtags matching
|
||||
* `#[\-_a-zA-Z0-9]+` and wraps them in an `a` element with
|
||||
* `rel=tag` set and a `href` of 'section/tagged/' + tagname without the #.
|
||||
*
|
||||
* @param string The note
|
||||
* @param string $note
|
||||
* @return string
|
||||
*/
|
||||
public function autoLinkHashtag($text)
|
||||
public function autoLinkHashtag(string $note): string
|
||||
{
|
||||
return preg_replace_callback(
|
||||
'/#([^\s]*)\b/',
|
||||
|
@ -462,25 +508,31 @@ class Note extends Model
|
|||
. Tag::normalize($matches[1]) . '">#'
|
||||
. $matches[1] . '</a>';
|
||||
},
|
||||
$text
|
||||
$note
|
||||
);
|
||||
}
|
||||
|
||||
private function convertMarkdown($text)
|
||||
/**
|
||||
* Pass a note through the commonmark library.
|
||||
*
|
||||
* @param string $note
|
||||
* @return string
|
||||
*/
|
||||
private function convertMarkdown(string $note): string
|
||||
{
|
||||
$environment = Environment::createCommonMarkEnvironment();
|
||||
$environment->addExtension(new LinkifyExtension());
|
||||
$converter = new Converter(new DocParser($environment), new HtmlRenderer($environment));
|
||||
|
||||
return $converter->convertToHtml($text);
|
||||
return $converter->convertToHtml($note);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do a reverse geocode lookup of a `lat,lng` value.
|
||||
*
|
||||
* @param float The latitude
|
||||
* @param float The longitude
|
||||
* @return string The location HTML
|
||||
* @param float $latitude
|
||||
* @param float $longitude
|
||||
* @return string
|
||||
*/
|
||||
public function reverseGeoCode(float $latitude, float $longitude): string
|
||||
{
|
||||
|
@ -498,7 +550,7 @@ class Note extends Model
|
|||
],
|
||||
'headers' => ['User-Agent' => 'jonnybarnes.uk via Guzzle, email jonny@jonnybarnes.uk'],
|
||||
]);
|
||||
$json = json_decode($response->getBody());
|
||||
$json = json_decode((string) $response->getBody());
|
||||
if (isset($json->address->town)) {
|
||||
$address = '<span class="p-locality">'
|
||||
. $json->address->town
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
@ -36,7 +38,7 @@ class Place extends Model
|
|||
*
|
||||
* @return array
|
||||
*/
|
||||
public function sluggable()
|
||||
public function sluggable(): array
|
||||
{
|
||||
return [
|
||||
'slug' => [
|
||||
|
@ -49,7 +51,7 @@ class Place extends Model
|
|||
/**
|
||||
* Define the relationship with Notes.
|
||||
*
|
||||
* @var array
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function notes()
|
||||
{
|
||||
|
@ -59,12 +61,12 @@ class Place extends Model
|
|||
/**
|
||||
* Select places near a given location.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param Point $point
|
||||
* @param int Distance
|
||||
* @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)
|
||||
public function scopeNear(Builder $query, Point $point, $distance = 1000): Builder
|
||||
{
|
||||
$field = DB::raw(
|
||||
sprintf(
|
||||
|
@ -77,7 +79,14 @@ class Place extends Model
|
|||
return $query->where($field, '<=', $distance)->orderBy($field);
|
||||
}
|
||||
|
||||
public function scopeWhereExternalURL(Builder $query, string $url)
|
||||
/**
|
||||
* 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,
|
||||
|
@ -87,21 +96,21 @@ class Place extends Model
|
|||
/**
|
||||
* Get the latitude from the `location` property.
|
||||
*
|
||||
* @return string latitude
|
||||
* @return float
|
||||
*/
|
||||
public function getLatitudeAttribute()
|
||||
public function getLatitudeAttribute(): float
|
||||
{
|
||||
return explode(' ', $this->location)[1];
|
||||
return $this->location->getLat();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the longitude from the `location` property.
|
||||
*
|
||||
* @return string longitude
|
||||
* @return float
|
||||
*/
|
||||
public function getLongitudeAttribute()
|
||||
public function getLongitudeAttribute(): float
|
||||
{
|
||||
return explode(' ', $this->location)[0];
|
||||
return $this->location->getLng();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,7 +118,7 @@ class Place extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLongurlAttribute()
|
||||
public function getLongurlAttribute(): string
|
||||
{
|
||||
return config('app.url') . '/places/' . $this->slug;
|
||||
}
|
||||
|
@ -119,7 +128,7 @@ class Place extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getShorturlAttribute()
|
||||
public function getShorturlAttribute(): string
|
||||
{
|
||||
return config('app.shorturl') . '/places/' . $this->slug;
|
||||
}
|
||||
|
@ -129,12 +138,17 @@ class Place extends Model
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUriAttribute()
|
||||
public function getUriAttribute(): string
|
||||
{
|
||||
return $this->longurl;
|
||||
}
|
||||
|
||||
public function setExternalUrlsAttribute($url)
|
||||
/**
|
||||
* Dealing with a jsonb column, so we check input first.
|
||||
*
|
||||
* @param string|null $url
|
||||
*/
|
||||
public function setExternalUrlsAttribute(?string $url)
|
||||
{
|
||||
if ($url === null) {
|
||||
return;
|
||||
|
@ -148,7 +162,13 @@ class Place extends Model
|
|||
$this->attributes['external_urls'] = json_encode($already);
|
||||
}
|
||||
|
||||
private function getType(string $url): ?string
|
||||
/**
|
||||
* 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) {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
@ -14,9 +16,9 @@ class Tag extends Model
|
|||
protected $guarded = ['id'];
|
||||
|
||||
/**
|
||||
* Define the relationship with tags.
|
||||
* Define the relationship with notes.
|
||||
*
|
||||
* @var array
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function notes()
|
||||
{
|
||||
|
@ -25,6 +27,8 @@ class Tag extends Model
|
|||
|
||||
/**
|
||||
* The bookmarks that belong to the tag.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function bookmarks()
|
||||
{
|
||||
|
@ -32,11 +36,11 @@ class Tag extends Model
|
|||
}
|
||||
|
||||
/**
|
||||
* Normalize tags so they’re lowercase and fancy diatrics are removed.
|
||||
* When creating a Tag model instance, invoke the nomralize method on the tag.
|
||||
*
|
||||
* @param string
|
||||
* @param string $value
|
||||
*/
|
||||
public function setTagAttribute($value)
|
||||
public function setTagAttribute(string $value)
|
||||
{
|
||||
$this->attributes['tag'] = $this->normalize($value);
|
||||
}
|
||||
|
@ -45,9 +49,10 @@ class Tag extends Model
|
|||
* This method actually normalizes a tag. That means lowercase-ing and
|
||||
* removing fancy diatric characters.
|
||||
*
|
||||
* @param string
|
||||
* @param string $tag
|
||||
* @return string
|
||||
*/
|
||||
public static function normalize($tag)
|
||||
public static function normalize(string $tag): string
|
||||
{
|
||||
return mb_strtolower(
|
||||
preg_replace(
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Cache;
|
||||
|
@ -29,7 +31,7 @@ class WebMention extends Model
|
|||
/**
|
||||
* Define the relationship.
|
||||
*
|
||||
* @var array
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
||||
*/
|
||||
public function commentable()
|
||||
{
|
||||
|
@ -41,7 +43,7 @@ class WebMention extends Model
|
|||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAuthorAttribute()
|
||||
public function getAuthorAttribute(): array
|
||||
{
|
||||
$authorship = new Authorship();
|
||||
$hCard = $authorship->findAuthor(json_decode($this->mf2, true));
|
||||
|
@ -57,11 +59,12 @@ class WebMention extends Model
|
|||
/**
|
||||
* Get the published value for the webmention.
|
||||
*
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPublishedAttribute()
|
||||
public function getPublishedAttribute(): ?string
|
||||
{
|
||||
$microformats = json_decode($this->mf2, true);
|
||||
$mf2 = $this->mf2 ?? '';
|
||||
$microformats = json_decode($mf2, true);
|
||||
if (isset($microformats['items'][0]['properties']['published'][0])) {
|
||||
try {
|
||||
$published = carbon()->parse(
|
||||
|
@ -82,12 +85,17 @@ class WebMention extends Model
|
|||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getReplyAttribute()
|
||||
public function getReplyAttribute(): ?string
|
||||
{
|
||||
if ($this->mf2 === null) {
|
||||
return null;
|
||||
}
|
||||
$microformats = json_decode($this->mf2, true);
|
||||
if (isset($microformats['items'][0]['properties']['content'][0]['html'])) {
|
||||
return $this->filterHTML($microformats['items'][0]['properties']['content'][0]['html']);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -126,10 +134,10 @@ class WebMention extends Model
|
|||
/**
|
||||
* Filter the HTML in a reply webmention.
|
||||
*
|
||||
* @param string The reply HTML
|
||||
* @return string The filtered HTML
|
||||
* @param string $html
|
||||
* @return string
|
||||
*/
|
||||
private function filterHTML($html)
|
||||
private function filterHTML(string $html): string
|
||||
{
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$config->set('Cache.SerializerPath', storage_path() . '/HTMLPurifier');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue