datatable.directory codebase https://datatable.directory/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
datatable.directory/app/Models/Proposal.php

105 lines
2.9 KiB

7 years ago
<?php
namespace App\Models;
use App\Models\Concerns\Reportable;
use App\Tables\Changeset;
use MightyPork\Exceptions\NotApplicableException;
7 years ago
/**
* Change proposal
*
* @property int $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property int $table_id
* @property int $revision_id
* @property int $author_id
* @property string $note
* @property Proposal $changes - JSONB
* @property-read User $author
* @property-read Table $table
* @property-read Revision $revision
7 years ago
*/
class Proposal extends BaseModel
7 years ago
{
use Reportable;
7 years ago
protected $guarded = [];
protected $touches = ['author', 'table'];
7 years ago
/** Authoring user */
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
/** Target revision */
public function revision()
{
return $this->belongsTo(Revision::class);
}
/** Target table (that this was submitted to) */
public function table()
{
return $this->belongsTo(Table::class);
}
public function getChangesAttribute($value)
{
$changeset = Changeset::fromObject(fromJSON($value));
$changeset->revision = $this->revision;
$changeset->table = $this->table;
$changeset->note = $this->note;
return $changeset;
}
public function setChangesAttribute($value)
{
if ($value instanceof Changeset) {
$this->attributes['changes'] = toJSON($value->toObject());
} else {
throw new NotApplicableException("Only a Changeset may be set to Proposal->changes");
}
}
/**
* Create a new Proposal instance wrapping this changeset,
* owned by the currently logged in User. The created instance
* is NOT saved yet.
*
* @param Changeset $changeset - changeset to hydrate the proposal with
* @return Proposal
*/
public static function fromChangeset(Changeset $changeset)
{
if (!$changeset->hasAnyChanges()) {
throw new NotApplicableException('No changes to propose.');
}
if ($changeset->note == null) {
throw new NotApplicableException('Proposal note must be filled.');
}
if ($changeset->table == null || !$changeset->table instanceof Table) {
throw new NotApplicableException('Table not assigned to Changeset');
}
if ($changeset->revision == null || !$changeset->revision instanceof Revision) {
throw new NotApplicableException('Revision not assigned to Changeset');
}
return new Proposal([
// relations
'table_id' => $changeset->table->getKey(),
'revision_id' => $changeset->revision->getKey(),
'author_id' => \Auth::user()->getKey(),
// the proposal info
'note' => $changeset->note,
'changes' => $changeset->toObject(),
]);
}
7 years ago
}