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:
27
src copy/Core/Posts/EventPost.php
Executable file
27
src copy/Core/Posts/EventPost.php
Executable 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);
|
||||
}
|
||||
}
|
6
src copy/Core/Support/MergeableInterface.php
Normal file
6
src copy/Core/Support/MergeableInterface.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php namespace Flarum\Core\Models;
|
||||
|
||||
interface MergeableInterface
|
||||
{
|
||||
public function saveAfter(Model $previous);
|
||||
}
|
23
src copy/Core/Support/MergeableTrait.php
Normal file
23
src copy/Core/Support/MergeableTrait.php
Normal 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);
|
||||
}
|
Reference in New Issue
Block a user