mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-12 17:24:03 +00:00
Refactor $afterListeneres
This commit is contained in:
parent
826c33f647
commit
b82084ba90
1 changed files with 19 additions and 15 deletions
|
|
@ -97,19 +97,27 @@ trait VirtualColumn
|
||||||
static::decodeVirtualColumn($model);
|
static::decodeVirtualColumn($model);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function bootVirtualColumn()
|
protected function getAfterListeners(): array
|
||||||
{
|
{
|
||||||
static::registerAfterListener('retrieved', function ($model) {
|
return [
|
||||||
// We always decode after model retrieval.
|
'retrieved' => [
|
||||||
|
function ($model) {
|
||||||
|
// Always decode after model retrieval
|
||||||
$model->dataEncoded = true;
|
$model->dataEncoded = true;
|
||||||
|
|
||||||
static::decodeVirtualColumn($model);
|
$this->decodeVirtualColumn($model);
|
||||||
});
|
}
|
||||||
|
],
|
||||||
// Encode if writing
|
'saving' => [
|
||||||
static::registerAfterListener('saving', [static::class, 'encodeAttributes']);
|
[$this, 'encodeAttributes']
|
||||||
static::registerAfterListener('creating', [static::class, 'encodeAttributes']);
|
],
|
||||||
static::registerAfterListener('updating', [static::class, 'encodeAttributes']);
|
'creating' => [
|
||||||
|
[$this, 'encodeAttributes']
|
||||||
|
],
|
||||||
|
'updating' => [
|
||||||
|
[$this, 'encodeAttributes']
|
||||||
|
],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function decodeIfEncoded()
|
protected function decodeIfEncoded()
|
||||||
|
|
@ -132,11 +140,7 @@ trait VirtualColumn
|
||||||
|
|
||||||
public function runAfterListeners($event, $halt = true)
|
public function runAfterListeners($event, $halt = true)
|
||||||
{
|
{
|
||||||
$listeners = static::$afterListeners[$event] ?? [];
|
$listeners = $this->getAfterListeners()[$event] ?? [];
|
||||||
|
|
||||||
// $listeners = array_filter(static::$afterListeners[$event] ?? [], function ($listener) {
|
|
||||||
// return $listener[0] === static::class;
|
|
||||||
// });
|
|
||||||
|
|
||||||
if (! $event) {
|
if (! $event) {
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue