Skip to content
This repository has been archived by the owner on Apr 7, 2021. It is now read-only.

Commit

Permalink
Merge 9832f83 into d99bffc
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkTierney1975 committed Feb 23, 2018
2 parents d99bffc + 9832f83 commit 602b4ac
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Traits/HasEncryptedAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,41 @@ public function getDirty()
return $dirty;
}

/**
* Determine if the new and old values for a given key are equivalent.
*
* @param string $key
* @param mixed $current
* @return bool
*/
protected function originalIsEquivalent($key, $current)
{
if (! array_key_exists($key, $this->original)) {
return false;
}

$original = $this->getOriginal($key);

if (isset($this->encrypted) && is_array($this->encrypted) && in_array($key, $this->encrypted)) {
$current = $this->decryptedAttribute($current);
$original = $this->decryptedAttribute($this->getOriginal($key));
}

if ($current === $original) {
return true;
} elseif (is_null($current)) {
return false;
} elseif ($this->isDateAttribute($key)) {
return $this->fromDateTime($current) ===
$this->fromDateTime($original);
} elseif ($this->hasCast($key)) {
return $this->castAttribute($key, $current) ===
$this->castAttribute($key, $original);
}
return is_numeric($current) && is_numeric($original)
&& strcmp((string) $current, (string) $original) === 0;
}

/**
* Set a given attribute on the model.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Traits/DirtyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,19 @@ public function testUpdate()
$this->assertNotEquals($model->getOriginal('shouldnt_be_encrypted'), $new_model->getOriginal('shouldnt_be_encrypted'));
$this->assertNotEquals($model->shouldnt_be_encrypted, $new_model->shouldnt_be_encrypted);
}

public function testDontEncryptUnchangedAttributes()
{
$string = 'Hello world!';

$model = DatabaseModel::create(['should_be_encrypted' => $string]);

$this->assertTrue($model->exists);

$model->should_be_encrypted = $string;

$changedAttributes = $model->getDirty();

$this->assertTrue(empty($changedAttributes));
}
}

0 comments on commit 602b4ac

Please sign in to comment.