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

Commit d29f391

Browse files
authored
Merge pull request #18 from spatie/json_encode-support
Add proper support for json_encode
2 parents f1eff44 + e684413 commit d29f391

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/HasBinaryUuid.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public static function decodeUuid(string $binaryUuid): string
7171
return Uuid::fromBytes($binaryUuid)->toString();
7272
}
7373

74+
public function toArray()
75+
{
76+
return array_merge(parent::toArray(), [$this->getKeyName() => $this->uuid_text]);
77+
}
78+
7479
public function getUuidTextAttribute(): string
7580
{
7681
return static::decodeUuid($this->{$this->getKeyName()});

tests/Feature/HasBinaryUuidTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ public function it_generates_valid_routes()
132132
$this->assertContains("/uuid-test/{$uuid}", route('uuid-test', $model));
133133
}
134134

135+
/** @test */
136+
public function it_serialises_the_model_correctly()
137+
{
138+
$model = TestModel::create();
139+
140+
$json = $model->toJson();
141+
142+
$this->assertContains($model->uuid_text, $json);
143+
$this->assertNotContains($model->uuid, $json);
144+
}
145+
146+
/** @test */
147+
public function it_serialises_the_model_correctly_with_json_encode()
148+
{
149+
$model = TestModel::create();
150+
151+
$json = json_encode($model);
152+
153+
$this->assertContains($model->uuid_text, $json);
154+
$this->assertNotContains($model->uuid, $json);
155+
}
156+
135157
private function createModel(string $uuid, $relationUuid = null): TestModel
136158
{
137159
$model = new TestModel();

0 commit comments

Comments
 (0)