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

Commit b0c23d6

Browse files
authored
Merge pull request #40 from andrzejkupczyk/master
Prevent decoding the uuid when the model does not exist
2 parents ed1a38e + 7df6724 commit b0c23d6

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 7.0
54
- 7.1
65
- 7.2
76

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^7.0",
19+
"php": "^7.1",
2020
"illuminate/queue": "~5.5.28|~5.6.0",
2121
"ramsey/uuid": "^3.7"
2222
},

src/HasBinaryUuid.php

+9-1
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

+9
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)