Skip to content

Commit b11f1ba

Browse files
Merge branch 'patch-1'
2 parents 79b20e5 + 42544c8 commit b11f1ba

File tree

5 files changed

+32
-43
lines changed

5 files changed

+32
-43
lines changed

.travis.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ cache:
77
matrix:
88
include:
99
- php: 7.2
10-
env: ILLUMINATE_VERSION=6.*
10+
env: ILLUMINATE_VERSION=7.*
1111
- php: 7.3
12-
env: ILLUMINATE_VERSION=6.*
12+
env: ILLUMINATE_VERSION=7.*
13+
- php: 7.4
14+
env: ILLUMINATE_VERSION=7.*
1315

1416
before_install: travis_retry composer require "illuminate/database:${ILLUMINATE_VERSION}" "illuminate/events:${ILLUMINATE_VERSION}" --no-update -v
1517

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Laravel Model UUIDs
2-
## v5.0.0
2+
## v6.0.0
33

44
[![Build Status](https://travis-ci.org/michaeldyrynda/laravel-model-uuid.svg?branch=master)](https://travis-ci.org/michaeldyrynda/laravel-model-uuid)
55
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/michaeldyrynda/laravel-model-uuid/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/michaeldyrynda/laravel-model-uuid/?branch=master)
@@ -105,14 +105,17 @@ If you use the suggested [laravel-efficient-uuid](https://github.com/michaeldyry
105105

106106
namespace App;
107107

108-
use Illuminate\Database\Eloquent\Model;
108+
use Dyrynda\Database\Support\Casts\EfficientUuid;
109109
use Dyrynda\Database\Support\GeneratesUuid;
110+
use Illuminate\Database\Eloquent\Model;
110111

111112
class Post extends Model
112113
{
113114
use GeneratesUuid;
114115

115-
protected $casts = ['uuid' => 'uuid'];
116+
protected $casts = [
117+
'uuid' => EfficientUuid::class,
118+
];
116119
}
117120
```
118121

composer.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^7.2",
20-
"illuminate/database": "^6.0",
21-
"illuminate/events": "^6.0",
22-
"illuminate/support": "^6.0",
19+
"php": "^7.2.5",
20+
"illuminate/database": "^6.0|^7.0",
21+
"illuminate/events": "^6.0|^7.0",
22+
"illuminate/support": "^6.0|^7.0",
2323
"ramsey/uuid": "~3.7",
2424
"moontoast/math": "^1.1"
2525
},

src/GeneratesUuid.php

+11-27
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Dyrynda\Database\Support;
44

5-
use Illuminate\Contracts\Support\Arrayable;
6-
use Illuminate\Database\Eloquent\Builder;
5+
use Ramsey\Uuid\Uuid;
76
use Illuminate\Support\Arr;
87
use Illuminate\Support\Str;
9-
use Ramsey\Uuid\Uuid;
8+
use Illuminate\Database\Eloquent\Builder;
9+
use Illuminate\Contracts\Support\Arrayable;
1010

1111
/**
1212
* UUID generation trait.
@@ -56,14 +56,16 @@ abstract public function hasCast($key, $types = null);
5656
public static function bootGeneratesUuid(): void
5757
{
5858
static::creating(function ($model) {
59-
/* @var \Illuminate\Database\Eloquent\Model|static $model */
60-
$uuid = $model->resolveUuid();
6159
foreach ($model->uuidColumns() as $item) {
60+
/* @var \Illuminate\Database\Eloquent\Model|static $model */
61+
$uuid = $model->resolveUuid();
62+
6263
if (isset($model->attributes[$item]) && ! is_null($model->attributes[$item])) {
6364
/* @var \Ramsey\Uuid\Uuid $uuid */
6465
$uuid = $uuid->fromString(strtolower($model->attributes[$item]));
6566
}
66-
$model->attributes[$item] = $model->hasCast($item, 'uuid') ? $uuid->getBytes() : $uuid->toString();
67+
68+
$model->{$item} = strtolower($uuid->toString());
6769
}
6870
});
6971
}
@@ -131,11 +133,9 @@ public function scopeWhereUuid($query, $uuid, $uuidColumn = null): Builder
131133
? $uuidColumn
132134
: $this->uuidColumns()[0];
133135

134-
if ($this->hasCast($uuidColumn)) {
135-
$uuid = $this->bytesFromUuid($uuid);
136-
}
137-
138-
return $query->whereIn($uuidColumn, Arr::wrap($uuid));
136+
return $query->whereIn($uuidColumn, array_map(function ($uuid) {
137+
return strtolower($uuid);
138+
}, Arr::wrap($uuid)));
139139
}
140140

141141
/**
@@ -156,20 +156,4 @@ protected function bytesFromUuid($uuid): array
156156

157157
return Arr::wrap($this->resolveUuid()->fromString($uuid)->getBytes());
158158
}
159-
160-
/**
161-
* Cast an attribute to a native PHP type.
162-
*
163-
* @param string $key
164-
* @param mixed $value
165-
* @return mixed
166-
*/
167-
protected function castAttribute($key, $value)
168-
{
169-
if (in_array($key, $this->uuidColumns()) && ! empty($value)) {
170-
return $this->resolveUuid()->fromBytes($value)->toString();
171-
}
172-
173-
return parent::castAttribute($key, $value);
174-
}
175159
}

tests/Feature/UuidTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
namespace Tests\Feature;
44

5-
use Illuminate\Container\Container;
6-
use Illuminate\Database\Capsule\Manager;
7-
use Illuminate\Events\Dispatcher;
5+
use Tests\Fixtures\Post;
6+
use Tests\Fixtures\UncastPost;
87
use PHPUnit\Framework\TestCase;
9-
use Tests\Fixtures\CustomCastUuidPost;
8+
use Tests\Fixtures\OrderedPost;
9+
use Illuminate\Events\Dispatcher;
1010
use Tests\Fixtures\CustomUuidPost;
11+
use Illuminate\Container\Container;
1112
use Tests\Fixtures\MultipleUuidPost;
12-
use Tests\Fixtures\OrderedPost;
13-
use Tests\Fixtures\Post;
14-
use Tests\Fixtures\UncastPost;
13+
use Tests\Fixtures\CustomCastUuidPost;
14+
use Illuminate\Database\Capsule\Manager;
1515

1616
class UuidTest extends TestCase
1717
{

0 commit comments

Comments
 (0)