From 92986b783da1bcb10341a9c8e36b9db3e767fd06 Mon Sep 17 00:00:00 2001 From: Rafik Abdulwahab Date: Fri, 30 Apr 2021 06:13:05 +0100 Subject: [PATCH 1/2] Allow versioning of hidden fields in model_data --- README.md | 22 +++++++++++++ src/Mpociot/Versionable/VersionableTrait.php | 4 +++ tests/VersionableTest.php | 34 ++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/README.md b/README.md index 9ffc492..57f119d 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,28 @@ class User extends Model { } ``` + + +### Hidden fields + +There are times you might want to include hidden fields in the version data. You might have hidden the fields with the `visible` or `hidden` properties in your model. + +You can have those fields that are typically hidden in the rest of your project saved in the version data by adding them to the `versionedHiddenFields` property of the versionable model. + +```php +class User { + + use VersionableTrait; + + // Typically hidden fields + protected $hidden = ['email', 'password']; + + // Save these hidden fields + protected $versionedHiddenFields = ['email', 'password']; + +} +``` + ### Maximum number of stored versions diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index c321b0f..f441489 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -175,7 +175,11 @@ protected function versionablePostSave() $version->versionable_id = $this->getKey(); $version->versionable_type = get_class($this); $version->user_id = $this->getAuthUserId(); + + $versionedHiddenFields = isset($this->versionedHiddenFields) ? $this->versionedHiddenFields : []; + $this->makeVisible($versionedHiddenFields); $version->model_data = serialize($this->attributesToArray()); + $this->makeHidden($versionedHiddenFields); if (!empty( $this->reason )) { $version->reason = $this->reason; diff --git a/tests/VersionableTest.php b/tests/VersionableTest.php index 7aa580c..431c815 100644 --- a/tests/VersionableTest.php +++ b/tests/VersionableTest.php @@ -507,6 +507,31 @@ public function testKeepMaxVersionCount() $this->assertEquals( $name_v3, $model->name ); } + + public function testAllowHiddenFields() { + $user = new TestHiddenFieldsUser(); + $user->name = "Marcel"; + $user->email = "m.pociot@test.php"; + $user->password = "12345"; + $user->save(); + sleep(1); + + $user->name = "John"; + $user->email = "j.barlow@test.php"; + $user->password = "6789"; + $user->save(); + sleep(1); + + $diff = $user->previousVersion()->diff(); + + $this->assertArrayHasKey('email', $diff); + $this->assertArrayHasKey('password', $diff); + $this->assertEquals( 'John', $diff['name'] ); + $this->assertEquals( 'j.barlow@test.php', $diff['email'] ); + $this->assertEquals( '6789', $diff['password'] ); + + $this->assertArrayNotHasKey('password', $user->toArray()); + } } @@ -564,3 +589,12 @@ class ModelWithJsonField extends Model protected $casts = ['json_field' => 'array']; } +class TestHiddenFieldsUser extends \Illuminate\Foundation\Auth\User { + use \Mpociot\Versionable\VersionableTrait; + + protected $table = "users"; + + protected $hidden = ['email', 'password']; + + protected $versionedHiddenFields = ['email', 'password']; +} From 1c0a7aea8bf5a61814bf1353aab08c6b4cef09bc Mon Sep 17 00:00:00 2001 From: Rafik Abdulwahab Date: Fri, 30 Apr 2021 12:40:51 +0100 Subject: [PATCH 2/2] check for hidden property with shorter syntax --- src/Mpociot/Versionable/VersionableTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index f441489..c2dd8ce 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -176,7 +176,7 @@ protected function versionablePostSave() $version->versionable_type = get_class($this); $version->user_id = $this->getAuthUserId(); - $versionedHiddenFields = isset($this->versionedHiddenFields) ? $this->versionedHiddenFields : []; + $versionedHiddenFields = $this->versionedHiddenFields ?? []; $this->makeVisible($versionedHiddenFields); $version->model_data = serialize($this->attributesToArray()); $this->makeHidden($versionedHiddenFields);