Skip to content

Commit cd75f9b

Browse files
Add document version setter, remove public property
1 parent 991b79a commit cd75f9b

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

src/Eloquent/HasDocumentVersion.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
trait HasDocumentVersion
88
{
9-
public $documentVersion = 1;
10-
119
/**
1210
* Auto call on model instance as booting
1311
*
@@ -17,23 +15,26 @@ public static function bootHasDocumentVersion(): void
1715
{
1816
static::saving(function ($model) {
1917
$version = $model->getDocumentVersion();
20-
if (empty($version)) {
21-
$model->{static::getDocumentVersionKey()} = $model->documentVersion ?? 1;
18+
19+
if (!$version) {
20+
$model->setDocumentVersion(static::getCurrentVersion());
2221
}
2322
});
2423

2524
static::retrieved(function ($model) {
26-
$model->migrateDocumentVersion($model->getDocumentVersion() ?? 0);
25+
$model->migrateDocumentVersion($model->getDocumentVersion());
2726
});
2827
}
2928

3029
/**
3130
* migrate model document version schema
3231
*
33-
* @param int $fromVersion
32+
* @param int|null $fromVersion
3433
* @return void
3534
*/
36-
public function migrateDocumentVersion(int $fromVersion): void {}
35+
public function migrateDocumentVersion($fromVersion): void
36+
{
37+
}
3738

3839
/**
3940
* Get Current document version
@@ -45,6 +46,15 @@ public function getDocumentVersion(): ?int
4546
return $this->{static::getDocumentVersionKey()} ?? null;
4647
}
4748

49+
/**
50+
* @param int $version
51+
* @return void
52+
*/
53+
public function setDocumentVersion(int $version): void
54+
{
55+
$this->{static::getDocumentVersionKey()} = $version;
56+
}
57+
4858
/**
4959
* Get document version key
5060
*
@@ -56,4 +66,14 @@ protected static function getDocumentVersionKey(): string
5666
? (string) static::DOCUMENT_VERSION_KEY
5767
: 'schema_version';
5868
}
69+
70+
/**
71+
* @return int
72+
*/
73+
protected static function getCurrentVersion(): int
74+
{
75+
return defined(static::class.'::DOCUMENT_VERSION')
76+
? (int) static::DOCUMENT_VERSION
77+
: 1;
78+
}
5979
}

tests/DocumentVersionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ public function testCreateWithNullId()
2525

2626
$document = DocumentVersion::query()->where('name', 'Luc')->first();
2727
$this->assertEquals(35, $document->age);
28+
$this->assertEquals(2, $document->getDocumentVersion());
2829

2930
// Test without migration
3031
$newDocument = new DocumentVersion(['name' => 'Vador']);
31-
$newDocument->documentVersion = 2;
32+
$newDocument->setDocumentVersion(2);
3233
$newDocument->save();
3334

3435
$this->assertEquals(2, $newDocument->getDocumentVersion());

tests/Models/DocumentVersion.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ class DocumentVersion extends Eloquent
1414
protected $connection = 'mongodb';
1515
protected $collection = 'documentVersion';
1616
protected static $unguarded = true;
17-
public function migrateDocumentVersion(int $fromVersion): void
17+
18+
public function migrateDocumentVersion($fromVersion): void
1819
{
1920
if ($fromVersion) {
2021
if ($fromVersion < 2) {
2122
$this->age = 35;
23+
24+
$this->setDocumentVersion(2);
2225
}
2326
}
2427
}

0 commit comments

Comments
 (0)