Skip to content

Commit 8f1fad7

Browse files
Merge branch 'mostafaznv/main'
2 parents 605a37c + 75a42fa commit 8f1fad7

File tree

2 files changed

+159
-9
lines changed

2 files changed

+159
-9
lines changed

src/GeneratesUuid.php

+50-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
*
2424
* @property string $uuidVersion
2525
*
26-
* @method static \Illuminate\Database\Eloquent\Builder whereUuid(string $uuid)
26+
* @method static \Illuminate\Database\Eloquent\Builder whereUuid(string|string[] $uuid, ?string $uuidColumn = null)
27+
* @method static \Illuminate\Database\Eloquent\Builder whereNotUuid(string|string[] $uuid, ?string $uuidColumn = null)
2728
*/
2829
trait GeneratesUuid
2930
{
@@ -121,17 +122,28 @@ public function resolveUuidVersion(): string
121122
*/
122123
public function scopeWhereUuid($query, $uuid, $uuidColumn = null): Builder
123124
{
124-
$uuidColumn = ! is_null($uuidColumn) && in_array($uuidColumn, $this->uuidColumns())
125-
? $uuidColumn
126-
: $this->uuidColumns()[0];
125+
$uuidColumn = $this->getUuidColumn($uuidColumn);
126+
$uuid = $this->prepareUuid($uuid, $uuidColumn);
127127

128-
$uuid = $this->normaliseUuids($uuid);
128+
return $query->whereIn(
129+
$this->qualifyColumn($uuidColumn),
130+
Arr::wrap($uuid)
131+
);
132+
}
129133

130-
if ($this->isClassCastable($uuidColumn)) {
131-
$uuid = $this->bytesFromUuid($uuid);
132-
}
134+
/**
135+
* Scope queries to find by UUID.
136+
*
137+
* @param \Illuminate\Database\Eloquent\Builder $query
138+
* @param string|array $uuid
139+
* @param string $uuidColumn
140+
*/
141+
public function scopeWhereNotUuid($query, $uuid, $uuidColumn = null): Builder
142+
{
143+
$uuidColumn = $this->getUuidColumn($uuidColumn);
144+
$uuid = $this->prepareUuid($uuid, $uuidColumn);
133145

134-
return $query->whereIn(
146+
return $query->whereNotIn(
135147
$this->qualifyColumn($uuidColumn),
136148
Arr::wrap($uuid)
137149
);
@@ -172,4 +184,33 @@ protected function normaliseUuids($uuid): array
172184

173185
return $uuid;
174186
}
187+
188+
/**
189+
* Guess UUID column based on model configurations or given uuid column
190+
*
191+
* @param ?string $uuidColumn
192+
*/
193+
protected function getUuidColumn($uuidColumn = null): string
194+
{
195+
return ! is_null($uuidColumn) && in_array($uuidColumn, $this->uuidColumns())
196+
? $uuidColumn
197+
: $this->uuidColumns()[0];
198+
}
199+
200+
/**
201+
* Prepare UUID by normalization
202+
*
203+
* @param string|array $uuid
204+
* @param string $uuidColumn
205+
*/
206+
protected function prepareUuid($uuid, $uuidColumn): array|string
207+
{
208+
$uuid = $this->normaliseUuids($uuid);
209+
210+
if ($this->isClassCastable($uuidColumn)) {
211+
$uuid = $this->bytesFromUuid($uuid);
212+
}
213+
214+
return $uuid;
215+
}
175216
}

tests/Feature/UuidTest.php

+109
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ public function you_can_find_a_model_by_its_uuid()
5555
$this->assertSame($uuid, $post->uuid);
5656
}
5757

58+
/** @test */
59+
public function you_can_exclude_a_model_by_its_uuid()
60+
{
61+
$uuid = '55635d83-10bc-424f-bf3f-395ea7a5b47f';
62+
63+
Post::create(['title' => 'test post', 'uuid' => $uuid]);
64+
65+
$this->assertNull(
66+
Post::whereNotUuid($uuid)->first()
67+
);
68+
}
69+
5870
/** @test */
5971
public function you_can_find_a_model_by_custom_uuid_parameter()
6072
{
@@ -88,6 +100,24 @@ public function you_can_search_by_array_of_uuids()
88100
])->count());
89101
}
90102

103+
/** @test */
104+
public function you_can_exclude_by_array_of_uuids()
105+
{
106+
Post::create(['title' => 'first post', 'uuid' => '8ab48e77-d9cd-4fe7-ace5-a5a428590c18']);
107+
Post::create(['title' => 'second post', 'uuid' => 'c7c26456-ddb0-45cd-9b1c-318296cce7a3']);
108+
Post::create(['title' => 'third post', 'uuid' => 'e99d440e-fa25-45f2-ba2f-7c4c48f6fb5d']);
109+
110+
$uuids = [
111+
'8ab48e77-d9cd-4fe7-ace5-A5A428590C18',
112+
'c7c26456-ddb0-45cd-9b1c-318296cce7a3',
113+
];
114+
115+
$posts = Post::whereNotUuid($uuids)->get();
116+
117+
$this->assertEquals(1, $posts->count());
118+
$this->assertEquals('e99d440e-fa25-45f2-ba2f-7c4c48f6fb5d', $posts->get(0)->uuid);
119+
}
120+
91121
/** @test */
92122
public function you_can_search_by_array_of_efficient_uuids()
93123
{
@@ -100,6 +130,24 @@ public function you_can_search_by_array_of_efficient_uuids()
100130
], 'efficient_uuid')->count());
101131
}
102132

