Skip to content

Commit

Permalink
make getCustomColumns() and getDataColumn() static again
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed Nov 8, 2023
1 parent 72de33a commit 89e00fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
18 changes: 9 additions & 9 deletions src/VirtualColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -54,7 +54,7 @@ protected function decodeVirtualColumn(): void
$this->syncOriginalAttribute($key);
}

$this->setAttribute($this->getDataColumn(), null);
$this->setAttribute(static::getDataColumn(), null);

$this->dataEncoded = false;
}
Expand All @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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;
}
}
8 changes: 4 additions & 4 deletions tests/VirtualColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class MyModel extends ParentModel

class FooModel extends ParentModel
{
public function getCustomColumns(): array
public static function getCustomColumns(): array
{
return [
'id',
Expand All @@ -192,7 +192,7 @@ public function getCustomColumns(): array
];
}

public function getDataColumn(): string
public static function getDataColumn(): string
{
return 'virtual';
}
Expand All @@ -215,7 +215,7 @@ class FooChild extends ParentModel
{
public $table = 'foo_childs';

public function getCustomColumns(): array
public static function getCustomColumns(): array
{
return [
'id',
Expand All @@ -227,7 +227,7 @@ class BarChild extends ParentModel
{
public $table = 'bar_childs';

public function getCustomColumns(): array
public static function getCustomColumns(): array
{
return [
'id',
Expand Down

0 comments on commit 89e00fb

Please sign in to comment.