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

Commit

Permalink
Merge pull request #18 from spatie/json_encode-support
Browse files Browse the repository at this point in the history
Add proper support for json_encode
  • Loading branch information
brendt authored Dec 1, 2017
2 parents f1eff44 + e684413 commit d29f391
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/HasBinaryUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public static function decodeUuid(string $binaryUuid): string
return Uuid::fromBytes($binaryUuid)->toString();
}

public function toArray()
{
return array_merge(parent::toArray(), [$this->getKeyName() => $this->uuid_text]);
}

public function getUuidTextAttribute(): string
{
return static::decodeUuid($this->{$this->getKeyName()});
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/HasBinaryUuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ public function it_generates_valid_routes()
$this->assertContains("/uuid-test/{$uuid}", route('uuid-test', $model));
}

/** @test */
public function it_serialises_the_model_correctly()
{
$model = TestModel::create();

$json = $model->toJson();

$this->assertContains($model->uuid_text, $json);
$this->assertNotContains($model->uuid, $json);
}

/** @test */
public function it_serialises_the_model_correctly_with_json_encode()
{
$model = TestModel::create();

$json = json_encode($model);

$this->assertContains($model->uuid_text, $json);
$this->assertNotContains($model->uuid, $json);
}

private function createModel(string $uuid, $relationUuid = null): TestModel
{
$model = new TestModel();
Expand Down

0 comments on commit d29f391

Please sign in to comment.