133+
/** @test */
134+
public function you_can_exclude_by_array_of_efficient_uuids()
135+
{
136+
EfficientUuidPost::create(['title' => 'first post', 'efficient_uuid' => '8ab48e77-d9cd-4fe7-ace5-a5a428590c18']);
137+
EfficientUuidPost::create(['title' => 'second post', 'efficient_uuid' => 'c7c26456-ddb0-45cd-9b1c-318296cce7a3']);
138+
EfficientUuidPost::create(['title' => 'third post', 'efficient_uuid' => 'e99d440e-fa25-45f2-ba2f-7c4c48f6fb5d']);
139+
140+
$uuids = [
141+
'8ab48e77-d9cd-4fe7-ace5-A5A428590C18',
142+
'c7c26456-ddb0-45cd-9b1c-318296cce7a3',
143+
];
144+
145+
$posts = EfficientUuidPost::whereNotUuid($uuids, 'efficient_uuid')->get();
146+
147+
$this->assertEquals(1, $posts->count());
148+
$this->assertSame('e99d440e-fa25-45f2-ba2f-7c4c48f6fb5d', $posts->get(0)->efficient_uuid);
149+
}
150+
103151
/** @test */
104152
public function you_can_search_by_array_of_uuids_for_custom_column()
105153
{
@@ -112,6 +160,24 @@ public function you_can_search_by_array_of_uuids_for_custom_column()
112160
], 'custom_uuid')->count());
113161
}
114162

163+
/** @test */
164+
public function you_can_exclude_by_array_of_uuids_for_custom_column()
165+
{
166+
CustomCastUuidPost::create(['title' => 'first post', 'custom_uuid' => '8ab48e77-d9cd-4fe7-ace5-a5a428590c18']);
167+
CustomCastUuidPost::create(['title' => 'second post', 'custom_uuid' => 'c7c26456-ddb0-45cd-9b1c-318296cce7a3']);
168+
CustomCastUuidPost::create(['title' => 'third post', 'custom_uuid' => 'e99d440e-fa25-45f2-ba2f-7c4c48f6fb5d']);
169+
170+
$uuids = [
171+
'8ab48e77-d9cd-4fe7-ace5-A5A428590C18',
172+
'c7c26456-ddb0-45cd-9b1c-318296cce7a3',
173+
];
174+
175+
$posts = CustomCastUuidPost::whereNotUuid($uuids, 'custom_uuid')->get();
176+
177+
$this->assertEquals(1, $posts->count());
178+
$this->assertSame('e99d440e-fa25-45f2-ba2f-7c4c48f6fb5d', $posts->get(0)->custom_uuid);
179+
}
180+
115181
/** @test */
116182
public function you_can_search_by_array_of_uuids_which_contains_an_invalid_uuid()
117183
{
@@ -125,6 +191,25 @@ public function you_can_search_by_array_of_uuids_which_contains_an_invalid_uuid(
125191
])->count());
126192
}
127193

194+
/** @test */
195+
public function you_can_exclude_by_array_of_uuids_which_contains_an_invalid_uuid()
196+
{
197+
Post::create(['title' => 'first post', 'uuid' => '8ab48e77-d9cd-4fe7-ace5-a5a428590c18']);
198+
Post::create(['title' => 'second post', 'uuid' => 'c7c26456-ddb0-45cd-9b1c-318296cce7a3']);
199+
Post::create(['title' => 'third post', 'uuid' => 'e99d440e-fa25-45f2-ba2f-7c4c48f6fb5d']);
200+
201+
$uuids = [
202+
'8ab48e77-d9cd-4fe7-ace5-A5A428590C18',
203+
'c7c26456-ddb0-45cd-9b1c-318296cce7a3',
204+
'this is invalid',
205+
];
206+
207+
$posts = Post::whereNotUuid($uuids)->get();
208+
209+
$this->assertEquals(1, $posts->count());
210+
$this->assertEquals('e99d440e-fa25-45f2-ba2f-7c4c48f6fb5d', $posts->get(0)->uuid);
211+
}
212+
128213
/** @test */
129214
public function you_can_generate_a_uuid_without_casting()
130215
{
@@ -164,6 +249,18 @@ public function you_can_find_a_model_by_uuid_without_casting()
164249
$this->assertSame($uuid, $post->uuid);
165250
}
166251

252+
/** @test */
253+
public function you_can_exclude_a_model_by_uuid_without_casting()
254+
{
255+
$uuid = 'b270f651-4db8-407b-aade-8666aca2750e';
256+
257+
UncastPost::create(['title' => 'test-post', 'uuid' => $uuid]);
258+
259+
$post = UncastPost::whereNotUuid($uuid)->first();
260+
261+
$this->assertNull($post);
262+
}
263+
167264
/** @test */
168265
public function you_can_find_a_model_by_uuid_with_casting()
169266
{
@@ -177,6 +274,18 @@ public function you_can_find_a_model_by_uuid_with_casting()
177274
$this->assertSame($uuid, $post->uuid);
178275
}
179276

277+
/** @test */
278+
public function you_can_exclude_a_model_by_uuid_with_casting()
279+
{
280+
$uuid = 'b270f651-4db8-407b-aade-8666aca2750e';
281+
282+
EfficientUuidPost::create(['title' => 'efficient uuid', 'uuid' => $uuid]);
283+
284+
$post = EfficientUuidPost::whereNotUuid($uuid)->first();
285+
286+
$this->assertNull($post);
287+
}
288+
180289
/** @test */
181290
public function it_handles_time_ordered_uuids()
182291
{

0 commit comments

Comments
 (0)