Skip to content

Commit aa55578

Browse files
authored
Optimized the implementation of When Method.
1 parent e94b5e1 commit aa55578

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/Builder.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,15 @@ public function orderBy(string $column, ?string $direction = 'asc'): static
129129

130130
/**
131131
* Apply the callback's query changes if the given "value" is true.
132-
* @param mixed $value
132+
*
133+
* @param callable($this, $value): $this $callback
134+
* @param callable($this, $value): $this $default
135+
* @return $this
133136
*/
134-
public function when($value, callable $callback, ?callable $default = null): static
137+
public function when(mixed $value, callable $callback, ?callable $default = null): static
135138
{
139+
$value = $value instanceof Closure ? $value($this) : $value;
140+
136141
if ($value) {
137142
return $callback($this, $value) ?: $this;
138143
}

tests/Cases/BuilderTest.php

+41-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function tearDown(): void
3434
$this->assertTrue(true);
3535
}
3636

37-
public function testPaginationCorrectlyHandlesPaginatedResults()
37+
public function testPaginationCorrectlyHandlesPaginatedResults(): void
3838
{
3939
Paginator::currentPageResolver(function () {
4040
return 1;
@@ -52,7 +52,7 @@ public function testPaginationCorrectlyHandlesPaginatedResults()
5252
$builder->paginate();
5353
}
5454

55-
public function testMacroable()
55+
public function testMacroable(): void
5656
{
5757
Builder::macro('foo', function () {
5858
return 'bar';
@@ -64,15 +64,51 @@ public function testMacroable()
6464
);
6565
}
6666

67-
public function testHardDeleteDoesntSetWheres()
67+
public function testHardDeleteDoesntSetWheres(): void
6868
{
6969
$builder = new Builder($model = m::mock(Model::class), 'zonda', null, false);
7070
$this->assertArrayNotHasKey('__soft_deleted', $builder->wheres);
7171
}
7272

73-
public function testSoftDeleteSetsWheres()
73+
public function testSoftDeleteSetsWheres(): void
7474
{
75-
$builder = new Builder($model = m::mock(Model::class), 'zonda', null, true);
75+
$builder = new Builder(m::mock(Model::class), 'zonda', null, true);
7676
$this->assertEquals(0, $builder->wheres['__soft_deleted']);
7777
}
78+
79+
public function testWhen(): void
80+
{
81+
$builder = new Builder(m::mock(Model::class), 'zonda', null, true);
82+
$builder->when(true, fn (Builder $collection) => $collection->take(1))
83+
->when(false, fn (Builder $collection) => $collection->take(2));
84+
$this->assertEquals(1, $builder->limit);
85+
}
86+
87+
public function testWhenWithValueForCallback(): void
88+
{
89+
$callback = fn (Builder $collection, int $value) => $collection->take($value);
90+
91+
$builder = new Builder(m::mock(Model::class), 'zonda', null, true);
92+
$builder->when(0, $callback)->when(1, $callback);
93+
$this->assertEquals(1, $builder->limit);
94+
}
95+
96+
public function testWhenValueOfClosure(): void
97+
{
98+
$callback = fn (Builder $collection, int $value) => $collection->take($value);
99+
100+
$builder = new Builder(m::mock(Model::class), 'zonda', null, true);
101+
$builder->when(fn () => 0, $callback)->when(fn () => 1, $callback);
102+
$this->assertEquals(1, $builder->limit);
103+
}
104+
105+
public function testWhenCallbackWithDefault(): void
106+
{
107+
$callback = fn (Builder $collection, int $value) => $collection;
108+
$default = fn (Builder $collection, $value) => $collection->take($value);
109+
110+
$builder = new Builder(m::mock(Model::class), 'zonda', null, true);
111+
$builder->when(fn () => 0, $callback, $default)->when(fn () => 1, $callback, $default);
112+
$this->assertEquals(0, $builder->limit);
113+
}
78114
}

0 commit comments

Comments
 (0)