1
0
mirror of https://github.com/flarum/core.git synced 2025-10-09 14:06:26 +02:00

Clean up merging stuff

This commit is contained in:
Toby Zerner
2015-07-01 16:31:06 +09:30
parent 3f32236379
commit d44b101373
7 changed files with 162 additions and 118 deletions

View File

@@ -0,0 +1,27 @@
<?php namespace Flarum\Core\Models;
abstract class EventPost extends Post implements MergeableInterface
{
use MergeableTrait;
/**
* Unserialize the content attribute.
*
* @param string $value
* @return string
*/
public function getContentAttribute($value)
{
return json_decode($value, true);
}
/**
* Serialize the content attribute.
*
* @param string $value
*/
public function setContentAttribute($value)
{
$this->attributes['content'] = json_encode($value);
}
}

View File

@@ -0,0 +1,6 @@
<?php namespace Flarum\Core\Models;
interface MergeableInterface
{
public function saveAfter(Model $previous);
}

View File

@@ -0,0 +1,23 @@
<?php namespace Flarum\Core\Models;
trait MergeableTrait
{
public function saveAfter(Model $previous)
{
if ($previous instanceof static) {
if ($merged = $this->mergeInto($previous)) {
$merged->save();
return $merged;
} else {
$previous->delete();
return $previous;
}
}
$this->save();
return $this;
}
abstract protected function mergeInto(Model $previous);
}