Skip to content

Commit

Permalink
make $this and $model use clear and consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed Nov 8, 2023
1 parent ff84ef1 commit 2635613
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
19 changes: 10 additions & 9 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/routes.php</file>
</exclude>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd">
<coverage/>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests</directory>
Expand All @@ -25,4 +18,12 @@
<env name="DB_DATABASE" value=":memory:"/>
<env name="AWS_DEFAULT_REGION" value="us-west-2"/>
</php>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/routes.php</file>
</exclude>
</source>
</phpunit>
28 changes: 28 additions & 0 deletions phpunit.xml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/routes.php</file>
</exclude>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:+osRhaqQtOcYM79fhVU8YdNBs/1iVJPWYUr9zvTPCs0="/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="redis"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="AWS_DEFAULT_REGION" value="us-west-2"/>
</php>
</phpunit>
46 changes: 23 additions & 23 deletions src/VirtualColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ trait VirtualColumn
*/
public bool $dataEncoded = false;

protected function decodeVirtualColumn(self $model): void
protected function decodeVirtualColumn(): void
{
if (! $model->dataEncoded) {
if (! $this->dataEncoded) {
return;
}

Expand All @@ -42,46 +42,46 @@ protected function decodeVirtualColumn(self $model): void
['encrypted', 'encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object'], // Default encrypted castables
);

foreach ($model->getAttribute($this->getDataColumn()) ?? [] as $key => $value) {
$attributeHasEncryptedCastable = in_array(data_get($model->getCasts(), $key), $encryptedCastables);
foreach ($this->getAttribute($this->getDataColumn()) ?? [] as $key => $value) {
$attributeHasEncryptedCastable = in_array(data_get($this->getCasts(), $key), $encryptedCastables);

if ($attributeHasEncryptedCastable && $this->valueEncrypted($value)) {
$model->attributes[$key] = $value;
$this->attributes[$key] = $value;
} else {
$model->setAttribute($key, $value);
$this->setAttribute($key, $value);
}

$model->syncOriginalAttribute($key);
$this->syncOriginalAttribute($key);
}

$model->setAttribute($this->getDataColumn(), null);
$this->setAttribute($this->getDataColumn(), null);

$model->dataEncoded = false;
$this->dataEncoded = false;
}

protected function encodeAttributes(self $model): void
protected function encodeAttributes(): void
{
if ($model->dataEncoded) {
if ($this->dataEncoded) {
return;
}

$dataColumn = $this->getDataColumn();
$customColumns = $this->getCustomColumns();
$attributes = array_filter($model->getAttributes(), fn ($key) => ! in_array($key, $customColumns), ARRAY_FILTER_USE_KEY);
$attributes = array_filter($this->getAttributes(), fn ($key) => ! in_array($key, $customColumns), ARRAY_FILTER_USE_KEY);

// Remove data column from the attributes
unset($attributes[$dataColumn]);

foreach ($attributes as $key => $value) {
// Remove attribute from the model
unset($model->attributes[$key]);
unset($model->original[$key]);
unset($this->attributes[$key]);
unset($this->original[$key]);
}

// Add attribute to the data column
$model->setAttribute($dataColumn, $attributes);
$this->setAttribute($dataColumn, $attributes);

$model->dataEncoded = true;
$this->dataEncoded = true;
}

public function valueEncrypted(string $value): bool
Expand All @@ -95,22 +95,22 @@ public function valueEncrypted(string $value): bool
}
}

protected function decodeAttributes(self $model)
protected function decodeAttributes()
{
$model->dataEncoded = true;
$this->dataEncoded = true;

$this->decodeVirtualColumn($model);
$this->decodeVirtualColumn();
}

protected function getAfterListeners(): array
{
return [
'retrieved' => [
function ($model) {
function () {
// Always decode after model retrieval
$model->dataEncoded = true;
$this->dataEncoded = true;

$this->decodeVirtualColumn($model);
$this->decodeVirtualColumn();
},
],
'saving' => [
Expand All @@ -128,7 +128,7 @@ function ($model) {
protected function decodeIfEncoded()
{
if ($this->dataEncoded) {
$this->decodeVirtualColumn($this);
$this->decodeVirtualColumn();
}
}

Expand Down

0 comments on commit 2635613

Please sign in to comment.