From 89e00fbcd95d696cb5b4d4d901dc6d735c088d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 8 Nov 2023 22:32:12 +0100 Subject: [PATCH] make getCustomColumns() and getDataColumn() static again --- src/VirtualColumn.php | 18 +++++++++--------- tests/VirtualColumnTest.php | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/VirtualColumn.php b/src/VirtualColumn.php index c09a6fe..e3cfec6 100644 --- a/src/VirtualColumn.php +++ b/src/VirtualColumn.php @@ -42,7 +42,7 @@ protected function decodeVirtualColumn(): void ['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object'], // Default encrypted castables ); - foreach ($this->getAttribute($this->getDataColumn()) ?? [] as $key => $value) { + foreach ($this->getAttribute(static::getDataColumn()) ?? [] as $key => $value) { $attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables); if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) { @@ -54,7 +54,7 @@ protected function decodeVirtualColumn(): void $this->syncOriginalAttribute($key); } - $this->setAttribute($this->getDataColumn(), null); + $this->setAttribute(static::getDataColumn(), null); $this->dataEncoded = false; } @@ -65,8 +65,8 @@ protected function encodeAttributes(): void return; } - $dataColumn = $this->getDataColumn(); - $customColumns = $this->getCustomColumns(); + $dataColumn = static::getDataColumn(); + $customColumns = static::getCustomColumns(); $attributes = array_filter($this->getAttributes(), fn ($key) => ! in_array($key, $customColumns), ARRAY_FILTER_USE_KEY); // Remove data column from the attributes @@ -166,19 +166,19 @@ public function runAfterListeners($event, $halt = true) public function getCasts() { return array_merge(parent::getCasts(), [ - $this->getDataColumn() => 'array', + static::getDataColumn() => 'array', ]); } /** * Get the name of the column that stores additional data. */ - public function getDataColumn(): string + public static function getDataColumn(): string { return 'data'; } - public function getCustomColumns(): array + public static function getCustomColumns(): array { return [ 'id', @@ -192,10 +192,10 @@ public function getCustomColumns(): array */ public function getColumnForQuery(string $column): string { - if (in_array($column, $this->getCustomColumns(), true)) { + if (in_array($column, static::getCustomColumns(), true)) { return $column; } - return $this->getDataColumn() . '->' . $column; + return static::getDataColumn() . '->' . $column; } } diff --git a/tests/VirtualColumnTest.php b/tests/VirtualColumnTest.php index 0d5dd6c..bb33458 100644 --- a/tests/VirtualColumnTest.php +++ b/tests/VirtualColumnTest.php @@ -183,7 +183,7 @@ class MyModel extends ParentModel class FooModel extends ParentModel { - public function getCustomColumns(): array + public static function getCustomColumns(): array { return [ 'id', @@ -192,7 +192,7 @@ public function getCustomColumns(): array ]; } - public function getDataColumn(): string + public static function getDataColumn(): string { return 'virtual'; } @@ -215,7 +215,7 @@ class FooChild extends ParentModel { public $table = 'foo_childs'; - public function getCustomColumns(): array + public static function getCustomColumns(): array { return [ 'id', @@ -227,7 +227,7 @@ class BarChild extends ParentModel { public $table = 'bar_childs'; - public function getCustomColumns(): array + public static function getCustomColumns(): array { return [ 'id',