Skip to content

Commit 3a518c7

Browse files
committed
Renamed when() to whenHas() + Added whenFilled() method
1 parent e00a84b commit 3a518c7

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

src/Illuminate/Http/Concerns/InteractsWithInput.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,18 +432,34 @@ protected function retrieveItem($source, $key, $default)
432432
}
433433

434434
/**
435-
* Apply the callback if the request contains a given input item key.
435+
* Apply the callback if the request contains the given input item key.
436436
*
437437
* @param string $key
438438
* @param callable $callback
439439
* @return $this
440440
*/
441-
public function when($key, callable $callback)
441+
public function whenHas($key, callable $callback)
442442
{
443443
if ($this->has($key)) {
444444
$callback(data_get($this->all(), $key));
445445
}
446446

447447
return $this;
448448
}
449+
450+
/**
451+
* Apply the callback if the request contains a non-empty value for the given input item key.
452+
*
453+
* @param string $key
454+
* @param callable $callback
455+
* @return $this
456+
*/
457+
public function whenFilled($key, callable $callback)
458+
{
459+
if ($this->filled($key)) {
460+
$callback(data_get($this->all(), $key));
461+
}
462+
463+
return $this;
464+
}
449465
}

tests/Http/HttpRequestTest.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -300,35 +300,60 @@ public function testHasMethod()
300300
$this->assertTrue($request->has('foo.baz'));
301301
}
302302

303-
public function testWhenMethod()
303+
public function testWhenHasMethod()
304304
{
305305
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]);
306306

307-
$name = null;
308-
$age = null;
309-
$city = '';
310-
$foo = null;
307+
$name = $age = $city = $foo = false;
311308

312-
$request->when('name', function ($value) use (&$name) {
309+
$request->whenHas('name', function ($value) use (&$name) {
313310
$name = $value;
314311
});
315312

316-
$request->when('age', function ($value) use (&$age) {
313+
$request->whenHas('age', function ($value) use (&$age) {
317314
$age = $value;
318315
});
319316

320-
$request->when('city', function ($value) use (&$city) {
317+
$request->whenHas('city', function ($value) use (&$city) {
321318
$city = $value;
322319
});
323320

324-
$request->when('foo', function () use (&$foo) {
321+
$request->whenHas('foo', function () use (&$foo) {
325322
$foo = 'test';
326323
});
327324

328325
$this->assertSame('Taylor', $name);
329326
$this->assertSame('', $age);
330327
$this->assertNull($city);
331-
$this->assertNull($foo);
328+
$this->assertFalse($foo);
329+
}
330+
331+
public function testWhenFilledMethod()
332+
{
333+
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]);
334+
335+
$name = $age = $city = $foo = false;
336+
337+
$request->whenFilled('name', function ($value) use (&$name) {
338+
$name = $value;
339+
});
340+
341+
$request->whenFilled('age', function ($value) use (&$age) {
342+
$age = 'test';
343+
});
344+
345+
$request->whenFilled('city', function ($value) use (&$city) {
346+
$city = 'test';
347+
});
348+
349+
$request->whenFilled('foo', function () use (&$foo) {
350+
$foo = 'test';
351+
});
352+
353+
$this->assertSame('Taylor', $name);
354+
$this->assertFalse($age);
355+
$this->assertFalse($city);
356+
$this->assertFalse($foo);
332357
}
333358

334359
public function testMissingMethod()

0 commit comments

Comments
 (0)