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

make $this and $model use clear and consistent

This commit is contained in:
Samuel Štancl 2023-11-08 10:03:27 +01:00
parent ff84ef1379
commit 26356130b1
3 changed files with 61 additions and 32 deletions

View file

@ -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>
@ -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
phpunit.xml.bak Normal file
View file

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

View file

@ -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;
}
@ -42,46 +42,46 @@ trait VirtualColumn
['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
@ -95,22 +95,22 @@ trait VirtualColumn
}
}
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' => [
@ -128,7 +128,7 @@ trait VirtualColumn
protected function decodeIfEncoded()
{
if ($this->dataEncoded) {
$this->decodeVirtualColumn($this);
$this->decodeVirtualColumn();
}
}