Add BaseModel with stricter attribute existence checking

pull/26/head
Ondřej Hruška 6 years ago
parent 72ddd75023
commit f1529b3b8e
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 34
      app/Models/BaseModel.php
  2. 4
      app/Models/ContentReport.php
  3. 4
      app/Models/EmailConfirmation.php
  4. 3
      app/Models/Proposal.php
  5. 3
      app/Models/Revision.php
  6. 4
      app/Models/Row.php
  7. 3
      app/Models/Table.php
  8. 3
      app/Models/TableComment.php
  9. 5
      app/Models/TableFavouritePivot.php
  10. 13
      app/Models/User.php

@ -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);
}
}

@ -2,8 +2,6 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/** /**
* Report (something objectionable spotted by a user) * Report (something objectionable spotted by a user)
* *
@ -17,7 +15,7 @@ use Illuminate\Database\Eloquent\Model;
* @property mixed $object - morph * @property mixed $object - morph
* @property-read User $author * @property-read User $author
*/ */
class ContentReport extends Model class ContentReport extends BaseModel
{ {
protected $guarded = []; protected $guarded = [];

@ -2,8 +2,6 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/** /**
* E-mail confirmation * E-mail confirmation
* *
@ -14,7 +12,7 @@ use Illuminate\Database\Eloquent\Model;
* @property string $email * @property string $email
* @property-read User $user * @property-read User $user
*/ */
class EmailConfirmation extends Model class EmailConfirmation extends BaseModel
{ {
protected $guarded = []; protected $guarded = [];
const UPDATED_AT = null; // disable update timestamp const UPDATED_AT = null; // disable update timestamp

@ -3,7 +3,6 @@
namespace App\Models; namespace App\Models;
use App\Models\Concerns\Reportable; use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Model;
/** /**
* Change proposal * Change proposal
@ -20,7 +19,7 @@ use Illuminate\Database\Eloquent\Model;
* @property-read Table $table * @property-read Table $table
* @property-read Revision $revision * @property-read Revision $revision
*/ */
class Proposal extends Model class Proposal extends BaseModel
{ {
use Reportable; use Reportable;
protected $guarded = []; protected $guarded = [];

@ -2,7 +2,6 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Riesjart\Relaquent\Model\Concerns\HasRelaquentRelationships; 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|null $sourceProposal - proposal that was used to create this revision
* @property-read Proposal[]|Collection $dependentProposals * @property-read Proposal[]|Collection $dependentProposals
*/ */
class Revision extends Model class Revision extends BaseModel
{ {
use HasRelaquentRelationships; use HasRelaquentRelationships;
protected $guarded = []; protected $guarded = [];

@ -2,15 +2,13 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/** /**
* Row in a data table * Row in a data table
* *
* @property int $id * @property int $id
* @property string $data - JSONB * @property string $data - JSONB
*/ */
class Row extends Model class Row extends BaseModel
{ {
protected $guarded = []; protected $guarded = [];
public $timestamps = false; public $timestamps = false;

@ -5,7 +5,6 @@ namespace App\Models;
use App\Models\Concerns\Reportable; use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/** /**
* A data table object (referencing revisions) * A data table object (referencing revisions)
@ -33,7 +32,7 @@ use Illuminate\Database\Eloquent\Model;
* @property-read User[]|Collection $favouritingUsers * @property-read User[]|Collection $favouritingUsers
* @property-read User[]|Collection $discussionFollowers * @property-read User[]|Collection $discussionFollowers
*/ */
class Table extends Model class Table extends BaseModel
{ {
use Reportable; use Reportable;
protected $guarded = []; protected $guarded = [];

@ -4,7 +4,6 @@ namespace App\Models;
use App\Models\Concerns\Reportable; use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/** /**
* Comment on a data table * Comment on a data table
@ -21,7 +20,7 @@ use Illuminate\Database\Eloquent\Model;
* @property-read TableComment|null $ancestor * @property-read TableComment|null $ancestor
* @property-read TableComment[]|Collection $replies * @property-read TableComment[]|Collection $replies
*/ */
class TableComment extends Model class TableComment extends BaseModel
{ {
use Reportable; use Reportable;
protected $guarded = []; protected $guarded = [];

@ -3,13 +3,10 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/** /**
* Model representing the pivot table, used for more efficient favourite counting * Model representing the pivot table, used for more efficient favourite counting
*/ */
class TableFavouritePivot extends Model class TableFavouritePivot extends BaseModel
{ {
protected $table = 'table_favourites'; protected $table = 'table_favourites';
public $timestamps = []; public $timestamps = [];

@ -7,10 +7,15 @@ use App\Models\Concerns\InstanceCache;
use App\Models\Concerns\Reportable; use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
use MightyPork\Exceptions\ArgumentException; use MightyPork\Exceptions\ArgumentException;
use MightyPork\Exceptions\NotExistException; 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 * A user in the application
@ -37,8 +42,12 @@ use MightyPork\Exceptions\NotExistException;
* @property-read Notification[]|Collection $unreadNotifications * @property-read Notification[]|Collection $unreadNotifications
* @property-read string $handle - user handle, including @ * @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 Reportable;
use Notifiable; use Notifiable;
use InstanceCache; use InstanceCache;

Loading…
Cancel
Save