Skip to content

Commit c17b6a1

Browse files
committed
Merge branch 'pascalbaljet/7.x' into 7.x
2 parents f228374 + 9ed3046 commit c17b6a1

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/Illuminate/Http/Concerns/InteractsWithInput.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ public function hasAny($keys)
106106
return Arr::hasAny($input, $keys);
107107
}
108108

109+
/**
110+
* Apply the callback if the request contains the given input item key.
111+
*
112+
* @param string $key
113+
* @param callable $callback
114+
* @return $this|mixed
115+
*/
116+
public function whenHas($key, callable $callback)
117+
{
118+
if ($this->has($key)) {
119+
return $callback(data_get($this->all(), $key)) ?: $this;
120+
}
121+
122+
return $this;
123+
}
124+
109125
/**
110126
* Determine if the request contains a non-empty value for an input item.
111127
*
@@ -163,6 +179,22 @@ public function anyFilled($keys)
163179
return false;
164180
}
165181

182+
/**
183+
* Apply the callback if the request contains a non-empty value for the given input item key.
184+
*
185+
* @param string $key
186+
* @param callable $callback
187+
* @return $this|mixed
188+
*/
189+
public function whenFilled($key, callable $callback)
190+
{
191+
if ($this->filled($key)) {
192+
return $callback(data_get($this->all(), $key)) ?: $this;
193+
}
194+
195+
return $this;
196+
}
197+
166198
/**
167199
* Determine if the request is missing a given input item key.
168200
*

tests/Http/HttpRequestTest.php

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

303+
public function testWhenHasMethod()
304+
{
305+
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]);
306+
307+
$name = $age = $city = $foo = false;
308+
309+
$request->whenHas('name', function ($value) use (&$name) {
310+
$name = $value;
311+
});
312+
313+
$request->whenHas('age', function ($value) use (&$age) {
314+
$age = $value;
315+
});
316+
317+
$request->whenHas('city', function ($value) use (&$city) {
318+
$city = $value;
319+
});
320+
321+
$request->whenHas('foo', function () use (&$foo) {
322+
$foo = 'test';
323+
});
324+
325+
$this->assertSame('Taylor', $name);
326+
$this->assertSame('', $age);
327+
$this->assertNull($city);
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);
357+
}
358+
303359
public function testMissingMethod()
304360
{
305361
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]);

0 commit comments

Comments
 (0)