Add BaseModel with stricter attribute existence checking

This commit is contained in:
2018-07-29 16:37:11 +02:00
parent 72ddd75023
commit f1529b3b8e
10 changed files with 53 additions and 23 deletions
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
public function getAttribute($key)
{
if (! $key) {
throw new \LogicException("No attribute ".var_export($key, true));
}
return parent::getAttribute($key);
}
/**
* Get a relationship.
*
* @param string $key
* @return mixed
*/
public function getRelationValue($key)
{
if (!method_exists($this, $key)) {
throw new \LogicException("No attribute or relation ".var_export($key, true));
}
return parent::getRelationValue($key);
}
}
+1 -3
View File
@@ -2,8 +2,6 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Report (something objectionable spotted by a user)
*
@@ -17,7 +15,7 @@ use Illuminate\Database\Eloquent\Model;
* @property mixed $object - morph
* @property-read User $author
*/
class ContentReport extends Model
class ContentReport extends BaseModel
{
protected $guarded = [];
+1 -3
View File
@@ -2,8 +2,6 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* E-mail confirmation
*
@@ -14,7 +12,7 @@ use Illuminate\Database\Eloquent\Model;
* @property string $email
* @property-read User $user
*/
class EmailConfirmation extends Model
class EmailConfirmation extends BaseModel
{
protected $guarded = [];
const UPDATED_AT = null; // disable update timestamp
+1 -2
View File
@@ -3,7 +3,6 @@
namespace App\Models;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Model;
/**
* Change proposal
@@ -20,7 +19,7 @@ use Illuminate\Database\Eloquent\Model;
* @property-read Table $table
* @property-read Revision $revision
*/
class Proposal extends Model
class Proposal extends BaseModel
{
use Reportable;
protected $guarded = [];
+1 -2
View File
@@ -2,7 +2,6 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Riesjart\Relaquent\Model\Concerns\HasRelaquentRelationships;
@@ -21,7 +20,7 @@ use Riesjart\Relaquent\Model\Concerns\HasRelaquentRelationships;
* @property-read Proposal|null $sourceProposal - proposal that was used to create this revision
* @property-read Proposal[]|Collection $dependentProposals
*/
class Revision extends Model
class Revision extends BaseModel
{
use HasRelaquentRelationships;
protected $guarded = [];
+1 -3
View File
@@ -2,15 +2,13 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Row in a data table
*
* @property int $id
* @property string $data - JSONB
*/
class Row extends Model
class Row extends BaseModel
{
protected $guarded = [];
public $timestamps = false;
+1 -2
View File
@@ -5,7 +5,6 @@ namespace App\Models;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* A data table object (referencing revisions)
@@ -33,7 +32,7 @@ use Illuminate\Database\Eloquent\Model;
* @property-read User[]|Collection $favouritingUsers
* @property-read User[]|Collection $discussionFollowers
*/
class Table extends Model
class Table extends BaseModel
{
use Reportable;
protected $guarded = [];
+1 -2
View File
@@ -4,7 +4,6 @@ namespace App\Models;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* Comment on a data table
@@ -21,7 +20,7 @@ use Illuminate\Database\Eloquent\Model;
* @property-read TableComment|null $ancestor
* @property-read TableComment[]|Collection $replies
*/
class TableComment extends Model
class TableComment extends BaseModel
{
use Reportable;
protected $guarded = [];
+1 -4
View File
@@ -3,13 +3,10 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Model representing the pivot table, used for more efficient favourite counting
*/
class TableFavouritePivot extends Model
class TableFavouritePivot extends BaseModel
{
protected $table = 'table_favourites';
public $timestamps = [];
+11 -2
View File
@@ -7,10 +7,15 @@ use App\Models\Concerns\InstanceCache;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notification;
use MightyPork\Exceptions\ArgumentException;
use MightyPork\Exceptions\NotExistException;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
/**
* A user in the application
@@ -37,8 +42,12 @@ use MightyPork\Exceptions\NotExistException;
* @property-read Notification[]|Collection $unreadNotifications
* @property-read string $handle - user handle, including @
*/
class User extends Authenticatable
class User extends BaseModel implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
use Reportable;
use Notifiable;
use InstanceCache;