1
0
Fork 0
mirror of https://github.com/archtechx/virtualcolumn.git synced 2025-12-12 18:44:04 +00:00

Refactor $afterListeneres

This commit is contained in:
lukinovec 2023-10-17 09:42:22 +02:00
parent 826c33f647
commit b82084ba90

View file

@ -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;