preparing for proposal composition
This commit is contained in:
+59
-1
@@ -3,6 +3,8 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\Reportable;
|
||||
use App\Tables\Changeset;
|
||||
use MightyPork\Exceptions\NotApplicableException;
|
||||
|
||||
/**
|
||||
* Change proposal
|
||||
@@ -14,7 +16,7 @@ use App\Models\Concerns\Reportable;
|
||||
* @property int $revision_id
|
||||
* @property int $author_id
|
||||
* @property string $note
|
||||
* @property object $changes - JSONB
|
||||
* @property Proposal $changes - JSONB
|
||||
* @property-read User $author
|
||||
* @property-read Table $table
|
||||
* @property-read Revision $revision
|
||||
@@ -43,4 +45,60 @@ class Proposal extends BaseModel
|
||||
{
|
||||
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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user