Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit 97485c9

Browse files
author
Andrzej Kupczyk
committed
Prevent decoding the uuid when the model does not exist
1 parent ed1a38e commit 97485c9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/HasBinaryUuid.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,19 @@ public static function decodeUuid(string $binaryUuid): string
7373

7474
public function toArray()
7575
{
76+
if (! $this->exists) {
77+
return parent::toArray();
78+
}
79+
7680
return array_merge(parent::toArray(), [$this->getKeyName() => $this->uuid_text]);
7781
}
7882

79-
public function getUuidTextAttribute(): string
83+
public function getUuidTextAttribute(): ?string
8084
{
85+
if (! $this->exists) {
86+
return null;
87+
}
88+
8189
return static::decodeUuid($this->{$this->getKeyName()});
8290
}
8391

tests/Feature/HasBinaryUuidTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,13 @@ public function it_serialises_the_model_correctly_with_json_encode()
155155
$this->assertContains($model->uuid_text, $json);
156156
$this->assertNotContains($model->uuid, $json);
157157
}
158+
159+
/** @test */
160+
public function it_prevents_decoding_the_uuid_when_the_model_does_not_exist()
161+
{
162+
$model = new TestModel;
163+
164+
$this->assertEmpty($model->toArray());
165+
$this->assertNull($model->uuid_text);
166+
}
158167
}

0 commit comments

Comments
 (0)