Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
add UUID setter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Feb 15, 2020
1 parent eae21e8 commit a104ef9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![StyleCI](https://styleci.io/repos/240738815/shield)](https://styleci.io/repos/240738815)
[![Total Downloads](https://img.shields.io/packagist/dt/astrotomic/laravel-eloquent-uuid.svg?label=Downloads&style=flat-square)](https://packagist.org/packages/astrotomic/laravel-eloquent-uuid)

TL;DR
A simple drop-in solution for UUID support in your Eloquent models.

## Installation

Expand Down
4 changes: 4 additions & 0 deletions src/Eloquent/Concerns/UsesUUID.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public function scopeByUuid(Builder $query, $uuid): Builder
*/
public function setUuid($uuid): Model
{
if (! Uuid::isValid($uuid)) {
throw new InvalidArgumentException('The UUID has to be of type string, array or null.');
}

return $this->setAttribute($this->getUuidName(), strval($uuid));
}

Expand Down
9 changes: 9 additions & 0 deletions tests/UsesUuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,13 @@ public function it_throws_exception_when_invalid_type_passed_to_scope()

PostUuidAttribute::byUuid(1.5)->firstOrFail();
}

/** @test */
public function it_throws_exception_when_invalid_uuid_set()
{
static::expectException(InvalidArgumentException::class);

$post = new PostUuidAttribute();
$post->setUuid('foobar');
}
}

0 comments on commit a104ef9

Please sign in to comment.