Skip to content

Commit 1598c56

Browse files
committed
remove callOnly in favor of callingOnly with callback param
1 parent 35cca50 commit 1598c56

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/UnionBuilder.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace OpenSoutheners\LaravelEloquentUnionBuilder;
44

5+
use Closure;
56
use Exception;
67
use Illuminate\Database\Eloquent\Builder;
78
use Illuminate\Database\SQLiteConnection;
@@ -28,11 +29,6 @@ final class UnionBuilder
2829
*/
2930
protected $selectModelsColumns = [];
3031

31-
/**
32-
* @var array<string>
33-
*/
34-
protected $callingOnly = [];
35-
3632
/**
3733
* Construct new instance of class.
3834
*
@@ -177,11 +173,12 @@ public function getAllSelectedColumns()
177173
public function add(Builder $builder, array $columns = [])
178174
{
179175
$builderModel = $builder->getModel();
176+
$builderModelClass = get_class($builder->getModel());
180177

181-
$this->selectModelsColumns[get_class($builderModel)] = $columns
178+
$this->selectModelsColumns[$builderModelClass] = $columns
182179
?: Schema::getColumnListing($builderModel->getTable());
183180

184-
$this->builders[] = $builder;
181+
$this->builders[$builderModelClass] = $builder;
185182

186183
return $this;
187184
}
@@ -190,11 +187,12 @@ public function add(Builder $builder, array $columns = [])
190187
* Forward next call only on the specified model's builder.
191188
*
192189
* @param class-string<\Illuminate\Database\Eloquent\Model> $model
190+
* @param \Closure $callback
193191
* @return $this
194192
*/
195-
public function callOnly($model)
193+
public function callingOnly(string $model, Closure $callback)
196194
{
197-
$this->callingOnly[] = $model;
195+
$callback($this->builders[$model]);
198196

199197
return $this;
200198
}
@@ -209,15 +207,9 @@ public function callOnly($model)
209207
public function __call($method, $arguments)
210208
{
211209
foreach ($this->builders as $builder) {
212-
if (! empty($this->callingOnly) && ! in_array(get_class($builder->getModel()), $this->callingOnly)) {
213-
continue;
214-
}
215-
216210
$this->forwardCallTo($builder, $method, $arguments);
217211
}
218212

219-
$this->callingOnly = [];
220-
221213
return $this;
222214
}
223215
}

tests/UnionBuilderTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace OpenSoutheners\LaravelEloquentUnionBuilder\Tests;
44

55
use Exception;
6+
use Illuminate\Database\Eloquent\Builder;
67
use OpenSoutheners\LaravelEloquentUnionBuilder\Tests\Fixtures\Post;
78
use OpenSoutheners\LaravelEloquentUnionBuilder\Tests\Fixtures\Tag;
89
use OpenSoutheners\LaravelEloquentUnionBuilder\Tests\Fixtures\User;
@@ -119,9 +120,9 @@ public function testUnionBuilderSearchThrowsExceptionWhenNonSearchableModelFound
119120
public function testUnionBuilderCallOnlyWhereOnPostReturnsFilteredPostsOnly()
120121
{
121122
$queryResults = UnionBuilder::from([Tag::class, Post::class])
122-
->callOnly(Post::class)
123-
->where('slug', 'hello-world')
124-
->get();
123+
->callingOnly(Post::class, function (Builder $query) {
124+
$query->where('slug', 'hello-world');
125+
})->get();
125126

126127
$this->assertCount(3, $queryResults);
127128

0 commit comments

Comments
 (0)